Private Sub textbox_KeyPress(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles textbox.KeyPress
If Not Char.IsNumber(e.KeyChar) And _
Not e.KeyChar = Chr(Keys.Back) And _
Not e.KeyChar = Chr(Keys.Subtract) Then
e.Handled = True
End If
End Sub
This works perfectly for the number keys and the backspace key but not for
the minus(subtract) key. I can replace Keys.Subtract for any other key on
the keyboard and it works but I can`t get Keys.Subtract to work. Any ideas?
Thanks
Rob
> This works perfectly for the number keys and the backspace key but not for
> the minus(subtract) key. I can replace Keys.Subtract for any other key on
> the keyboard and it works but I can`t get Keys.Subtract to work. Any
> ideas?
Does this work as you expected:
If Not Char.IsNumber(e.KeyChar) AndAlso _
Not e.KeyChar = Chr(Keys.Back) AndAlso _
Not e.KeyChar = "-" Then
e.Handled = True
End If
-Teemu
Thanks
Rob
"Teemu" <tsi...@hotmail.com> wrote in message
news:pR1dm.30905$vi5....@uutiset.elisa.fi...
I'm not totally sure, but if I remember correctly, these Keys.Subtract,
Keys.Add and others should be used only in KeyDown and KeyUp events to look
up e.KeyCode.
KeyPress event returns just a character and KeyUp and KeyDown events return
a key code for example F-buttons and other special buttons.
-Teemu
Rob
"Teemu" <tsi...@hotmail.com> wrote in message
news:%Bcdm.30999$vi5....@uutiset.elisa.fi...
Those Keys enumerations which have same integer as character's ASCII code
(letters, backspace and a few others) will "accidentally" work although it
is much easier to use for example "h" or "&" in KeyPress event.
-Teemu
See http://msdn.microsoft.com/en-us/library/ms646267(VS.85).aspx and the
explanation of virtual key codes and character messages.
Armin