Trap Enter Key in ComboBox?

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
 
G

Guillaume Hanique

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.
 
R

Roy Osherove

You're using the wrong ENUM value. try this:

If e.KeyCode = Keys.Enter Then

MsgBox("got it!")

End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top