代码之家  ›  专栏  ›  技术社区  ›  user26087

使用enter键覆盖datagridview的导航

  •  1
  • user26087  · 技术社区  · 16 年前

    5 回复  |  直到 16 年前
        1
  •  3
  •   Cerebrus    16 年前

    您需要做的就是处理DataGridView的KeyDown事件,并在处理程序中检查当前按下的键是否为Enter键。如果是这样,只需将DataGridView的CurrentCell设置为下一个单元格(还要检查这是否是行中的最后一个单元格,在这种情况下,移动到下一行的第一个单元格。)

    Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles DataGridView1.KeyDown
      If e.KeyCode = Keys.Enter Then
        Dim numCols As Integer = DataGridView1.ColumnCount
        Dim numRows As Integer = DataGridView1.RowCount
        Dim currCell As DataGridViewCell = DataGridView1.CurrentCell
        If currCell.ColumnIndex = numCols - 1 Then
          If currCell.RowIndex < numRows - 1 Then
            DataGridView1.CurrentCell = DataGridView1.Item(0, currCell.RowIndex + 1)
          End If
        Else
          DataGridView1.CurrentCell = DataGridView1.Item(currCell.ColumnIndex + 1, currCell.RowIndex)
        End If
        e.Handled = True
      End If
    End Sub
    
        2
  •  2
  •   user1856205    12 年前
    Private Sub DbGDetail_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles DbGDetail.KeyDown
        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{Tab}")
            e.Handled = True
        End If
    End Sub
    
    
    Private Sub DbGDetail_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DbGDetail.CellEndEdit
    
        SendKeys.Send("{up}")
        SendKeys.Send("{Tab}")
    
    End Sub
    
        3
  •  1
  •   ken papa    12 年前

    我找到了进去所需的帮助 this link 具体在 张荣春对分类的反馈 DataGridView ProcessDialogKey

    请注意,在这种情况下,我只是将CurrentCell留在原地,但可以在调用“return true”之前重新分配它。

    public class DataGridViewAddRow : DataGridView
    {
    
        protected override bool ProcessDialogKey(Keys keyData)
        {
    
            //cell is in Edit mode
    
            if (keyData == Keys.Enter)
            {
    
                if (this.CurrentCell.RowIndex == this.Rows.Count-2)
                {                            
                    return true;
                }
    
            }
    
            return base.ProcessDialogKey(keyData);
    
        }
    
    }
    
        4
  •  0
  •   Widodo    12 年前

    当按下VB.net的Enter键时,DataGridView会聚焦到下一个单元格或列:

    如果是├1.CurrentCell。ColumnIndex=ViewModel 1.ColumnCount-1然后 ViewModel 1.CurrentCell=ViewModel 1.项(0,ViewModel 1.Currentell.RowIndex+1) 其他的 语句结束

    结束子

        5
  •  0
  •   user5812309    9 年前

    公共类自定义DataGridView

    <System.Security.Permissions.UIPermission( _
        System.Security.Permissions.SecurityAction.LinkDemand, _
        Window:=System.Security.Permissions.UIPermissionWindow.AllWindows)> _
    Protected Overrides Function ProcessDialogKey( _
        ByVal keyData As Keys) As Boolean
    
        ' Extract the key code from the key value. 
        Dim key As Keys = keyData And Keys.KeyCode
    
        ' Handle the ENTER key as if it were a RIGHT ARROW key. 
        If key = Keys.Enter Then
            Return Me.ProcessTabKey(keyData)
        End If
    
        Return MyBase.ProcessDialogKey(keyData)
    
    End Function
    
    <System.Security.Permissions.SecurityPermission( _
        System.Security.Permissions.SecurityAction.LinkDemand, Flags:= _
        System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _
    Protected Overrides Function ProcessDataGridViewKey( _
        ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
    
        ' Handle the ENTER key as if it were a RIGHT ARROW key. 
        If e.KeyCode = Keys.Enter Then
            Return Me.ProcessTabKey(e.KeyData)
        End If
    
        Return MyBase.ProcessDataGridViewKey(e)
    
    End Function
    

    结束课程