Results 1 to 5 of 5

Thread: help with characters in a text box

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60

    Angry help with characters in a text box

    I'm new to VB .NET and need some help please.

    I need help with what code to use for a text box that only can allow numeric keys and the Backspace key. The number can only be 50 or less, otherwise an error message is displayed.

    Thank you very much!

  2. #2
    Member
    Join Date
    Sep 2002
    Posts
    37
    The following code will allow only numeric values, backspace key and the tab key to leave the textbox. In your validation routine you can check that the value is less then 50.


    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress


    Dim I As Integer = Convert.ToInt32(e.KeyChar)
    Select Case I
    Case 48 To 57 ' 0-9 (digit)
    e.Handled = False
    Case 8 ' allow backspace key
    e.Handled = False
    Case 9 ' allow Tab key
    e.Handled = False
    Case Else
    e.Handled = True
    End Select

    End Sub


    Harold Hoffman

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    There are also other functions in the framework..
    Code:
            Dim key As Integer = Convert.ToInt32(e.KeyChar)
            If Not (System.Char.IsNumber(e.KeyChar)) Then
                If key <> 8 Then
                    e.Handled = True
                End If
            End If
    Nevermind, I just saw that the number has to be less than 50. Go with the above code, but keep in mind the System.Char class for stuff like this in the future.

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    Thanks for the help guys. I'm not quite understanding the code and can't get it to quite work. It blocks all chars except 0-9, but doesn't limit it to 50. The value in the text box is to be multiplied by 80, and displayed in a label as currency. I have that all working except for limiting it to 50. Its also only computing the first digit * 80 until another character is entered. It should be real time. Help! Thanks!

    <vbscript>
    Private Sub EntNumTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles EnterNumTextBox.KeyPress
    Dim I As Integer = Convert.ToInt32(e.KeyChar)
    Select Case I
    Case 48 To 57 ' 0-9 (digit)
    e.Handled = False
    Case 8 ' allow backspace key
    e.Handled = False
    Case 9 ' allow Tab key
    e.Handled = False
    Case Else
    e.Handled = True
    End Select
    Me.TotalLabel.Text = Val(Me.EnterNumTextBox.Text) * 80
    Me.TotalLabel.Text = Format(Me.TotalLabel.Text, "currency")
    End Sub
    </vbscript>

  5. #5
    New Member
    Join Date
    Feb 2001
    Location
    Woodstock, NB Canada
    Posts
    6
    On the KeyPress event when you are validating for numeric data - throw in a validation of the text already in the textbox with the text you are trying to add.....

    e.g.
    Let's say you have textbox1="9"
    Now, you try to input any other number
    (making the number 90-99)


    On the keypress event of the textbox, do this....

    If Val(textbox1) > 50 then
    Messagebox.show ("Value cannot exceed 50, please try again")
    textbox1.SelectAll
    textbox1.focus()
    Endif

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width