Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

How do I create a keypress event for C#?

 

I want something like.

 

private void MyTextBox_Keypress (object sender, EventArgs e)
{
 if (keypressed == "Enter Key")
    nextControl.Setfocus;
}

"Beer is proof that God loves us and wants us to be happy."

-Benjamin Franklin

  • Leaders
Posted

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

// 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();
}

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...