Trap Enter Key in ComboBox?

  • Thread starter Thread starter Fred Sawtelle
  • Start date Start date
F

Fred Sawtelle

I'm trying to trap the Enter key in any of the key events
(keypress, keydown, keyup) of the Windows .Net combobox
control. Try as I might, I cannot get the event to
respond to an Enter key press; nor can I find anything
documenting this situation. Help is appreciated.

Here's my code. This pops up a messagebox for normal
alphanumeric keys, but does nothing at all when Enter is
pressed.

Private Sub cboLoan_KeyDown(ByVal sender As Object, ByVal
e As System.Windows.Forms.KeyEventArgs) Handles
cboLoan.KeyDown
If e.KeyCode = Keys.Return Then
MessageBox.Show("KeyDown: got it")
End If
End Sub

Private Sub cboLoan_KeyPress(ByVal sender As Object,
ByVal e As System.Windows.Forms.KeyPressEventArgs)
Handles cboLoan.KeyPress
If e.KeyChar = ChrW(13) Then
MessageBox.Show("KeyPress: got it")
End If
End Sub

Private Sub cboLoan_KeyUp(ByVal sender As Object, ByVal e
As System.Windows.Forms.KeyEventArgs) Handles
cboLoan.KeyUp
If e.KeyCode = Keys.Return Then
MessageBox.Show("KeyUp: got it")
End If
End Sub
 
Try use the override ProcessDialogKey of the form that has the combobox. In
this function return true if you handled the key, otherwise return
mybase.processdialogkey(...) and the program will continue as if the
override wasn't there.

Good luck,
Guillaume Hanique.
 
You're using the wrong ENUM value. try this:

If e.KeyCode = Keys.Enter Then

MsgBox("got it!")

End If
 
Back
Top