Only allow numbers and backspace/delete on KeyDown?

  • Thread starter Thread starter Lars Netzel
  • Start date Start date
L

Lars Netzel

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
 
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 said:
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.
 
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
 
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
 
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 said:
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
 
Thank you..

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

/Lars


Kerry Moorman said:
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 said:
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
 
Back
Top