C# Textbox - Enter to go to next control

I was building a Windows Form Application in Visual C# Express 2010 and had a problem that needed a lot of searching to figure out.  I could not get a value to "enter" and tab to the next textbox when I hit the Enter key.


After much trial and error (most of which would work but I still got a bell when I hit Enter) I found this.


Enter this as the KeyDown Event. Note that KeyPress uses KeyChar return types.

private void textbox1_KeyDown(object sender, KeyEventArgs e)
{
     //checks for Enter, suppresses it and sends a tab
     if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.None)
    {
        e.SuppressKeyPress = true;  //eliminates the enter key *BELL*
        SendKeys.Send(
"{TAB}"); //changes focus to the next control
    }
}

Comments

Popular posts from this blog

Maintenance of a 30 Year Old CNC Milling Machine

DIY CNC Milling Machine Control Panel for EMC2

Power Supply for CNC