Thursday, May 5, 2011

Focus next control when pressing 'TAB' key

I have a DataGridView where I manage manually the focused cells when navigating with TAB key. For example, when the first cell from the last row is the last cell than can be navigated in the DataGridView I want that when pressing 'TAB' the focus to go on the next focusable control (a button).

SendKeys.Send("{TAB}") will not work - the focus will go to the second cell on the last row

From stackoverflow
  • Have you tried the SelectNextControl method?

  • Control nextControl = this.dataGridView1.Parent.GetNextControl(this.dataGridView1, true);
    Control prevControl = this.dataGridView1.Parent.GetNextControl(this.dataGridView1, false);
  • If you set the StandardTab property to True then the behavior of the Tab key will change from moving to the next grid cell to moving to the next control on the form. This may be what you want.

    If you want to control which grid cell/column/row gets focused then you can handle the ProcessDialogKey and ProcessDataGridViewKey events in your code.

  • Like Rune Grimstad said, if you want any tab behavior other than the one provided by default or StandardTab, you will have to implement your own tab behavior.

    Create a new Class that inherits from DataGridView.

    protected override bool ProcessDialogKey(Keys keyData)
        case Keys.Tab | Keys.Shift:
            // implement your logic here.
        case Keys.Tab:
            // implement your logic here.


Post a Comment