vb.net 如何在VB.net中的datagridview单元格结束编辑中捕获箭头键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19581732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:36:46  来源:igfitidea点击:

How to capture arrow keys in datagridview cell end edit in VB.net

vb.netdatagridviewcellarrow-keys

提问by Matthew

Here is my code for Cell End Edit and Selection Changed event:

这是我的 Cell End Edit 和 Selection Changed 事件代码:

Private Sub dvJOBranch_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dvJOBranch.CellEndEdit

    'get the current column and row index
    curCol = e.ColumnIndex
    curRow = e.RowIndex

    'if the cell is blank, set it to zero
    If IsDBNull(dvJOBranch.Rows(curRow).Cells.Item(curCol).Value) Then
        dvJOBranch.Rows(curRow).Cells.Item(curCol).Value = 0

        'convert it to integer
    Else
        dvJOBranch.Rows(curRow).Cells.Item(curCol).Value = _
            Convert.ToInt32(dvJOBranch.Rows(curRow).Cells.Item(curCol).Value.ToString())
    End If



    'if the user do mouseclick in datagridview
    isMouseClick = dvJOBranch.Capture.ToString()

    'if the user does not click any cell from the datagridview
    If isMouseClick = False Then

        isEdited = True
        iColumnindex = e.ColumnIndex
        irowindex = e.RowIndex

        If dvJOBranch.CurrentRow.Index = dvJOBranch.RowCount - 1 Then
            If dvJOBranch.CurrentCell.ColumnIndex < dvJOBranch.ColumnCount - 1 Then
                dvJOBranch.CurrentCell = dvJOBranch.Item(iColumnindex + 1, irowindex)
            End If
            isEdited = False
        End If

    End If



End Sub

Private Sub dvJOBranch_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dvJOBranch.SelectionChanged

    'if the user does not click any cell from the datagridview
    If isMouseClick = False Then

        If isEdited Then
            If dvJOBranch.CurrentCell.ColumnIndex < dvJOBranch.ColumnCount - 1 Then
                dvJOBranch.CurrentCell = dvJOBranch.Item(iColumnindex + 1, irowindex)
            Else
                dvJOBranch.CurrentCell = dvJOBranch.Item(2, irowindex + 1)
            End If
            isEdited = False
        End If


    End If

    'set it to false
    isMouseClick = False

End Sub

The function of this code is to move the current cell to the right after editing using ENTER key, In my code I also capture the mouse click if the user click any cell because it has errors If i do not capture the mouse click, What I'm trying to do now is to capture the Arrow keys while I'm editing. Because after editing a cell, for example when I press Arrow key UP, it moves to the right like the Function for my ENTER key instead of moving upward.

此代码的功能是在使用 ENTER 键编辑后将当前单元格向右移动,在我的代码中,如果用户单击任何单元格,我也会捕获鼠标单击,因为它有错误如果我不捕获鼠标单击,我会做什么我现在要做的是在编辑时捕获箭头键。因为在编辑单元格后,例如当我按向上箭头键时,它会像我的 ENTER 键的功能一样向右移动,而不是向上移动。

Any help and guide will be appreciated, Thank you

任何帮助和指导将不胜感激,谢谢

采纳答案by Crushermike

You could be using the KeyUp event of the datagridview like this :

您可以像这样使用 datagridview 的 KeyUp 事件:

Private Sub dvJOBranch(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles dvJOBranch.KeyUp
    If e.KeyCode = Keys.Up Then
        ' Do your code here
    End If
End Sub

And if you want the enter key to be handled in there you could just use a select case also :

如果你想在那里处理回车键,你也可以使用一个选择案例:

 Private Sub dvJOBranch(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles dvJOBranch.KeyUp
    Select Case e.KeyCode
        Case Keys.Enter
            ' Code for enter
        Case Keys.Up
            ' Code for up arrow
            'Etc
    End Select
End Sub