473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why Doesn't 13 Equals 13?

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


Nov 27 '05 #1
3 3337
eBob.com wrote:
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.

Nov 27 '05 #2
"eBob.com" <fa******@totallybogus.com> schrieb
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

Nov 27 '05 #3
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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:O1**************@tk2msftngp13.phx.gbl...
"eBob.com" <fa******@totallybogus.com> schrieb
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

Nov 27 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sorincom | last post by:
Hello, I have this class: public class TestClass { private string testData = ""; public string TestData { get { return this.testData; }
17
by: Zeng | last post by:
I'm trying to comparing 2 objects (pointer to object) to see if they are the "same" as each other. Here is what the definition of being the "same" object type for both objects, object 1, ...
3
by: Frank Wisniewski | last post by:
Is this suppose to work like this: I have a class called foo in which I tried to override equals and return different answers based on what was passed in. public class foo { private string...
6
by: sea# | last post by:
I'm trying to use a combo box on a windows form which will change its display on event of changing combo selection. This is the method I wrote: Wrote this code: private void...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
5
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
7
by: =?Utf-8?B?QWxleCBDb2hu?= | last post by:
In C++, there is an easy technique to provide an overloaded Equals() method. A straightforward translation to C# causes a stack overflow. Why does b.Equals(ba) in the snippet below not understand...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.