I want to execute the code behind my Search Button by pressing Enter. I have the Accept Button property to my search button. However, when i place my button as NOT visible my search doesn't execute.
I want to be able to press Enter within my text box and execute my button while its not visible. Any suggestions would be great! Below is one attempt at my code in KeyDown Event
if (e.KeyCode == Keys.Enter)
{
buttonSearch_Click((object)sender, (EventArgs)e);
}
In
WPF
apps This code working perfectlythere you go.
There are some cases, when textbox will not handle enter key. I think it may be when you have accept button set on form. In that case, instead of
KeyDown
event you should usetextbox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
Since everybody covered the
KeyDown
answers, how about using theIsDefault
on the button?You can read this tip for a quick howto and what it does: http://www.codeproject.com/Tips/665886/Button-Tip-IsDefault-IsCancel-and-other-usability
You can handle the keydown event of your
TextBox
control.It works even when the button
Visible
property is set tofalse