Link to home
Create AccountLog in
Avatar of DeniseGoodheart
DeniseGoodheart

asked on

How to Use KeyDown Event with the Combo Box Control

Good Day:

I am creating a WinForms application using VS.NET 2005 and VB.NET 2005.  I need to determine whether the user key pressed B or S in the combo box called cboType.  I get an error message that reads: Cannot handle event  keydown because they do not have the same signature.  I tried the following and get the same error.

Private Sub cboType_KeyDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboType.KeyDown
If e.KeyValue=keys.B then
'Do Something
End sub

Private Sub cboType_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboType.KeyPress
If e.KeyValuee=keys.S then
'Do Something
End Sub

Any Suggestions?
Thank You,
Denise


SOLUTION
Avatar of jppinto
jppinto
Flag of Portugal image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Or use tis:

Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 66 Then
   MsgBox "Key B pressed"
End If
End Sub
Sorry use the keyDown event
Private Sub cboType_KeyDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboType.KeyDown
If e.KeyCode=keys.B then
'Do Something
End sub
Avatar of DeniseGoodheart
DeniseGoodheart

ASKER

Hello,

Thanks for all your suggestions. None of the suggestions work and I still get the same error.

And the msforms is not defined for the following:
Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 66 Then
   MsgBox "Key B pressed"
End If
The keyDown was working for me, you did use the e.KeyCode not the KeyValue right?
I noticed above in your intial post your signature does not match this:
specifically the -> e As System.KeyEventArgs <-

Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
        If e.KeyCode = Keys.B Then
          'do something
        End If
End Sub

Open in new window

SOLUTION
Avatar of Alfredo Luis Torres Serrano
Alfredo Luis Torres Serrano
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Hi!

Thanks for the points :)