Thanks, Bob
Private Sub tbxYear_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) _
Handles tbxYear.KeyPress
MsgBox("e.KeyChar is " & Decimal.op_Implicit(e.KeyChar).ToString)
MsgBox("Keys.Enter is " & Decimal.op_Implicit(Keys.Enter).ToString)
If e.KeyChar.Equals(Keys.Enter) Then 'if Enter key then tab forward
MsgBox("it's an Enter key")
tbxYear.GetNextControl(tbxYear, True)
e.Handled = True
Return
End If
That's because you're comparing two different animals. e.KeyChar is a
Char, and System.Windows.Forms.Keys is an enum, which means that it
maps to an integer type.
You must convert one of them to the same type as the other, such as in:
If AscW(e.KeyChar) = Key.Enter Then
'... do your stuff
Or
If e.KeyChar.Equals(Convert(Keys.Enter).ToChar) Then ...
HTH.
Regards,
Branco.
You should distinguish between keys and chars:
- Not every key creates a char.
- Key combinations can create one single char.
- Different keys/key combinations create the same char. For example, Ctrl+M
creates chr(13) - because M is character #13 in the alphabet. The Enter key
produces the same character.
-> If you want to handle a key, use the KeyDown event.
-> To handle a char, use the KeyPress event.
Therefore, you should decide what is the right event, before. Your question
occurs only because you are mixing keys and chars - as Branco has already
said.
a) Thus, if you want to handle character #13, use
if e.keychar = controlchars.cr
in KeyPress.
b) If you want to handle the Enter key, use
if e.keycode = keys.Enter Then
in KeyDown.
Now, as you see, no conversion is necessary anymore. I'd use version b)
because the user probably should not be able to change the focus using
Ctrl+M.
Armin
But thanks to your responses I am sure I can now code a test which will work
correctly.
Thanks again, Bob
"Armin Zingler" <az.n...@freenet.de> wrote in message
news:O1Mde6y8...@tk2msftngp13.phx.gbl...