Do action when esc is pressed

Stoffel85

Newcomer
Joined
Dec 18, 2004
Messages
10
Location
Schoten, Belgium
Hi,

I want to make a search bar simular to the firefox search bar.
So I want it to close when I press Escape.

The problem is, when I press Escape, it won't run the KeyUp, KeyDown or KeyPress sequence.

I tried it on the toolbar, the textbox in the toolbar and on the button, none work.

Code:
  Private Sub tlTxtSomething_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tlCategorie.KeyDown
    If (e.KeyCode = Keys.Escape) Then
      tlSearch.Visible = False
    End If
  End Sub
 
Are you using a ToolBar or a ToolStrip? I don't believe that a ToolBar ever actually receives focus, and a control must have focus to receive keyboard input. If there are controls on the toolbar (and I'm guessing that you have a textbox), you can use the control's keydown/keypress events. If you are using a ToolStrip, and a control within the ToolStrip has focus, the ToolStrip will also receive the keyboard input. Another possibility is to set the containing form's KeyPreview property to true and handle keyboard events for the form.
 
Thanks for your reply.

It is a ToolStrip (I'm using VS2005).
It works when I use the KeyUp event in my form. But when I do the same in the ToolStrip KeyUp event, nothing happens.

If there's no other way to let that work, then I'll use the KeyUp event in my form.

Thanks already
 
Back
Top