C#gridview自动将焦点设置为下一个单元格
我正在编写一个Winforms应用程序,用户可以在gridview中输入十六进制值,但在一个单元格中最多只能输入FF。我想让用户在不按enter或tab键的情况下输入长值,如果用户键入FFFF,该值应转到gridview的两个单元格。我试图通过计算下面的2个键,自动将焦点设置到下一个单元格 我的代码不起作用,我不确定我的方法是否正确。如果我错了,请纠正我C#gridview自动将焦点设置为下一个单元格,c#,C#,我正在编写一个Winforms应用程序,用户可以在gridview中输入十六进制值,但在一个单元格中最多只能输入FF。我想让用户在不按enter或tab键的情况下输入长值,如果用户键入FFFF,该值应转到gridview的两个单元格。我试图通过计算下面的2个键,自动将焦点设置到下一个单元格 我的代码不起作用,我不确定我的方法是否正确。如果我错了,请纠正我 private void hexGridView1_EditingControlShowing(object sender, DataGrid
private void hexGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress += new KeyPressEventHandler(CheckKey);
}
private void CheckKey(object sender, KeyPressEventArgs e)
{
e.KeyChar = char.ToUpper(e.KeyChar);
char c = e.KeyChar;
if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || validForAll.Contains((Keys)e.KeyChar)))
{
e.Handled = true;
}
else
{
if (++count > 2) // or something same
{
count = 0;
int r = hexGridView1.CurrentCell.RowIndex;
int ci = hexGridView1.CurrentCell.ColumnIndex;
++r; //just for testing
hexGridView1.CurrentCell = hexGridView1[r, ci];
hexGridView1.CurrentCell.Selected = true;
hexGridView1.BeginEdit(true); //require ?
}
}
}
private void hexGridView1_EditingControlShowing(对象发送方,DataGridViewEditingControlShowingEventArgs e)
{
e、 Control.KeyPress+=新的KeyPressEventHandler(CheckKey);
}
私有无效检查密钥(对象发送方,KeyPressEventArgs e)
{
e、 KeyChar=char.ToUpper(即KeyChar);
char c=e.KeyChar;
如果(!((c>='0'&&c='A'&&c2)//或类似的东西
{
计数=0;
int r=hexGridView1.CurrentCell.RowIndex;
int ci=hexGridView1.CurrentCell.ColumnIndex;
++r、 //只是为了测试
hexGridView1.CurrentCell=hexGridView1[r,ci];
hexGridView1.CurrentCell.Selected=true;
hexGridView1.BeginEdit(true);//需要吗?
}
}
}
下面的代码修复了我的问题,而不是使用计数使用功能
if(hexGridView1.CurrentCell.EditedFormattedValue.ToString().Length == 2)
{
int iColumn = hexGridView1.CurrentCell.ColumnIndex;
int iRow = hexGridView1.CurrentCell.RowIndex;
if (iColumn == hexGridView1.ColumnCount - 1)
{
if (hexGridView1.RowCount > (iRow + 1))
{
hexGridView1.CurrentCell = hexGridView1[0, iRow + 1];
}
else
{
//focus next control
}
}
else
hexGridView1.CurrentCell = hexGridView1[iColumn + 1, iRow];
}