By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

KeyPress event and e.KeyChar

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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..
"Baz" <ba***********@yahoo.co.uk> wrote in message
news:44**************************@posting.google.c om...
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.

Nov 20 '05 #2

P: n/a
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #3

P: n/a
* ba***********@yahoo.co.uk (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'.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.