如何在 C# 中禁用 DataGridView 的每个单元格制表位?
How to disable per cell tab stop of DataGridView in C#?
如何在 C# 中禁用 DataGridView 的每个单元格制表位?
如果用户关注 DataGridView 并按下 'Tab',我希望下一个控件会被关注,而不是关注 DataGridView 的下一个单元格。
我该怎么做?
制作AllowUserToAddRows = false
然后
private void ToNextControl(bool Forward)
{
Control c = Parent.GetNextControl(this, Forward);
while (c != null && !c.TabStop) // get next control that is a tabstop
c = Parent.GetNextControl(c, Forward);
if (c != null)
{
//c.Select(); // Not needed for it to work
c.Focus(); // force it to focus
}
}
这将使下一个控件获得焦点:
private void DataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
DataGridView1.Enabled = false;
DataGridView1.GetNextControl(DataGridView1, true).Focus();
DataGridView1.Enabled = true;
e.Handled = true;
}
}
使用 KeyUp 时,datagridview 在放弃焦点之前仍会移动一个单元格。如果你想撤消,你可以添加这行代码:
DataGridView1.CurrentCell = DataGridView1.Rows[DataGridView1.CurrentCell.RowIndex].Cells[DataGridView1.CurrentCell.ColumnIndex - 1];
DataGridView1.Enabled = false;
DataGridView.GetNextControl(DataGridView1, true).Focus();
DataGridView1.Enabled = true;
将 DataGridView.StandardTab
属性 设置为 true
这是 属性 的描述:
"Indicates whether the TAB key moves the focus to the next control in the tab order rather than moving focus to the next cell in the control."
在 VS2012 .NET 4 中,在向表单添加新的数据网格视图后,默认情况下 false
(未在文档中看到默认状态)。
我建议在搜索问题的解决方案时,将对象和所需功能键入 google,然后是 space,然后是文本 msdn。这解决了我大约 40% 的困境。如果这不起作用,请将 msdn 替换为 Whosebug。这解决了另外 50%。最后 10% 是休息和回来的结果。
这是我的做法。
我试了很多次错误才得到正确的代码,因为它有点棘手。
制作一个 class 继承 DataGridView 以制作自定义 DataGridView
编译项目,MyDataGridView 将出现在您的工具箱中。
它的新功能是绕过只读单元格,您可以使用 ENTER 或 TAB 在单元格之间导航
public class MyDataGridView : DataGridView
{
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
if (keyData == Keys.Tab || keyData == Keys.Enter)
{
int index = this.CurrentCell.ColumnIndex;
if (index < this.ColumnCount - 1)
{
if (this.CurrentRow.Cells[index + 1].ReadOnly)
SendKeys.Send("{TAB}");
}
return this.ProcessTabKey(keyData);
}else if(keyData == (Keys.Enter | Keys.Shift) || keyData == (Keys.Tab | Keys.Shift))
{
int index = this.CurrentCell.ColumnIndex;
if (index > 0)
{
if (this.CurrentRow.Cells[index - 1].ReadOnly)
SendKeys.Send("+{TAB}");
}
return this.ProcessLeftKey(keyData);
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
如何在 C# 中禁用 DataGridView 的每个单元格制表位?
如果用户关注 DataGridView 并按下 'Tab',我希望下一个控件会被关注,而不是关注 DataGridView 的下一个单元格。
我该怎么做?
制作AllowUserToAddRows = false
然后
private void ToNextControl(bool Forward)
{
Control c = Parent.GetNextControl(this, Forward);
while (c != null && !c.TabStop) // get next control that is a tabstop
c = Parent.GetNextControl(c, Forward);
if (c != null)
{
//c.Select(); // Not needed for it to work
c.Focus(); // force it to focus
}
}
这将使下一个控件获得焦点:
private void DataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
DataGridView1.Enabled = false;
DataGridView1.GetNextControl(DataGridView1, true).Focus();
DataGridView1.Enabled = true;
e.Handled = true;
}
}
使用 KeyUp 时,datagridview 在放弃焦点之前仍会移动一个单元格。如果你想撤消,你可以添加这行代码:
DataGridView1.CurrentCell = DataGridView1.Rows[DataGridView1.CurrentCell.RowIndex].Cells[DataGridView1.CurrentCell.ColumnIndex - 1];
DataGridView1.Enabled = false;
DataGridView.GetNextControl(DataGridView1, true).Focus();
DataGridView1.Enabled = true;
将 DataGridView.StandardTab
属性 设置为 true
这是 属性 的描述:
"Indicates whether the TAB key moves the focus to the next control in the tab order rather than moving focus to the next cell in the control."
在 VS2012 .NET 4 中,在向表单添加新的数据网格视图后,默认情况下 false
(未在文档中看到默认状态)。
我建议在搜索问题的解决方案时,将对象和所需功能键入 google,然后是 space,然后是文本 msdn。这解决了我大约 40% 的困境。如果这不起作用,请将 msdn 替换为 Whosebug。这解决了另外 50%。最后 10% 是休息和回来的结果。
这是我的做法。 我试了很多次错误才得到正确的代码,因为它有点棘手。 制作一个 class 继承 DataGridView 以制作自定义 DataGridView 编译项目,MyDataGridView 将出现在您的工具箱中。 它的新功能是绕过只读单元格,您可以使用 ENTER 或 TAB 在单元格之间导航
public class MyDataGridView : DataGridView
{
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
if (keyData == Keys.Tab || keyData == Keys.Enter)
{
int index = this.CurrentCell.ColumnIndex;
if (index < this.ColumnCount - 1)
{
if (this.CurrentRow.Cells[index + 1].ReadOnly)
SendKeys.Send("{TAB}");
}
return this.ProcessTabKey(keyData);
}else if(keyData == (Keys.Enter | Keys.Shift) || keyData == (Keys.Tab | Keys.Shift))
{
int index = this.CurrentCell.ColumnIndex;
if (index > 0)
{
if (this.CurrentRow.Cells[index - 1].ReadOnly)
SendKeys.Send("+{TAB}");
}
return this.ProcessLeftKey(keyData);
}
return base.ProcessCmdKey(ref msg, keyData);
}
}