program to show ascii code used?

defiance

Member
Joined
May 8, 2011
Messages
13
Programming Experience
Beginner
dear all, i am a beginner so please bear with my unfamiliarity with terms that you guys may used here..

but I am trying out VB.net and is trying to make a program to
show the ascii code for every key press I made..
here is the coding i used..

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As _ System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

TextBox2.Text = e.KeyValue
End Sub

this one seems to work fine, but I noticed that regardless of whether
I use "A" or "a", the ascii code shown is always 65.
last i checked small letters have their own ascii code, so "a"
should show a ascii of 97, not 65..

any help anyone can provide here will be appreciated..
thank you
 
You should probably follow the Blog link in my signature and check out my post on Keyboard Events for more info but, in short, the KeyDown and KeyUp events relate to keys on the keyboard while the KeyPress event relates to characters. If you're interested in characters then you should be handling KeyPress.
 
hmm.. i tried using button click though.

Label1.Text = Asc(TextBox1.Text)

that seem to work fine.. but it doesn't work for keypress or keydown..
guess i can't complain..

anyway, thanz for your reply, jmcil
:)
 
hmm.. i tried using button click though.

Label1.Text = Asc(TextBox1.Text)

that seem to work fine.. but it doesn't work for keypress or keydown..
guess i can't complain..

anyway, thanz for your reply, jmcil
:)

That is not how you do it. As I said, read my blog post on the subject and you can learn how to use the KeyPress event properly.
 
Here's an example with KeyPress:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    TextBox2.Text = Asc(e.KeyChar)
End Sub
 
hmm..
i am pretty sure i tried that before..
i could be wrong..

I'll try it out tomoro..
thanz for the tip Josh
Peace
:)
 
I tried that code on my computer and got different ASCII codes for different cases, so I know it works at least on my computer.
 
Every ascii value in fact is an integer value because the processor only deals with numbers so when the user press the button the char will be converted to integer and the integer value will be returned as string value.
 
Every ascii value in fact is an integer value because the processor only deals with numbers
We are all aware of that already.
when the user press the button the char will be converted to integer and the integer value will be returned as string value.
The code you provided is not going to do that. Well, it will but that Integer is not going to be the ASCII code you want and it will actually throw an exception in most cases.

It's very, very simple. The proper way to do this is to handle the KeyPress event and use the e.KeyChar property, as I have explained in my blog post. You can then use the Asc function to get the ASCII code for the character or use Convert.ToInt32 to get the Unicode value. ASCII and Unicode values will be the same for the most common characters.
 
Did this work for anyone?

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    TextBox2.Text = Asc(e.KeyChar)
End Sub
 
For who asked for Ascii code
I'm telling you that it's going to work try me.

You obviously haven't even tried it yourself or you'd know that it doesn't work. Your code is going to parse the Text of the TextBox. Using your code, if you type 12345678 into the TextBox then you get a message displaying 12345678, which is obviously not a valid ASCII code. If you type 1 into the TextBox then you get a message displaying 1, even though the ASCII code for the character '1' is 49. If you type Hello into the TextBox then an exception is thrown. Your code does not work. It does not do what has been asked for. Please stop saying that it does and please stop asking for email addresses for private conversations.
 
JOSH, i tried your code.

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As _System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Label1.Text = Asc(e.KeyChar)
End Sub

It worked perfectly..
thank you very much..
 
Back
Top