Results 1 to 5 of 5

Thread: limit textbox keypress to numeric values.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    limit textbox keypress to numeric values.

    In VB6 I would limit the characters allowed in a text box by saying
    VB Code:
    1. if keyascii=??? then
    2.     keyascii=0
    3. endif
    The ??? being what ever I wanted to compare against
    Now with .Net there is only a readonly keychar property.

    How do I limit keypresses to numeric values.


    Things I do when I am bored: DotNetable

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.  
    3.         If e.KeyChar < "0"c Or e.KeyChar > "9"c Then
    4.             e.Handled = True
    5.         End If
    6.  
    7.     End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Member
    Join Date
    Apr 2004
    Posts
    44
    You need to use the keydown and keypress methods for each box that you are limiting. You also have to allow the - and . keys as well as all of the numpad ones.

    Keydown calls this

    Code:
            Private Sub OnlyNumbers(ByVal e As System.Windows.Forms.KeyEventArgs)
            ' Initialize the flag to false.
            nonNumberEntered = False
    
            ' Determine whether the keystroke is a number from the top of the keyboard.
            If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
                ' Determine whether the keystroke is a number from the keypad.
                If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
                    ' Determine whether the keystroke is a backspace.
                    If e.KeyCode <> Keys.Back Then
                        ' Determine whether the keystroke is a . or a - on either the main keyboard or keypad
                        If e.KeyCode <> Keys.Subtract Then  'keypad
                            If e.KeyCode <> Keys.Decimal Then 'keypad
                                If e.KeyCode <> Keys.OemPeriod Then 'keyboard
                                    If e.KeyCode <> Keys.OemMinus Then 'keyboard
                                        ' A non-numerical keystroke was pressed. 
                                        ' Set the flag to true and evaluate in KeyPress event.
                                        nonNumberEntered = True
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End Sub
    This goes within keyPress:

    Code:
            ' Check for the flag being set in the KeyDown event.
            If nonNumberEntered = True Then
                ' Stop the character from being entered into the control since it is non-numerical.
                e.Handled = True
            End If
    Hope that helps

    Jim

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    Now thats more complicated than it used to be.

    Thanks.


    Things I do when I am bored: DotNetable

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    yaksplat
    Your code doesn't work at all, I can type anything and everything in the textbox.

    crptcblade
    Yours is too good, I forgot to mention that I need to allow the backspace button to be pressed.


    Things I do when I am bored: DotNetable

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