Why Doesn't 13 Equals 13?

E

eBob.com

I'm trying to treat the Enter key in a textbox as the Tab key. Thanks to a
post which Google came up with I have coded a technique using GetNextControl
(see below). But it doesn't work. It seems to not work because the If
statement checking for the Enter key fails. So I added the two MsgBox
statements to display the value of the entered key and the Enter key. Both
display as 13 when I enter the Enter key and yet the code fails - I never
see the message box saying "it's an Enter key" and no tabbing action occurs.
I'm sure it's some really dumb mistake but I do not see it. Any assistance
will be much appreciated.

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
 
B

Branco Medeiros

eBob.com said:
I'm trying to treat the Enter key in a textbox as the Tab key. Thanks to a
post which Google came up with I have coded a technique using GetNextControl
(see below). But it doesn't work. It seems to not work because the If
statement checking for the Enter key fails. So I added the two MsgBox
statements to display the value of the entered key and the Enter key. Both
display as 13 when I enter the Enter key and yet the code fails - I never
see the message box saying "it's an Enter key" and no tabbing action occurs.
I'm sure it's some really dumb mistake but I do not see it. Any assistance
will be much appreciated.

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.
 
A

Armin Zingler

eBob.com said:
I'm trying to treat the Enter key in a textbox as the Tab key.

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
 
E

eBob.com

Armin and Branco, Thanks very much for your very helpful responses. At
first I kicked myself for not having Option Strict On. But I added Option
Strict On and I still don't get a compile time error message. I don't
understand that.

But thanks to your responses I am sure I can now code a test which will work
correctly.

Thanks again, Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top