我正在编写一个Winforms应用程序,用户可以在gridview中输入十六进制值,但在一个单元格中只能输入FF。我想让用户输入long值而不按Enter或tab,如果用户键入FFFF,则值应该转到gridview的两个单元格。我想通过计算下面的2个键来自动将焦点设置到下一个单元格。
我的代码无效我不确定我的approch是否正确。如果我错了,请纠正我。
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 ?
}
}
}
答案 0 :(得分:0)
下面的代码修复了我使用Count use函数的问题。
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];
}