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

Only allow numbers and backspace/delete on KeyDown?

P: n/a
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
Share this Question
Share on Google+
7 Replies


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

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

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.