473,324 Members | 2,254 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,324 software developers and data experts.

Only allow numbers and backspace/delete on KeyDown?

Hi

How do I in a textbox only allow users to type in numbers and comma and dot
?

they should of course be able to delete and backspace the stuff they type in
too..

I figured keyDown would b a good place

best regards
/Lars Netzel
Nov 21 '05 #1
7 11890
Lars,

You might want to think about using the textbox's Validating event to test
for valid input when the user attempts to change the focus away from the
textbox.

The Validating event is way more straightforward than attempting to
determine valid input while the user is typing, backspacing, deleting, etc.

Kerry Moorman
"Lars Netzel" wrote:
Hi

How do I in a textbox only allow users to type in numbers and comma and dot
?

they should of course be able to delete and backspace the stuff they type in
too..

I figured keyDown would b a good place

best regards
/Lars Netzel

Nov 21 '05 #2
"Lars Netzel" <ui****@adf.se> wrote in message news:OH**************@TK2MSFTNGP12.phx.gbl...
Hi

How do I in a textbox only allow users to type in numbers and comma and dot
?

they should of course be able to delete and backspace the stuff they type in
too..

I figured keyDown would b a good place

best regards
/Lars Netzel


Here is something I just wrote yesterday to do the same thing. It uses KeyPress instead of KeyDown, but I think it gives the same
result.

Private Sub NumbersOnly(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtIdleTime.KeyPress,
txtUnloadInterval.KeyPress

If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or Asc(e.KeyChar) = 8) Then

e.Handled = True

End If

End Sub

I hope this helps.

--

Al Reid
Nov 21 '05 #3
Lars,

I figured keyDown would b a good place

The keyDown is the worst place for this, because than is not yet know which
key it was. Thas is with KeyUp.

:-)

Know that only checking will typing is not such a good idea because there
can be pasted things in a textbox, while your client will not be happy with
you while doing it only while validating.

\\\
Private Sub textbox1_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyUp
If e.KeyValue <> 8 Then
If Not IsNumeric(TextBox1.Text) Then
If TextBox1.Text.Length > 0 Then
MessageBox.Show("Only numeric is allowed")
TextBox1.SelectionStart = TextBox1.Text.Length - 1
TextBox1.SelectionLength = 1
End If
End If
End If
End Sub
Private Sub TextBox1_Validating(ByVal sender _
As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
If TextBox1.Text.Length > 0 Then
If Not IsNumeric(TextBox1.Text) Then
MessageBox.Show("There was an error pasting")
TextBox1.Focus()
e.Cancel = True
End If
End If
End Sub
///
I hope this helps a little bit?

Cor
Nov 21 '05 #4
Cor,

Why do you say that?

Kerry Moorman

"Cor Ligthert [MVP]" wrote:

[snip]
while your client will not be happy with
you while doing it only while validating.

Cor

Nov 21 '05 #5
Kerry,
[snip]
while your client will not be happy with
you while doing it only while validating.

Did you ever typed in a number from 20 characters and then get a message
that it is not well. (And watch the word 'only').

However, it is just my personal opinion.

Cor
Nov 21 '05 #6
Cor,

Thanks for your reply. Yes, I noticed that you were stressing 'only' and I
can see that under certain circumstances it might be nice to validate input
while the user is typing and also in the Validating event.

However, in general I like to test for invalid input in the control's
Validating event and then use an ErrorProvider instead of a message box to
inform the user of a problem.

Kerry Moorman
"Cor Ligthert [MVP]" wrote:
Kerry,
[snip]
while your client will not be happy with
you while doing it only while validating.

Did you ever typed in a number from 20 characters and then get a message
that it is not well. (And watch the word 'only').

However, it is just my personal opinion.

Cor

Nov 21 '05 #7
Thank you..

I went for the IsNumeric check instead on the Closing event of the Window...
that works well for me..

/Lars
"Kerry Moorman" <Ke**********@discussions.microsoft.com> wrote in message
news:76**********************************@microsof t.com...
Cor,

Thanks for your reply. Yes, I noticed that you were stressing 'only' and
I
can see that under certain circumstances it might be nice to validate
input
while the user is typing and also in the Validating event.

However, in general I like to test for invalid input in the control's
Validating event and then use an ErrorProvider instead of a message box to
inform the user of a problem.

Kerry Moorman
"Cor Ligthert [MVP]" wrote:
Kerry,
>> [snip]
>> while your client will not be happy with
>> you while doing it only while validating.
>>

Did you ever typed in a number from 20 characters and then get a message
that it is not well. (And watch the word 'only').

However, it is just my personal opinion.

Cor

Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gerry Viator | last post by:
Hi all, I would like only a year in a range from 1950 to 2040 to be alowed in a textbox control. This doesn't seam to be working... Private Sub txtboxYear_KeyDown(ByVal sender As Object,...
9
by: Leroy | last post by:
I have this function: function ValidateIntKey() { var Key; var CharKey; Key = event.keyCode; alert(Key); //CharKey = String.charCodeAt(Key);
17
by: Julia Briggs | last post by:
Are there any gotchas using if (event.keyCode==8)? I understand that to represent backspace, but it doesn't work. I am running Windows XP, using a typical keyboard - but no luck in detecting...
3
by: Bisley | last post by:
I wish to restrict my textbox to only accepting numbers I have looked the textbox KeyDown event and can inspect KeyEventArgs parameter and interogate which key has been pressed, but I can't work...
6
by: Woody Splawn | last post by:
We have access to Infragistics etc., but we would like to make use of a regular VS textbox so that it accepts only numbers. We have discovered that if you bind an integer field to a textbox, for...
10
by: Tosch | last post by:
I have a combobox where a user can select a zoom factor or enter a zoom factor. I tried to limit entry into the combobox to numbers only by catching the keydown event and setting e.handled = true...
5
by: PJ | last post by:
I've been using this function to limit a text input to numbers only with success on an old site. function checkForInt(evt) { var charCode = ( evt.which ) ? evt.which : event.keyCode; return (...
5
by: Bill | last post by:
Hello, Could anyone post some simple code or advise me on how I can display the SSN number like *****7890 in a text box, even thought the user entered 1234567890, and the value of the variable...
6
by: Avi G | last post by:
Hi, what is the code to limit a textbox that will accept only text and not number private void button1_Click(object sender, EventArgs e) { if (textBox1.Text = "numbers") { // do something }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.