473,382 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

KeyPress event and e.KeyChar

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
3 14813
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
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
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Wiktor Zychla | last post by:
in my application I sometimes use KeyPress event. in the event handler I have to check the char the user is trying to type. in NTs everything is correct. however on W98 I've noticed that the...
1
by: Top Gun | last post by:
I am trying to trap on a keypress "+" event and convert it to a tab for a high-speed dataentry form that is done entirely from a 10-key. However, I do NOT want the "+" character to be passed on to...
3
by: Darryn Ross | last post by:
Hi, I am trying to catch the KeyPress event on my datagrid but it isn't working... i have also tried registering the handler with the event like this... dgGLBatch.KeyPress += new...
10
by: Tim Frawley | last post by:
I am attempting to detect a Shift+Tab in the KeyPress event for back navigation on a control that doesn't support this method. Does anyone have any ideas how to compare e.KeyChar to a ShiftTab? ...
2
by: KC | last post by:
I'm trying to build a simple text editor in part of my app. Right now I'm just testing the basics of the KeyPress event handler. The code is basic...real simple. I'm just testing stuff - Private...
3
by: JAG711 | last post by:
I have 2 mdi childforms which are database input forms. Each form has the usual add/delete/edit/save buttons and a few textboxes. I capture the keypress events on the textboxes to allow only...
7
by: Kay | last post by:
Hi all, I want to create a small function to monitor the keystroke. For example, if user key in a comma, I want to replace it with a single quote(etc)... In vb6 I an simply check the KeyAscii...
3
by: windy | last post by:
I got a question about keypress event on textbox: I found that the keypress event doesn't invoked when i press the "." button on alphabetic keyboard, however if i use numberpad's "." button...
3
by: chris52672 | last post by:
If I have text box 1 (Password Enter) and I have text box 2 (Password Reneter). After text has been entered in text box 1 and the enter key is pressed to make text box 2 active (the curser will...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.