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).
我有一个DataGridView,我在使用Tab键导航时手动管理聚焦单元格。例如,当最后一行中的第一个单元格是可以在DataGridView中导航的最后一个单元格时,我希望在按Tab键时焦点转到下一个可聚焦控件(一个按钮)。
SendKeys.Send("{TAB}") will not work - the focus will go to the second cell on the last row
SendKeys.Send(“{TAB}”)将无效 - 焦点将转到最后一行的第二个单元格
5 个解决方案
#2
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.
如果将StandardTab属性设置为True,则Tab键的行为将从移动到下一个网格单元格变为移动到窗体上的下一个控件。这可能是你想要的。
If you want to control which grid cell/column/row gets focused then you can handle the ProcessDialogKey
and ProcessDataGridViewKey
events in your code.
如果要控制哪个网格单元/列/行聚焦,则可以在代码中处理ProcessDialogKey和ProcessDataGridViewKey事件。
#3
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.
像Rune Grimstad所说,如果您想要除默认或StandardTab提供的选项之外的任何选项卡行为,您将必须实现自己的选项卡行为。
Create a new Class that inherits from DataGridView.
创建一个继承自DataGridView的新类。
protected override bool ProcessDialogKey(Keys keyData)
{
case Keys.Tab | Keys.Shift:
// implement your logic here.
break;
case Keys.Tab:
// implement your logic here.
break;
}
#4
Control nextControl = this.dataGridView1.Parent.GetNextControl(this.dataGridView1, true);
Control prevControl = this.dataGridView1.Parent.GetNextControl(this.dataGridView1, false);
#5
You can just simply use the DataGridView's StandardTab property.
您只需使用DataGridView的StandardTab属性即可。
dataGridView1.StandardTab = true;
Note that user can do this with non StandardTab with CTRL +TAB combination, StandardTab reverses this behaviour.
请注意,用户可以使用带有CTRL + TAB组合的非StandardTab执行此操作,StandardTab会反转此行为。
#1
Have you tried the SelectNextControl method?
你尝试过SelectNextControl方法吗?
#2
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.
如果将StandardTab属性设置为True,则Tab键的行为将从移动到下一个网格单元格变为移动到窗体上的下一个控件。这可能是你想要的。
If you want to control which grid cell/column/row gets focused then you can handle the ProcessDialogKey
and ProcessDataGridViewKey
events in your code.
如果要控制哪个网格单元/列/行聚焦,则可以在代码中处理ProcessDialogKey和ProcessDataGridViewKey事件。
#3
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.
像Rune Grimstad所说,如果您想要除默认或StandardTab提供的选项之外的任何选项卡行为,您将必须实现自己的选项卡行为。
Create a new Class that inherits from DataGridView.
创建一个继承自DataGridView的新类。
protected override bool ProcessDialogKey(Keys keyData)
{
case Keys.Tab | Keys.Shift:
// implement your logic here.
break;
case Keys.Tab:
// implement your logic here.
break;
}
#4
Control nextControl = this.dataGridView1.Parent.GetNextControl(this.dataGridView1, true);
Control prevControl = this.dataGridView1.Parent.GetNextControl(this.dataGridView1, false);
#5
You can just simply use the DataGridView's StandardTab property.
您只需使用DataGridView的StandardTab属性即可。
dataGridView1.StandardTab = true;
Note that user can do this with non StandardTab with CTRL +TAB combination, StandardTab reverses this behaviour.
请注意,用户可以使用带有CTRL + TAB组合的非StandardTab执行此操作,StandardTab会反转此行为。