Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27349

DataGridView - detecting Tab in cell edit and canceling

$
0
0
I am trying to detect the Tab key in a DataGridView while the cell is being edited, and then cancel the key. Below is what I am currently doing. Unfortunately, setting "e.IsInputKey = True" is not passing the key to the "KeyDown" event. Any help detecting the Tab and canceling the key would be appreciated. Thanks.

Code:

    Private Sub ProductDataGrid_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles ProductDataGrid.EditingControlShowing
        Dim cellField As TextBox = CType(e.Control, TextBox)

        AddHandler cellField.PreviewKeyDown, AddressOf CellField_PreviewKeyDown
        AddHandler cellField.KeyDown, AddressOf CellField_KeyDown
        AddHandler cellField.KeyPress, AddressOf CellField_KeyPress
    End Sub

    Private Sub CellField_PreviewKeyDown(ByVal sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs)
        If e.KeyCode = Keys.Tab Then
            e.IsInputKey = True
        End If
    End Sub

    Private Sub CellField_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If Not e.Shift AndAlso e.KeyCode = Keys.Tab Then
            'do stuff and cancel
            e.SuppressKeyPress = True
        ElseIf e.Shift AndAlso e.KeyCode = Keys.Tab Then
            'do stuff and cancel
            e.SuppressKeyPress = True
        End If
    End Sub


Viewing all articles
Browse latest Browse all 27349

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>