KeyPress event and e.KeyChar

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.
 
R

Rigga

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..
 
U

unknown

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.
 
H

Herfried K. Wagner [MVP]

* (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'.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top