Home All Groups Group Topic Archive Search About
Author
28 Sep 2006 12:03 PM
Peter Newman
vb.net 2003
i have the following procedure

Private Sub StrLicence_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles StrLicence.KeyPress
        Select Case e.KeyChar
            Case Microsoft.VisualBasic.ChrW(13)
                MsgBox(" ENTER PRESSED")
' move to next field
            Case Microsoft.VisualBasic.ChrW(8)
                MsgBox(" backspace PRESSED")
            Case Microsoft.VisualBasic.ChrW(9)
                MsgBox("TAB PRESSED")
' move to next field 
          Case Microsoft.VisualBasic.ChrW(48) To
Microsoft.VisualBasic.ChrW(57)
                MsgBox(" NUMBER PRESSED")
            Case Else
' show error
                MsgBox("MUST BE NUMERIC")
        End Select
    End Sub

what i want to do is in this case if an alpha charachter is pressed, i show
the must be numeric message, but i also want to stop that character being
displayed ...

Author
28 Sep 2006 6:31 PM
Tom Garth
The procedure I use I've updated from a VB function for KeyAscii.

Here is an example of the call.

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

    e.KeyChar = NumericChar(e.KeyChar, , True)

End Sub

Public Function NumericChar(ByVal KeyChar As Char, Optional ByVal
AllowNegative As Boolean = False, Optional ByVal AllowDecimal As Boolean =
False) As Char

    Select Case Asc(KeyChar)

        Case 8, 48 To 57

            NumericChar = KeyChar

        Case 45

            If AllowNegative = True Then

                NumericChar = KeyChar

            End If

        Case 46

            If AllowDecimal = True Then

                NumericChar = KeyChar

            End If

    End Select

End Function

Show quoteHide quote
"Peter Newman" <PeterNew***@discussions.microsoft.com> wrote in message
news:51EE633E-E28B-4AC5-9D20-CA98AE124426@microsoft.com...
> vb.net 2003
> i have the following procedure
>
> Private Sub StrLicence_KeyPress(ByVal sender As Object, ByVal e As
> System.Windows.Forms.KeyPressEventArgs) Handles StrLicence.KeyPress
>        Select Case e.KeyChar
>            Case Microsoft.VisualBasic.ChrW(13)
>                MsgBox(" ENTER PRESSED")
> ' move to next field
>            Case Microsoft.VisualBasic.ChrW(8)
>                MsgBox(" backspace PRESSED")
>            Case Microsoft.VisualBasic.ChrW(9)
>                MsgBox("TAB PRESSED")
> ' move to next field
>          Case Microsoft.VisualBasic.ChrW(48) To
> Microsoft.VisualBasic.ChrW(57)
>                MsgBox(" NUMBER PRESSED")
>            Case Else
> ' show error
>                MsgBox("MUST BE NUMERIC")
>        End Select
>    End Sub
>
> what i want to do is in this case if an alpha charachter is pressed, i
> show
> the must be numeric message, but i also want to stop that character being
> displayed ...
Author
28 Sep 2006 9:36 PM
Sca_Tone
Hi Peter,
   By using the e.Handled property you can stop the character output to
the textbox.
Also consider using regular expressions for validation.

        Select Case Char.IsDigit(e.KeyChar)
            Case True
                '/ Valid Character Input
            Case False
                '/ Invalid Character Input
                MessageBox.Show("Invalid Character Input")
                e.Handled = True
        End Select


        '/ SAME RESULT BUT USING REGULAR EXPRESSIONS
        If System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar,
"[0-9]") Then
            '/ Valid Character Input
        Else
            '/ Invalid Character Input
            MessageBox.Show("Invalid Character Input")
            e.Handled = True
        End If

Regards,
    Tony