如果选项卡索引位于datagridview的最后一行和最后一列,如果我此时按tab键,它将移至datagridview的第一行和第一列,而不是下一个control(button)。有人可以建议我如何停止最后一行的选项卡索引编制并移至下一个控件。我尝试了这段代码。
private void dgCoreRoutes_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
if (dgCoreRoutes.CurrentCell.RowIndex == dgCoreRoutes.Rows.Count-1)
{
dgCoreRoutes.TabStop = true;
}
if (dgCoreRoutes.CurrentCell.ReadOnly)
{
dgCoreRoutes.CurrentCell = GetCoreRoutesGridNextCell(dgCoreRoutes.CurrentCell);
e.Handled = true;
}
}
private DataGridViewCell GetCoreRoutesGridNextCell(DataGridViewCell currentCell)
{
int i = 0;
DataGridViewCell nextCell = currentCell;
do
{
int nextCellIndex = (nextCell.ColumnIndex + 1) % dgCoreRoutes.ColumnCount;
int nextRowIndex = nextCellIndex == 0 ? (nextCell.RowIndex + 1) % dgCoreRoutes.RowCount : nextCell.RowIndex;
nextCell = dgCoreRoutes.Rows[nextRowIndex].Cells[nextCellIndex];
i++;
}
while (i < dgCoreRoutes.RowCount * dgCoreRoutes.ColumnCount && nextCell.ReadOnly);
return nextCell;
}
我返回了false,并将重点放在了下一个控件上,现在它对我有用。
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if(keyData == Keys.Tab && dgCoreRoutes.CurrentCell == dgCoreRoutes.Rows[dgCoreRoutes.Rows.Count - 1].Cells[(int)enGridColumns.Margin])
{
btnNext.Focus();
return false;
}
else if (keyData == Keys.Tab && dgCoreRoutes.CurrentCell.ReadOnly)
{
dgCoreRoutes.CurrentCell = GetCoreRoutesGridNextCell(dgCoreRoutes.CurrentCell);
return true;
}
else
return base.ProcessCmdKey(ref msg, keyData);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句