monkey from Tanzania
monkey from Tanzania

Reputation: 526

KeyDown With ComboBox

from i have the following items in combobox A,B,C,D,E,F,G i want keydown to check which item selected to combobox i try

Private Sub kbHook_KeyDown(ByVal Key As System.Windows.Forms.Keys) Handles kbHook.KeyDown
 `If My.Computer.Keyboard.CtrlKeyDown AndAlso
    My.Computer.Keyboard.AltKeyDown AndAlso
    My.Computer.Keyboard.ShiftKeyDown AndAlso
          ComboBox2.SelectedValue.ToString then ` 
'some stuff 
end if
end sub

in combobox i choose A(ctrl+alt+shft+A) but i got null error

Upvotes: 1

Views: 400

Answers (2)

betrice mpalanzi
betrice mpalanzi

Reputation: 1117

kbHook_KeyDown used when program run in background i think you want to show your form i hope this will help

Private Sub kbHook_KeyDown(ByVal Key As System.Windows.Forms.Keys) Handles kbHook.KeyDown
    Select Case CStr(ComboBox2.SelectedItem)
        Case "A"
            If My.Computer.Keyboard.CtrlKeyDown AndAlso
    My.Computer.Keyboard.AltKeyDown AndAlso
    My.Computer.Keyboard.ShiftKeyDown AndAlso
    Key = Keys.A Then
              form1.show
                Me.BackColor = Color.Indigo

            End If
        Case "B"
            If My.Computer.Keyboard.CtrlKeyDown AndAlso
    My.Computer.Keyboard.AltKeyDown AndAlso
    My.Computer.Keyboard.ShiftKeyDown AndAlso
    Key = Keys.B Then
            form1.show
                Me.BackColor = Color.Indigo

            End If
        Case "C"
            If My.Computer.Keyboard.CtrlKeyDown AndAlso
    My.Computer.Keyboard.AltKeyDown AndAlso
    My.Computer.Keyboard.ShiftKeyDown AndAlso
    Key = Keys.C Then
                form1.show
                Me.BackColor = Color.Indigo

                 Case Else
                Me.Hide()

End Select
            End If

Upvotes: 1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112447

To check the selected item from a ComboBox do not handle the KeyDown event but the SelectedIndexChanged event. The SelectedItem property contains the selected item.

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _
    Handles ComboBox1.SelectedIndexChanged

    Select Case CStr(ComboBox1.SelectedItem)
        Case "A"
            MessageBox.Show("First case")
        Case "B"
            MessageBox.Show("Second case")
        Case "C"
            MessageBox.Show("etc.")
        Case "D"
        Case "E"
        Case "F"
        Case "G"
        Case Else
    End Select
End Sub

Upvotes: 0

Related Questions