Re: Restrict keys in textbox w/o KeyAscii?

Discussion in 'Microsoft VB .NET' started by hansolo, May 28, 2004.

  1. hansolo

    hansolo Guest

    > Ok folks. Had some code in VB6 that worked fine within a TextBox KeyPress
    > events KeyAscii parameter for limiting the text in a text box to only
    > numbers, backspace and decimal characters. I understand the whole VB.Net
    > situation about not being able to assign anything other than zero (no key
    > stroke) to the eventArgs.KeyChar parm in the VB.Net TextBox KeyPress event
    > so I have rolled my own function as suggested by Microsoft to handle
    > limiting key strokes in a TextBox.
    >
    > The Function:
    >
    > Function RestrictTxt(ByVal keyz As Integer) As Boolean
    > If keyz >= 46 And keyz <= 57 Or keyz = 8 Or keyz = 110 Then
    > RestrictTxt = False
    > Else
    > RestrictTxt = True
    > End If
    > End Function
    >
    > The TextBox KeyPress Event using the function:
    >
    > Private Sub Text2_KeyPress(ByVal eventSender As System.Object, ByVal
    > eventArgs As_ System.Windows.Forms.KeyPressEventArgs) Handles

    Text2.KeyPress
    > Dim KeyAscii As Integer = Asc(eventArgs.KeyChar)
    > eventArgs.Handled = RestrictTxt(KeyAscii)
    > End Sub
    >
    > The Problem:
    >
    > It basically does what its supposed to except for two strange errors.
    > 1) When I press the "n" key which should have a key code value of 78, the
    > function is getting passed the value 110 which should be the Del (decimal)
    > key. As my function above allows the value 110, it passes False to
    > eventArgs.Handled and somehow the letter "n" is allowed in the textbox.
    >
    > 2) The only other key I am having trouble with is the "/". When I press

    the
    > "/" key (keypad or main keys) which should have a key code value of 111,

    the
    > function is getting passed the value 47 which should be the HELP key. As

    my
    > function above allows values 46 thru 57, it passes False to
    > eventArgs.Handled and somehow the character "/" is allowed in the textbox.
    >
    > If you know how these keys "n" and "/" are evaluating to the incorrect key
    > code values in my code but resulting in the correct character (which I

    don't
    > want) in the text box please let me know. If possible please try on your
    > system as I am thinking something is wrong with my keyboard :). In the
    > second case, I can easily edit the function to exclude value 47. It was
    > included to make the code simpler by using a range but in the first case,

    I
    > cannot exclude value 110 because I need the decimal character.
    >
    > Thanks,
    >
    > Chuck
    >
    >
    >
     
    hansolo, May 28, 2004
    #1
    1. Advertisements

  2. Can't seem to find the start of this thread?!
    Well, ayway

    There is nothing wrong with your keyboard
    You have your ASCII codes mixed up
    n = 110
    N = 78
    Backspace = 8
    / = 47

    ASCII for Del is irrelevant since it doesn't generate a KeyPress event
    Never heard of the HELP key. Do you mean F1?
    You should support ',' aswell (ASCII 44)

    /claes

    "hansolo" <> wrote in message
    news:vYytc.201$eP.39@lakeread01...
    > > Ok folks. Had some code in VB6 that worked fine within a TextBox

    KeyPress
    > > events KeyAscii parameter for limiting the text in a text box to only
    > > numbers, backspace and decimal characters. I understand the whole

    VB.Net
    > > situation about not being able to assign anything other than zero (no

    key
    > > stroke) to the eventArgs.KeyChar parm in the VB.Net TextBox KeyPress

    event
    > > so I have rolled my own function as suggested by Microsoft to handle
    > > limiting key strokes in a TextBox.
    > >
    > > The Function:
    > >
    > > Function RestrictTxt(ByVal keyz As Integer) As Boolean
    > > If keyz >= 46 And keyz <= 57 Or keyz = 8 Or keyz = 110 Then
    > > RestrictTxt = False
    > > Else
    > > RestrictTxt = True
    > > End If
    > > End Function
    > >
    > > The TextBox KeyPress Event using the function:
    > >
    > > Private Sub Text2_KeyPress(ByVal eventSender As System.Object, ByVal
    > > eventArgs As_ System.Windows.Forms.KeyPressEventArgs) Handles

    > Text2.KeyPress
    > > Dim KeyAscii As Integer = Asc(eventArgs.KeyChar)
    > > eventArgs.Handled = RestrictTxt(KeyAscii)
    > > End Sub
    > >
    > > The Problem:
    > >
    > > It basically does what its supposed to except for two strange errors.
    > > 1) When I press the "n" key which should have a key code value of 78,

    the
    > > function is getting passed the value 110 which should be the Del

    (decimal)
    > > key. As my function above allows the value 110, it passes False to
    > > eventArgs.Handled and somehow the letter "n" is allowed in the textbox.
    > >
    > > 2) The only other key I am having trouble with is the "/". When I press

    > the
    > > "/" key (keypad or main keys) which should have a key code value of 111,

    > the
    > > function is getting passed the value 47 which should be the HELP key.

    As
    > my
    > > function above allows values 46 thru 57, it passes False to
    > > eventArgs.Handled and somehow the character "/" is allowed in the

    textbox.
    > >
    > > If you know how these keys "n" and "/" are evaluating to the incorrect

    key
    > > code values in my code but resulting in the correct character (which I

    > don't
    > > want) in the text box please let me know. If possible please try on

    your
    > > system as I am thinking something is wrong with my keyboard :). In the
    > > second case, I can easily edit the function to exclude value 47. It was
    > > included to make the code simpler by using a range but in the first

    case,
    > I
    > > cannot exclude value 110 because I need the decimal character.
    > >
    > > Thanks,
    > >
    > > Chuck
    > >
    > >
    > >

    >
    >
     
    Claes Bergefall, May 28, 2004
    #2
    1. Advertisements

  3. hansolo

    hansolo Guest

    Thanks, you are correct as I am sure you know. I was getting my information
    from the Keys Enumeration member help topic in .NET 2003. Evedently a few
    things in this table do not match the Ascii codes. Pretty close though.

    Thanks for picking up on my error.

    Chuck


    "Claes Bergefall" <> wrote in message
    news:...
    > Can't seem to find the start of this thread?!
    > Well, ayway
    >
    > There is nothing wrong with your keyboard
    > You have your ASCII codes mixed up
    > n = 110
    > N = 78
    > Backspace = 8
    > / = 47
    >
    > ASCII for Del is irrelevant since it doesn't generate a KeyPress event
    > Never heard of the HELP key. Do you mean F1?
    > You should support ',' aswell (ASCII 44)
    >
    > /claes
    >
    > "hansolo" <> wrote in message
    > news:vYytc.201$eP.39@lakeread01...
    > > > Ok folks. Had some code in VB6 that worked fine within a TextBox

    > KeyPress
    > > > events KeyAscii parameter for limiting the text in a text box to only
    > > > numbers, backspace and decimal characters. I understand the whole

    > VB.Net
    > > > situation about not being able to assign anything other than zero (no

    > key
    > > > stroke) to the eventArgs.KeyChar parm in the VB.Net TextBox KeyPress

    > event
    > > > so I have rolled my own function as suggested by Microsoft to handle
    > > > limiting key strokes in a TextBox.
    > > >
    > > > The Function:
    > > >
    > > > Function RestrictTxt(ByVal keyz As Integer) As Boolean
    > > > If keyz >= 46 And keyz <= 57 Or keyz = 8 Or keyz = 110 Then
    > > > RestrictTxt = False
    > > > Else
    > > > RestrictTxt = True
    > > > End If
    > > > End Function
    > > >
    > > > The TextBox KeyPress Event using the function:
    > > >
    > > > Private Sub Text2_KeyPress(ByVal eventSender As System.Object, ByVal
    > > > eventArgs As_ System.Windows.Forms.KeyPressEventArgs) Handles

    > > Text2.KeyPress
    > > > Dim KeyAscii As Integer = Asc(eventArgs.KeyChar)
    > > > eventArgs.Handled = RestrictTxt(KeyAscii)
    > > > End Sub
    > > >
    > > > The Problem:
    > > >
    > > > It basically does what its supposed to except for two strange errors.
    > > > 1) When I press the "n" key which should have a key code value of 78,

    > the
    > > > function is getting passed the value 110 which should be the Del

    > (decimal)
    > > > key. As my function above allows the value 110, it passes False to
    > > > eventArgs.Handled and somehow the letter "n" is allowed in the

    textbox.
    > > >
    > > > 2) The only other key I am having trouble with is the "/". When I

    press
    > > the
    > > > "/" key (keypad or main keys) which should have a key code value of

    111,
    > > the
    > > > function is getting passed the value 47 which should be the HELP key.

    > As
    > > my
    > > > function above allows values 46 thru 57, it passes False to
    > > > eventArgs.Handled and somehow the character "/" is allowed in the

    > textbox.
    > > >
    > > > If you know how these keys "n" and "/" are evaluating to the incorrect

    > key
    > > > code values in my code but resulting in the correct character (which I

    > > don't
    > > > want) in the text box please let me know. If possible please try on

    > your
    > > > system as I am thinking something is wrong with my keyboard :). In

    the
    > > > second case, I can easily edit the function to exclude value 47. It

    was
    > > > included to make the code simpler by using a range but in the first

    > case,
    > > I
    > > > cannot exclude value 110 because I need the decimal character.
    > > >
    > > > Thanks,
    > > >
    > > > Chuck
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    hansolo, May 28, 2004
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.microsoft.com

    list of keys in keys enumeration

    news.microsoft.com, Nov 22, 2003, in forum: Microsoft VB .NET
    Replies:
    1
    Views:
    380
    Herfried K. Wagner [MVP]
    Nov 22, 2003
  2. Daren Hawes
    Replies:
    2
    Views:
    566
    Cor Ligthert
    Jul 27, 2004
  3. ScareCrow

    Limit textbox to numeric keys

    ScareCrow, Dec 19, 2004, in forum: Microsoft VB .NET
    Replies:
    5
    Views:
    334
    Guest
    Dec 19, 2004
  4. Luqman
    Replies:
    2
    Views:
    1,054
    Luqman
    Jan 2, 2006
  5. Cylix

    Restrict input charaters in textbox

    Cylix, Jul 5, 2007, in forum: Microsoft VB .NET
    Replies:
    5
    Views:
    173
    Cylix
    Jul 6, 2007
Loading...

Share This Page