带组合框的KeyDown

时间:2019-06-10 14:08:02

标签: vb.net

from我在组合框A,B,C,D,E,F,G中有以下项目,我想按下按键以检查选择了哪个项目组合框 我尝试

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

在组合框中,我选择A(ctrl + alt + shft + A),但出现空错误

2 个答案:

答案 0 :(得分:1)

kbHook_KeyDown在程序在后台运行时使用,我认为您想显示自己的表单,希望对您有所帮助

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

答案 1 :(得分:0)

要检查ComboBox中的选定项目,请不要处理KeyDown事件,而要处理SelectedIndexChanged事件。 SelectedItem属性包含所选项目。

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