0
votes

I have set the "ENTER" key as a "TAB" key in windows form.

following is the code

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        KeyEventArgs e = new KeyEventArgs(keyData);
        if (e.KeyCode == Keys.Enter)
        {
            SendKeys.Send("{TAB}");
        }
        if (e.KeyCode == Keys.Escape)
        {
            this.Close();
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

its working perfectly with all controls but when I press ENTER on any combobox then it takes two tab indexes. eg.

  1. textbox1
  2. combobox
  3. textbox2
  4. textbox3
  5. textbox4

when i press ENTER to leave combobox then the cursor(FOCUS) is directly comes to the textbox 3 i want that focus to the next control of combobox , i.e, textbox 2

please help me

thanks in advance..!

2

2 Answers

1
votes

Control.TabIndex property decides that which order the control should get focused when a Tab key is pressed.

You should set TabIndex properties of controls in a order that you would like them to focus based on Tab key press.

That said, you shouldn't use SendKeys.Send("{TAB}") method for simulating a Tab key press. You should use Control.SelectNextControl method for this purpose.

1
votes

Just return true; after you send the Tab:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        KeyEventArgs e = new KeyEventArgs(keyData);
        if (e.KeyCode == Keys.Enter)
        {
            SendKeys.Send("{TAB}");
            return true;
        }
        if (e.KeyCode == Keys.Escape)
        {
            this.Close();
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }