Restricting Keystrokes in VB .Net

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

H

In VB6 I used to trap by keycode on the keydown event and if the keystroke didn't fit into my criteria I would set the value of the keycode to 0 which would invalidate the keystroke. I noticed that the properties are readonly in .Net. How would I do the same as I used to in 6? I am doing the time honoured only allowing of numbers in a text box and don't seem to be getting right.

Thank you.
 
KeyDown and check out the values of the 'e' the EventArgs..
Robin said:
HI

In VB6 I used to trap by keycode on the keydown event and if the keystroke
didn't fit into my criteria I would set the value of the keycode to 0 which
would invalidate the keystroke. I noticed that the properties are readonly
in .Net. How would I do the same as I used to in 6? I am doing the time
honoured only allowing of numbers in a text box and don't seem to be getting
right.
 
Set the "handled" property to true to indicate that the event has been
handled.

Lloyd Sheen

Robin said:
Yes I know that I can trap the values but I cannot simply assign a 0 and
override what the user typed as per version 6. The e.keycode value is read
only. If I want to change it after it has been typed I shall have to do
something different.
 
Perhaps I am not being clear enough. If I say only want to allow a keyvalue = to the numeric set and somebody types a "G" which is 71 even if I set the handled property to true I still see a "G" in the textbox. I don't want the "G" to be there. I want to trap and disallow invalid keystrokes so that they never make it to the screen

Thanks
 
I understand what you are trying to do. We had lots of text boxes with
handlers in VB6 and the set the keyvalue = 0 was a common piece of code.

I am looking for code in dot.net that does this , I know I translated some
of our current code so it is around. Just cannot promiss when it will show
up. I am pretty certain it had to do with the "handled" property though.

Lloyd
Robin said:
Perhaps I am not being clear enough. If I say only want to allow a
keyvalue = to the numeric set and somebody types a "G" which is 71 even if I
set the handled property to true I still see a "G" in the textbox. I don't
want the "G" to be there. I want to trap and disallow invalid keystrokes so
that they never make it to the screen.
 
Put the code in the following (with your code)
What the below does is stop all keyboard activity so just add whatever you
want let through.
You will not be able to modify what is entered only stop what you deem
invalid.


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


Lloyd

Robin said:
Perhaps I am not being clear enough. If I say only want to allow a
keyvalue = to the numeric set and somebody types a "G" which is 71 even if I
set the handled property to true I still see a "G" in the textbox. I don't
want the "G" to be there. I want to trap and disallow invalid keystrokes so
that they never make it to the screen.
 
Back
Top