Sign in to follow this  

C#: Detecting Keypress and handling the event

This topic is 4393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
[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


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

This topic is 4393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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