KeyPress event and e.KeyChar

  • Thread starter Thread starter Baz
  • Start date Start date
B

Baz

Hi All,

In VB6, I had text boxes which, in I wanted to allow numeric data
only. To do this, I put some code in the KeyPress event of the
control, and if the key that was entered was outside the range of 0-9,
then I reset the KeyAscii prperty to 0. Thus:

Private Sub tTransactionAmount_KeyPress(KeyAscii As Integer)
If Keyascii < 48 Or Keyascii > 57 Then Keyascii = 0
End Sub

How can I get the same effect in VB.NET? I tried doing something
similar in the KeyPress event, but the e.KeyChar value is read-only,
so I can't override it like I do the KeyAscii value in VB6.

Any thoughts?

Thanks,

Baz.
 
Hi Baz,

Use the Handled item of the KeyPressedEventsArg..

as in e.Handled = True

setting e.handled to true when the user presses a non numeric key, the
textbox will ignore it..
 
Hi Rigga,

Thanks a lot for that; it worked a treat. As you can tell, I'm a bit of
a novice on .NET!

Baz.
 
* (e-mail address removed) (Baz) scripsit:
In VB6, I had text boxes which, in I wanted to allow numeric data
only. To do this, I put some code in the KeyPress event of the
control, and if the key that was entered was outside the range of 0-9,
then I reset the KeyAscii prperty to 0. Thus:

Private Sub tTransactionAmount_KeyPress(KeyAscii As Integer)
If Keyascii < 48 Or Keyascii > 57 Then Keyascii = 0
End Sub

How can I get the same effect in VB.NET? I tried doing something
similar in the KeyPress event, but the e.KeyChar value is read-only,
so I can't override it like I do the KeyAscii value in VB6.

Just set 'e.Handled' to 'True'.
 
Back
Top