0
votes

I have a Datagridview on my form with two columns. I only want the cells in the first column to be selected. So I'm using the datagridview's keydown event to capture the TAB and SHIFT+TAB. So if the first row is selected and the user presses tab, the second row will be selected instead of staying on the first row and selecting the second column's cell.

My if statement for the TAB key is working fine, but for some reason the SHIFT+TAB statement isn't. The cell above the currently selected cell isn't getting selected. Nothing happens.

Private Sub myGrid_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles myGrid.KeyDown
    If e.KeyCode = Keys.Tab Then
        e.SuppressKeyPress = True
        SendKeys.Send("{DOWN}")
    End If

    If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
        e.SuppressKeyPress = True
        SendKeys.Send("{UP}")
    End If
End Sub 

The really weird thing though is if I add a messagebox into the statement it works.

If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
    MsgBox("test")
    e.SuppressKeyPress = True
    SendKeys.Send("{UP}")
End If

Any ideas?

4
Why do you want to redefine user experience? All users are used to the fact that up and down is moving, well, up and down, and tab is... sideways. It's like making a car run vertically instead of horizontally.Neolisk
The two columns are "Question #" and "Score". When you select a row the rest of the information on the form changes to match the information for the selected question. I just want the user to only have to hit TAB once instead of twice to go down a row.ShawnOrr
Why not leave a user with their default option - up/down?Neolisk
The user can use up/down arrow keys if they want to. I'm just wanting the TAB key to go from column 0 : cell 0 to column 0 : cell 1 instead of column 0 : cell 0 to column 1 : cell 0ShawnOrr

4 Answers

0
votes

Sendkeys is a last, last resort...

How about something like this:

Private Sub dgv_KeyDown(sender As Object, e As KeyEventArgs) Handles dgv.KeyDown
    If e.KeyCode = Keys.Tab Then
        e.SuppressKeyPress = True
        dgv.CurrentCell = dgv(0, dgv.CurrentCell.RowIndex + 1)
    End If
End Sub
0
votes

With the help from @rheitzman I was able to see the main issue. The issue came from having the TAB and SHIFT+TAB in two different if statements. Here's my updated code:

Private Sub myGrid_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles myGrid.KeyDown
    Dim row As String = Me.myGrid.CurrentCellAddress.Y

    If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
        e.SuppressKeyPress = True
        Me.myGrid.CurrentCell = Me.myGrid.Rows(row - 1).Cells(0)
        Me.myGrid.Rows(row - 1).Cells(0).Selected = True

    ElseIf e.KeyCode = Keys.Tab Then
        e.SuppressKeyPress = True
        Me.myGrid.CurrentCell = Me.myGrid.Rows(row + 1).Cells(0)
        Me.myGrid.Rows(row + 1).Cells(0).Selected = True
    End If
End Sub
0
votes

Tab and Shift+Tab are already handled by Windows Forms and DataGridView. They move the cell highlighter/selector move left(previous) and right(next). You'd have to create a class that inherits DataGridView and manually override their functionality to achieve what you want, then use that new class for your data grid.

0
votes
Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown

    Dim r As Integer = Me.DataGridView1.CurrentCellAddress.Y
    Dim f As Integer = Me.DataGridView1.CurrentCellAddress.X

    If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
        e.SuppressKeyPress = True
        If 0 = r Then
            Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(DataGridView1.RowCount - 1).Cells(f - 1)
            Me.DataGridView1.Rows(DataGridView1.RowCount - 1).Cells(f - 1).Selected = True
        Else
            Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(r - 1).Cells(f)
            Me.DataGridView1.Rows(r - 1).Cells(f).Selected = True
        End If
    ElseIf e.KeyCode = Keys.Tab Then
        e.SuppressKeyPress = True
        If DataGridView1.RowCount = (r + 1) Then
            Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(0).Cells(f + 1)
            Me.DataGridView1.Rows(0).Cells(f + 1).Selected = True
        Else
            Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(r + 1).Cells(f)
            Me.DataGridView1.Rows(r + 1).Cells(f).Selected = True
        End If
    End If
End Sub