Masked Textbox questions

melvados

Member
Joined
Aug 6, 2008
Messages
5
Programming Experience
Beginner
I seen this code at the MSDN library

VB.NET:
Private Sub MaskedTextBox1_MaskInputRejected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected
    If (Me.MaskedTextBox1.MaskFull) Then
        ToolTip1.ToolTipTitle = "Input Rejected - Too Much Data"
        ToolTip1.Show("You cannot enter any more data into the date field. Delete some characters in order to insert more data.", Me.MaskedTextBox1, Me.MaskedTextBox1.Location.X, Me.MaskedTextBox1.Location.Y, 5000)
    ElseIf (e.Position = Me.MaskedTextBox1.Mask.Length) Then
        ToolTip1.ToolTipTitle = "Input Rejected - End of Field"
        ToolTip1.Show("You cannot add extra characters to the end of this date field.", Me.MaskedTextBox1, 0, -20, 5000)
    Else
        ToolTip1.ToolTipTitle = "Input Rejected"
        ToolTip1.Show("You can only add numeric characters (0-9) into this date field.", Me.MaskedTextBox1, 0, -20, 5000)
    End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.ToolTip1.IsBalloon = True
    Me.MaskedTextBox1.Mask = "00/00/0000"
End Sub

Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MaskedTextBox1.KeyDown
    ' The balloon tip is visible for five seconds; if the user types any data before it disappears, collapse it ourselves.
    Me.ToolTip1.Hide(Me.MaskedTextBox1)
End Sub

I changed this line Me.MaskedTextBox1.Mask = "00/00/0000" into a money format where users will type Me.MaskedTextBox1.Mask = "0000.00"

How can I make it in a way that it will not be limited to 6 digits? I mean like users may input 123.45 instead, but if were to use the modified code I change, instead of getting 123.45, it will gives 1234.56
 
Hi pesquinaldo, thanks you for your reply.
Yes, I have tried "#,###,###.##" format string instead of the zeros string. But is it possible for the cursor of start from the end and input numbers?

Example, users input 54312 and it will apeears as like this : (I will write is as a 'step by step')
1) _,___,___._5
2) _,___,___.54
3) _,___,__5.43
4) _,___,_54.31
5) _,___,543.12

Is it possible to do this?
 
Formatting Characters

Yes, I believe so.

I've use this formatting string often. The leading "#" and "," are ignored so entering the number 50 (with the format #,###,###.##) will produce the number 50 as a result. Changing the format to #,###,###.99) will produce the number 50.00 from the same input.

Gah, I just read your post more carefully. No, the formatting scheme (above) won't do what you need. Give me a while to remember how to accomplish this.
 
Use a NumericUpDown control instead.
 
HI, I have come across the same requirement and found this thread.

However I cannot seem to get the updown control to perform as melvados describes as his requirement, as is mine.

I can't even get the control to not display the scroll buttons :-(
 
I can't even get the control to not display the scroll buttons :-(
It is not an optional element of the control, but an important part in the user interface for selecting numbers. Though from my try this works (I would never do it really :)):
VB.NET:
Me.NumericUpDown1.Controls(0).Visible = False
as per requirement
Are you talking about aligning text to right? Set TextAlign to Right.
 
Hi,

I have it kind of sorted like this (read my comments at the bottom of post!

VB.NET:
Private Sub btnThree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThree.Click

        If CheckForDecimalPoint() Then

            Me.txtBetAmount.Text &= "3"

        End If

    End Sub

Private Sub btnPoint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPoint.Click

        If Me.txtBetAmount.Text.Contains(".") Then

            Exit Sub

        End If

        Me.txtBetAmount.Text &= "."

        blnDecimalPoint = True

    End Sub

Private Function CheckForDecimalPoint() As Boolean

        If blnDecimalPoint Then

            If Me.txtBetAmount.Text.Length - Me.txtBetAmount.Text.IndexOf(".") = 3 Then

                Return False

            End If

        End If

        Return True

    End Function

Someone taught me years ago there are two types of code efficient and ineffiecient. If it works it is right!

So my code is right justified and the user can clcik the number buttons on the form to enter a number and then the decimal point button where required....just like a calculator.

This code then checks the position of the decimal point to ensure only two numbers are entered to the right of the point.

It works a treat....but I always have to ask myself is this efficient when I do things this way? It works so I am happy but it ......... you know ?

:)
 
Back
Top