Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Strange problem with the minus(subtract) key

15 views
Skip to first unread message

roidy

unread,
Aug 1, 2009, 2:27:50 PM8/1/09
to
I have a text box and I need to limit the input to just numbers, backspace
and the minus(subtract) key. I`ve set up a KeyPress event for the text box
and am using the following code:-

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

Teemu

unread,
Aug 1, 2009, 4:47:17 PM8/1/09
to

"roidy" <r...@nnnnnn.com> kirjoitti viestiss�
news:5P%cm.128425$ay4....@newsfe27.ams2...

> 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


roidy

unread,
Aug 1, 2009, 4:55:45 PM8/1/09
to
That worked perfectly, it never occurred to me to just use "-", seems
strange that Keys.Subtract doesn't work through.

Thanks
Rob


"Teemu" <tsi...@hotmail.com> wrote in message
news:pR1dm.30905$vi5....@uutiset.elisa.fi...

Teemu

unread,
Aug 2, 2009, 5:01:47 AM8/2/09
to

"roidy" <r...@nnnnnn.com> kirjoitti viestiss�
news:RZ1dm.28215$Th1....@newsfe02.ams2...

> That worked perfectly, it never occurred to me to just use "-", seems
> strange that Keys.Subtract doesn't work through.

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

roidy

unread,
Aug 2, 2009, 10:39:38 AM8/2/09
to
Yep I just tested it and your right, but it seems kind of strange that
Keys.Back and most other keys can be used in a KeyPress event but not
Keys.Subtract. Oh well.

Rob


"Teemu" <tsi...@hotmail.com> wrote in message

news:%Bcdm.30999$vi5....@uutiset.elisa.fi...

Teemu

unread,
Aug 2, 2009, 12:53:15 PM8/2/09
to

"roidy" <r...@nnnnnn.com> kirjoitti viestiss�
news:bzhdm.118647$Ib5....@newsfe24.ams2...

> Yep I just tested it and your right, but it seems kind of strange that
> Keys.Back and most other keys can be used in a KeyPress event but not
> Keys.Subtract. Oh well.

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

Armin Zingler

unread,
Aug 2, 2009, 2:49:24 PM8/2/09
to
roidy schrieb:

> Yep I just tested it and your right, but it seems kind of strange that
> Keys.Back and most other keys can be used in a KeyPress event but not
> Keys.Subtract. Oh well.


See http://msdn.microsoft.com/en-us/library/ms646267(VS.85).aspx and the
explanation of virtual key codes and character messages.


Armin

0 new messages