locked
Restricted data input for a Textbox RRS feed

  • Question

  • I want to restrict user to input data in a textbox as under:
    1) Textbox before input should display only 00.00
    2) User may only input maximum of  two digits before "." and maximum of two digits after "."
    like 22.44, 2.40, 32.04
    3) If user inputs only 2 the textbox should display it as 00.02
       
    If user inputs only 20 the textbox should display it as 0.20
        If user inputs 202 the textbox should display it as 2.02
        if user inputs 3202 the textbox should display it as 32.02
        if user inputs 43202 the textbox should display an error message that only 4 digits are admissible to be put in.

    I tried following code to restrict user to only input numeric data:

    If TextBox1.Text <> "" And Not IsNumeric(TextBox1.Text) Then
                MessageBox.Show("Please enter only numeric data")
                TextBox1.Clear()
                TextBox1.Select()
            End If

    Also i tried the following code

    Private Function NumbersOnly(ByVal pstrChar As Char, ByVal oTextBox As TextBox) As Boolean
            'validate the entry for a textbox limiting it to only numeric values and the decimal point
            If (Convert.ToString(pstrChar) = "." And InStr(oTextBox.Text, ".")) Then Return True 'accept only one instance of the decimal point
            If Convert.ToString(pstrChar) <> "." And pstrChar <> vbBack Then
                Return IIf(IsNumeric(pstrChar), False, True) 'check if numeric is returned
            End If
            Return False 'for backspace
        End Function

    But it is not giving the results i require. Please advise how i can ammend this code to attain the requirements at Serial No. 1,2&3 above.
    Thanks


     

    Saturday, August 15, 2009 1:45 AM

Answers

  • I have trouble correcting my earlier code. I did not see that you want the output in the form "00.00". Below is an amendment to that effect. Just copy, paste and run:

      Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
    
    
            Dim myVal As String = TextBox1.Text
            If myVal.Length > 4 Then
                MsgBox("Only maximum of four digits is allowed")
                TextBox1.Select()
    
            ElseIf Not IsNumeric(myVal) Then
                Debug.WriteLine(TextBox1.Text)
                MessageBox.Show("Please enter numbers only", "Data Entry Error")
                TextBox1.Select()
    
            Else
                TextBox1.Text = CStr(CDbl(myVal) / 100)
                If myVal.Length < 4 Then
                    TextBox1.Text = "0" & CStr(CDbl(myVal) / 100)
                End If
    
            End If
    
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1 .Text = "00.00"
        End Sub

    Only performance counts!
    • Marked as answer by sazd1 Sunday, August 16, 2009 1:53 AM
    Saturday, August 15, 2009 2:28 PM
  • The cursor doesn't leave because it's not supposed to.

    This will fix the problem with the extra character if no decimal point is there

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    
            Dim point As Boolean = False
            If TextBox1.Text.Contains("."c) Then
                point = True
                TextBox1.MaxLength = 5
            End If
    
            If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or (e.KeyChar = "." And Not point)) Then
                e.Handled = True
            End If
    
        End Sub
    
        Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
            Dim mynumber As String = TextBox1.Text
            If Not TextBox1.Text.Contains(".") Then
                Select Case TextBox1.TextLength
                    Case 1
                        mynumber = "00.0" & mynumber
                    Case 2
                        mynumber = "00." & mynumber
                    Case 3
                        mynumber = "0" & TextBox1.Text.Insert(1, ".")
                    Case 4
                        mynumber = TextBox1.Text.Insert(2, ".")
                End Select
            Else
                Dim mydec As Decimal = CDec(TextBox1.Text)
                TextBox1.Text = mydec.ToString("00.00")
                Exit Sub
            End If
    
            TextBox1.Text = mynumber
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1.Text = "00.00"
            TextBox1.MaxLength = 5
        End Sub
    
    
        Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            If TextBox1.Text.Contains("."c) AndAlso TextBox1.Text.IndexOf("."c) > 2 Then
                TextBox1.Clear()
                TextBox1.Focus()
            End If
            If Not TextBox1.Text.Contains("."c) Then
                TextBox1.MaxLength = 4
            End If
        End Sub
    • Marked as answer by sazd1 Sunday, August 16, 2009 1:54 AM
    Saturday, August 15, 2009 3:21 PM

