C# KeyEvent doesn't log the enter/return key

Posted by Pieter888 on Stack Overflow See other posts from Stack Overflow or by Pieter888
Published on 2010-03-20T16:35:02Z Indexed on 2010/03/20 16:41 UTC
Read the original article Hit count: 277

Filed under:
|
|
|

Hey all,

I've been making this login form in C# and I wanted to 'submit' all the data as soon as the user either clicks on submit or presses the enter/return key.

I've been testing a bit with KeyEvents but nothing so far worked.

void tbPassword_KeyPress(object sender, KeyPressEventArgs e)
    {
        MessageBox.Show(e.KeyChar.ToString());
    }

The above code was to test if the event even worked in the first place. It works perfectly, when I press 'd' it shows me 'd' when I press '8' it shows me '8' but pressing enter doesn't do anything.

So I though this was because enter isn't really bound to a character but it did show backspace, it worked just fine so it got me confused about why it didn't register my enter key.

So the question is: How do I log the enter/return key? and why doesn't it log the key press right right now like it should?

note: I've put the event in a textbox

tbPassword.KeyPress += new KeyPressEventHandler(tbPassword_KeyPress);

So it fires when the enter button is pressed WHILE the textbox is selected (which is was the whole time of course) maybe that has something to do with the execution of the code.

© Stack Overflow or respective owner

Related posts about c#

Related posts about keyevent