How to disable Ctrl + Arrow down on datagridview vb.net?

0

The problem is that in editing mode when doing copy-paste control is used and the arrow to move to the cell below but if you keep pressed control and the arrow goes to the last record of the datagridview and this will make the users are wrong, if you can help me in advance thank you!

    
asked by david arellano 25.01.2018 в 23:19
source

1 answer

1

You can do it in the event KeyDown of DataGridView : if the key pressed is the up or down arrow and the control key is pressed you cancel the event KeyPress so that it does not execute the default behavior (go to the first or last row):

Private Sub DataGridView_KeyDown(sender As Object, e As KeyEventArgs)
    If (e.Control AndAlso (e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Down)) Then
        e.SuppressKeyPress = True
    End If
End Sub 

As you point out in the comments this solution does not work if the EditMode of DataGridView is set to EditOnEnter and you are editing a cell.

For that case the only solution I can think of is to create a custom control that inherits the DataGridView and overwrite the function ProcessDataGridViewKey replacing the functionality of Ctrl + Up and Ctrl + Down by Up and% % co:

Public Class CustomDataGridView
    Inherits DataGridView

    <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

        If e.Control Then
            If e.KeyCode = Keys.Up Then
                Return ProcessUpKey(Keys.Up)
            ElseIf e.KeyCode = Keys.Down Then
                Return ProcessDownKey(Keys.Down)
            End If
        End If

        Return MyBase.ProcessDataGridViewKey(e)

    End Function

End Class

In your form you should include a Down control instead of CustomDataGridView . The new control will have the same functionality except the behavior of those two key combinations.

    
answered by 26.01.2018 / 00:19
source