我期待的是当我完成编辑单元格并单击输入时,光标将聚焦到指定的单元格。PreviewKeyDown事件触发两次
在我的表单中,我期望游标会依次关注单元格列索引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吗?如果是这样,您需要将事件标记为已处理。 – paqogomez
是的,它是窗口窗体应用程序。 需要什么? 我只是一个初学者。 你能解释一下吗? 感谢您的帮助。 –
这是一个winform还是WPF? –