C# 选项卡索引在datagridview行中循环,不会移动到windows窗体c中的下一个控件#

C# 选项卡索引在datagridview行中循环,不会移动到windows窗体c中的下一个控件#,c#,winforms,datagridview,tabindex,C#,Winforms,Datagridview,Tabindex,如果tab index位于datagridview的最后一行和最后一列,如果此时按tab键,它将移动到datagridview的第一行和第一列,而不是下一个控件(按钮)。有人能建议我如何停止最后一行的选项卡索引并移动到下一个控件吗。我试过这个密码 private void dgCoreRoutes_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Tab) { if (dgCoreRoutes.Curr

如果tab index位于datagridview的最后一行和最后一列,如果此时按tab键,它将移动到datagridview的第一行和第一列,而不是下一个控件(按钮)。有人能建议我如何停止最后一行的选项卡索引并移动到下一个控件吗。我试过这个密码

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;
}
private void dgCoreRoutes\u KeyUp(对象发送方,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、 已处理=正确;
}
}
专用DataGridViewCell GetCoreRoutesGridNextCell(DataGridViewCell currentCell)
{
int i=0;
DataGridViewCell-nextCell=currentCell;
做
{
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++;
} 
而(i
在dataGridView中编辑单元格时,可能会有另一个控件用于编辑,因此我们无法在KeyUp或keydown方法中获取Tab键事件

 protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
 {
    if (keyData == Keys.Tab && dgCoreRoutes.CurrentCell.RowIndex == dgCoreRoutes.Rows.Count-1)
    {
       dgCoreRoutes.TabStop = true;
       //return true;
       //Use standardTab = true; if you want to tab only standard columns and not cells.
    }


else if (keyData == Keys.Tab && dgCoreRoutes.CurrentCell.ReadOnly)
{
       dgCoreRoutes.CurrentCell = GetCoreRoutesGridNextCell(dgCoreRoutes.CurrentCell);
       e.Handled = true;
       return true;
}
     else
           return base.ProcessCmdKey(ref msg, keyData);
    }

我返回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);
}

很难猜到会发生什么,DGV已经对Tab键进行了特殊处理,并在keydown而不是keyup时执行此操作。如果要正确执行此操作,必须从DGV派生自己的类并重写。顺便说一句,结果通常很糟糕。我想在具有编辑控件的特定列上设置Tab索引,可能这个方法是个问题,我正在努力解决这个问题好的,你的意思是,我应该删除这些事件,并尝试使用此ProcessCmdKey重写方法处理此功能是的,不要删除。只需将句柄重定向到此处,对现有处理程序进行一段时间的注释,然后看看这对你有何影响。是的,它不会进入第一行,但会被冻结,不会继续其中,我尝试设置下一个控件焦点属性,但没有帮助,尝试了其他选项。我认为由于“TabStop=true”,它处于冻结状态。请删除该行代码。让我尝试并返回解决方案