Jump to content
  • Advertisement
Sign in to follow this  
Fixxer

C#: Detecting Keypress and handling the event

This topic is 4706 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

Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!