All replies

  • Hi,
    You can use the MaskedTextBox.

    Drag and drop a MaskedTextBox and set the values for it as:

            MaskedTextBox1.Mask = "##.##"
            MaskedTextBox1.Text = "0000"
    

    This will let you only to have the numeric values and only numeric values.

    For you to convert the value when entered as 02.02 to result as 2.02 you can use:

    desired_value = FormatNumber(MaskedTextBox1.Text)


    HTH - if so mark as answer so that this question can be closed - happy programming.
    • Edited by Novice2Pro Saturday, August 15, 2009 2:06 AM
    Saturday, August 15, 2009 1:51 AM
  • Hi Novice
    I tried that already, I think it is more close to my requirement but it was not behaving perfectly. I used following code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            MaskedTextBox1.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
        End Sub
    
    Private Sub MaskedTextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaskedTextBox1.Leave
            If MaskedTextBox1.Text <> "" Then
                MaskedTextBox1.Text = Format(CDec(MaskedTextBox1.Text), "00.00")
            End If
        End Sub

    When i input only 2 it displays it as 02.00 my requirement is it should display it as 00.02

    when i input 20 it takes it as 20.00 my requirement is it should display it as 00.20 and so on.
    Also when i input 213402 it displays it as 213402.00 and does not give an error because i want to restrict user to input only four digits.
    Also when i input 213402.32456 it displays it as 21340232456.00
    Please advise how i can restrict this to my requirements.

    Saturday, August 15, 2009 2:09 AM
  • you can put in whatever messageboxes you want - I use them sparingly. This just stops accepting characters when = 4. It also only allows 1 decimal point, and won't accept a decimal point past the third character


    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            
            Dim point As Boolean = False
            If TextBox1.Text.Contains("."c) Then
                point = True
            End If
    
            If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or (e.KeyChar = "." And Not point)) Then
                e.Handled = True
            End If
    
        End Sub
    
        Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
            Dim mynumber As String = TextBox1.Text
            If Not TextBox1.Text.Contains(".") Then
                Select Case TextBox1.TextLength
                    Case 1
                        mynumber = "00.0" & mynumber
                    Case 2
                        mynumber = "00." & mynumber
                    Case 3
                        mynumber = "0" & TextBox1.Text.Insert(1, ".")
                    Case 4
                        mynumber = TextBox1.Text.Insert(2, ".")
                End Select
            Else
                Dim mydec As Decimal = CDec(TextBox1.Text)
                TextBox1.Text = mydec.ToString("00.00")
                Exit Sub
            End If
    
            TextBox1.Text = mynumber
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1.Text = "00.00"
            TextBox1.MaxLength = 5
        End Sub
    
        
        Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            If TextBox1.Text.Contains("."c) AndAlso TextBox1.Text.IndexOf("."c) > 2 Then
                TextBox1.Clear()
                TextBox1.Focus()
            End If
        End Sub


    • Proposed as answer by Rahul P Nath Saturday, August 15, 2009 1:38 PM
    Saturday, August 15, 2009 2:59 AM
  • Hi Jwavila

    Thanks for your reply.Yes this code is working fine as i required with a perfect go. I am having only two problems with this:

    Firstly,when i input some digits and press enter the cursor stays there in the textbox and not leaves the textbox.
    Secondly, when i type 3245 it displays it as 32.45 that is fine to be displayed and is required to do the same. But when i type 32456 it also accepts this kind of figure and displays it as 32456 which it should not be done, i think it is accepting the fifth digit in place of "."
    Please advise and thanks once again for your provision of code.

    Saturday, August 15, 2009 11:46 AM
  •  Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
    
    
            Dim myVal As String = TextBox1.Text
            If myVal.Length > 4 Then
                MsgBox("Only maximum of four digits is allowed")
                TextBox1.Select()
    
            ElseIf Not IsNumeric(myVal) Then
                Debug.WriteLine(TextBox1.Text)
                MessageBox.Show("Please enter numbers only", "Data Entry Error")
                TextBox1.Select()
            Else
                TextBox1.Text = CStr(CDbl(myVal) / 100)
    
            End If
    
    
    




    End Sub


      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1.Text = "00.00"
        End Sub


    Only performance counts!
    Saturday, August 15, 2009 1:57 PM
  • I have trouble correcting my earlier code. I did not see that you want the output in the form "00.00". Below is an amendment to that effect. Just copy, paste and run:

      Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
    
    
            Dim myVal As String = TextBox1.Text
            If myVal.Length > 4 Then
                MsgBox("Only maximum of four digits is allowed")
                TextBox1.Select()
    
            ElseIf Not IsNumeric(myVal) Then
                Debug.WriteLine(TextBox1.Text)
                MessageBox.Show("Please enter numbers only", "Data Entry Error")
                TextBox1.Select()
    
            Else
                TextBox1.Text = CStr(CDbl(myVal) / 100)
                If myVal.Length < 4 Then
                    TextBox1.Text = "0" & CStr(CDbl(myVal) / 100)
                End If
    
            End If
    
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1 .Text = "00.00"
        End Sub

    Only performance counts!
    • Marked as answer by sazd1 Sunday, August 16, 2009 1:53 AM
    Saturday, August 15, 2009 2:28 PM
  • The cursor doesn't leave because it's not supposed to.

    This will fix the problem with the extra character if no decimal point is there

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    
            Dim point As Boolean = False
            If TextBox1.Text.Contains("."c) Then
                point = True
                TextBox1.MaxLength = 5
            End If
    
            If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or (e.KeyChar = "." And Not point)) Then
                e.Handled = True
            End If
    
        End Sub
    
        Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
            Dim mynumber As String = TextBox1.Text
            If Not TextBox1.Text.Contains(".") Then
                Select Case TextBox1.TextLength
                    Case 1
                        mynumber = "00.0" & mynumber
                    Case 2
                        mynumber = "00." & mynumber
                    Case 3
                        mynumber = "0" & TextBox1.Text.Insert(1, ".")
                    Case 4
                        mynumber = TextBox1.Text.Insert(2, ".")
                End Select
            Else
                Dim mydec As Decimal = CDec(TextBox1.Text)
                TextBox1.Text = mydec.ToString("00.00")
                Exit Sub
            End If
    
            TextBox1.Text = mynumber
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1.Text = "00.00"
            TextBox1.MaxLength = 5
        End Sub
    
    
        Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            If TextBox1.Text.Contains("."c) AndAlso TextBox1.Text.IndexOf("."c) > 2 Then
                TextBox1.Clear()
                TextBox1.Focus()
            End If
            If Not TextBox1.Text.Contains("."c) Then
                TextBox1.MaxLength = 4
            End If
        End Sub
    • Marked as answer by sazd1 Sunday, August 16, 2009 1:54 AM
    Saturday, August 15, 2009 3:21 PM
  • Hi Sylvia
    Thanks for your reply. Your code is working well. Thanks for your response to my query.
    Sunday, August 16, 2009 1:53 AM
  • hmmm...

    I couldn't get Sylva's code to meet all of your requirements.

    try things like:

    20

    1.23

    12.34

    But if it works for you, use it :)
    Sunday, August 16, 2009 2:23 AM
  • Hi Jwavila
    Thanks for your response and helping to clear this issue for me. It is working so well.
    I want to make this code a comprehensive one for Numeric entry. Please guide me for my following queries in this regard:

    1)
    the textbox shows 00.00 which is fine and it should display this. My query is that i cannot type in textbox unless i select 00.00. This is because the code has a fixed length of 5 digits so the cursor remains at last position and does not allow to type beyond 00.00. Even i put the cursor after first 0 it does not allow any digit to enter.
    I want that it shows 00.00 as it is doing but it should allow the insersion of digits .......like if i want to type 36.45 then  if i type 5 the texbox should immediately look like this 00.05 when i type next digit it should look like this 00.45 likewise when i type next digit it should display 06.45 and finally 36.45. i hope i am able to clear my requirement for this issue.

    2)
    In the existing code when i type in the textbox 23.4 the textbox displays it as 234,00.
    Please guide me on these issues.

    Thanks

    Sunday, August 16, 2009 2:31 AM