Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having the "enter" key act as the "tab" key; avoiding focus

Status
Not open for further replies.

DayLaborer

Programmer
Jan 3, 2006
347
0
0
US
The user of my app wants the "enter" key to function like the "tab" key, e.g. to navigate from field to field. I found a good idea here:

The problem is that although clicking the tab key itself will take me from textbox to textbox, when I use the method mentioned above, it stops on Labels, too.

I tried something like this:
Code:
        private void General_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Control nextControl = this.GetNextControl(ActiveControl, true);
                if (nextControl != null)
                {
                    e.Handled = true;
                    if (!(nextControl is Label))
                    {
                        nextControl.Focus();
                    }
                    else //the next control is currently a label
                    {
                        nextControl = this.GetNextControl(ActiveControl, true);
                        while (nextControl != null)
                        {
                            nextControl.Focus();
                            if (!(nextControl is Label))
                            {
                                break;
                            }
                            nextControl = this.GetNextControl(ActiveControl, true);
                        }
                    }
                }
            }
        }
...but it didn't work.

How can I get around this?

Thanks,
Eliezer
 
Try this...

Enable the form's KeyPreview property, and use the SendKeys class to send the TAB command from the Form_KeyDown event. I believe this class is basically a wrapper for the Win32 SendKeys(), thus follows the same rules on sending key strokes.
Code:
  private void Form1_KeyDown(object sender, KeyEventArgs e)
  {
    if (e.KeyCode == Keys.Enter)
    {
      SendKeys.Send("{TAB}");
    }
  }
However, this may not work for certain complex controls such as the DataGridView control.

hth [wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top