I have one form, in that one textbox & Buttons is there for Search functionality. Enter whatever in the text box, it will search from the one of the Tree View Node.

So my question is when I form is showed to user and when I press the Enter Key, it will serach the that Text in from the Tree node rather then click on that Search button ?

4 Years
Discussion Span
Last Post by Momerath

Every control including the form has they're own KeyPress event, which makes programming for that quite a bother. A more efficient solution would be to give the textbox first tabindex, so that when the form starts the textbox immediatley has focus. This way you only need to program the KeyPress event handler for one control


You need to have a method in the KeyDown event of the textbox like say :-

private void CallKeyDown(object sender, KeyEventArgs e)
      // Your Search Functionality

Hope that helps :)

Edited by KM499: Code added


If you set the KeyPreview property of the Form to true, it will get to see if it wants to handle the keypress before it's passed on to whatever control has focus, so you don't need a handler for all the controls, just one for the form.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.