how can i disable the arrows and enter key when I edit the datagridview cell in vb.net

Muhammad Usman Qader 0 Reputation points
2023-02-04T11:19:15.6333333+00:00

Aslam o Alikum

I am making a project in vb.net and I am facing a problem in datagridview when i am typing some word in a cell when i press the up or down key then my cursor move to next cell. i want when i press the enter key then my cursor is move to next cell otherwise still in editing mode in a current cell. any one can help me to solved this issue.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Reza Aghaei 4,936 Reputation points MVP
    2023-02-04T12:03:08.7233333+00:00

    You can handle ProcessDataGridViewKey and check if editing control is there, then ignore the arrow keys, and let the editing control use the keys. But if the editing control is not there, the default behavior is good.

    Public Class MyDataGridView
        Inherits DataGridView
        'Use this method to disable arrow keys navigation to next cell while typing
        Protected Overrides Function ProcessDataGridViewKey(e As KeyEventArgs) As Boolean
            Dim arrowKeys = New Keys() {Keys.Down, Keys.Up, Keys.Left, Keys.Right}
            If arrowKeys.Contains(e.KeyCode) And EditingControl IsNot Nothing Then
                Return False ' pass it To the editing control
            End If
            Return MyBase.ProcessDataGridViewKey(e)
        End Function
    
        'Use this method to disable Enter key navigation to next cell while typing
        Protected Overrides Function ProcessDialogKey(keyData As Keys) As Boolean
            If ((keyData And Keys.Enter) = Keys.Enter) And EditingControl IsNot Nothing Then
                Return True 'Processed
            End If
            Return MyBase.ProcessDialogKey(keyData)
        End Function
    
    End Class
    

    This method is a good point to control almost all the keys, but DataGridView has other methods to control the key behavior, including ProcessDialogKey or ProcessCmdKey that you can override to customize the functionality. You can also call (not override) the methods like ProcessRightKey, ProcessF2Key, and etc. to call the functionality.