Sign in to follow this  
Fixxer

C#: Detecting Keypress and handling the event

Recommended Posts

Magius    220
You can handle the KeyPress event of the text box and check whether the KeyChar property of the event arguments is equal to ASCII character 13.

Magius

Share this post


Link to post
Share on other sites
Telastyn    3777
[I am no expert so this might be inaccurate, but]

The Textbox control should trigger the KeyDown event [delegates take an object and an KeyEventArgs]. If KeyEventArgs.KeyCode is Keys.Enter or Keys.Return [they actually look to be different names for the same thing] viola, enter key pressed.

Share this post


Link to post
Share on other sites
Magius    220


private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
MessageBox.Show("Enter key pressed.");
e.Handled = true;
}

e.Handled = false;
}



Hope that helps!

Magius

Share this post


Link to post
Share on other sites
Magius    220
You have to make sure you hook up that event handler to the KeyPress event of the text box. You can do this through the designer or in code. The code to do this would be:



textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);



Magius

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this