简体   繁体   中英

Capture keycode when arrow keying in combobox vb.net

If you are arrow keying items in a combobox vb.net only fires off the selectedindexchanged event. I would like to know if they are keying up or down or wheather they actually clicked the item with a mouse.

The reason is if they selected the item with the mouse then i would put focus back to the main panel scroll bar so they could use the wheel immediately after selecting an item.

If they arrow key through the items in a combobox then do not focus the main panel.

I tried capturing the keyup events for the combobox but it doesn't work if you are arrow keying the items in a combobox.

You can actually use the PreviewKeyDown event along with the MouseUp event for the combobox.

    Dim UsePanelScroll As Boolean = False

    Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'some code

        If UsePanelScroll = True Then
            Panel1.Focus()
        End If

    End Sub

    Private Sub ComboBox_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs)
        Select Case e.KeyCode
            Case Keys.Tab, Keys.Up, Keys.Down
                UsePanelScroll = False
            Case Else
                UsePanelScroll = True
        End Select
    End Sub

    Private Sub ComboBox_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            UsePanelScroll = True
        End If
    End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM