Event for C# Windows Control

MTSkull

Centurion
Joined
Mar 25, 2003
Messages
151
Location
Boulder, Colorado
How do I create a keypress event for C#?

I want something like.

Code:
private void MyTextBox_Keypress (object sender, EventArgs e)
{
  if (keypressed == "Enter Key")
     nextControl.Setfocus;
}
 
Attach the handler through the IDE (select the text box, go to the properties window, and click the lighting bolt icon, find your event, and select an event handler or double click the event to create a new handler). The code for the handler should probably look something like this (handling certain keys for certain controls in certain versions of .Net can be a pain, though):
C#:
// I would prefer KeyDown over KeyPress because it works with keys instead of the characters that they represent
private void MyTextBox_KeyDown(object sender, KeyEventArgs e) {
    if(e.KeyCode == Keys.Enter)
        nextControl.Focus();
}
 
Back
Top