【发布时间】:2014-08-02 06:48:35
【问题描述】:
我期待的是当我完成编辑单元格并单击输入时,光标将聚焦到指定的单元格。
在我的表单中,我希望光标依次聚焦在单元格列索引 5、2、3 上。 稍后下一行列索引将是 5。
但是 PreviewKeyDown 事件 ID 处理了两次。
所以我通过了我想要的第二步,结果得到了一个错误。
这是我迄今为止尝试过的实现:
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtCell = e.Control as TextBox;
if (txtCell != null)
{
txtCell.PreviewKeyDown += new PreviewKeyDownEventHandler(txtCell_PreviewKeyDown);
txtCell.PreviewKeyDown += new PreviewKeyDownEventHandler(txtCell_PreviewKeyDown);
txtCell.KeyDown += new KeyEventHandler(txtCell_KeyDown);
txtCell.KeyDown += new KeyEventHandler(txtCell_KeyDown);
}
}
void txtCell_KeyDown(object sender, KeyEventArgs e)
{
try
{
TextBox tCell = (TextBox)sender;
if (dataGridView1.CurrentCell.ColumnIndex == 5)
{
if (e.KeyCode == Keys.Return)
{
e.Handled = true;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void txtCell_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
try
{
if (e.KeyCode == Keys.Return)
{
int iColumn = dataGridView1.CurrentCell.ColumnIndex;
int iRow = dataGridView1.CurrentCell.RowIndex;
if (iColumn == 5)
{
dataGridView1.Focus();
dataGridView1.CurrentCell = dataGridView1[2, iRow];
//-----I want to test the focus across the cell or not.
dataGridView1.CurrentCell.Value = "123";
}
if (iColumn == 2)
{
dataGridView1.Focus();
dataGridView1.CurrentCell = dataGridView1[3, iRow];
//-----I want to test the focus across the cell or not.
dataGridView1.CurrentCell.Value = "123";
}
if (iColumn == 3)
{
dataGridView1.Focus();
dataGridView1.CurrentCell = dataGridView1[5, iRow + 1];
//-----I want to test the focus across the cell or not.
dataGridView1.CurrentCell.Value = "123";
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
【问题讨论】:
-
这是 wpf 吗?如果是这样,您需要将事件标记为已处理。
-
是的,它是窗体应用程序。需要什么?我只是一个初学者。你能再解释一下吗。感谢您的帮助。
-
这是winform还是WPF?
-
这是一个winform。不过谢谢。我现在解决了。
标签: c# winforms events datagridview