The simple option is just to set the forms's AcceptButton to the button you want pressed (usually "OK" etc):
TextBox tb = new TextBox();
Button btn = new Button { Dock = DockStyle.Bottom };
btn.Click += delegate { Debug.WriteLine("Submit: " + tb.Text); };
Application.Run(new Form { AcceptButton = btn, Controls = { tb, btn } });
If this isn't an option, you can look at the KeyDown event etc, but that is more work...
TextBox tb = new TextBox();
Button btn = new Button { Dock = DockStyle.Bottom };
btn.Click += delegate { Debug.WriteLine("Submit: " + tb.Text); };
tb.KeyDown += (sender,args) => {
if (args.KeyCode == Keys.Return)
{
btn.PerformClick();
}
};
Application.Run(new Form { Controls = { tb, btn } });
The usual way to do this is to set the Form
's AcceptButton
to the button you want "clicked". You can do this either in the VS designer or in code and the AcceptButton
can be changed at any time.
This may or may not be applicable to your situation, but I have used this in conjunction with GotFocus
events for different TextBox
es on my form to enable different behavior based on where the user hit Enter. For example:
void TextBox1_GotFocus(object sender, EventArgs e)
{
this.AcceptButton = ProcessTextBox1;
}
void TextBox2_GotFocus(object sender, EventArgs e)
{
this.AcceptButton = ProcessTextBox2;
}
One thing to be careful of when using this method is that you don't leave the AcceptButton
set to ProcessTextBox1
when TextBox3
becomes focused. I would recommend using either the LostFocus
event on the TextBox
es that set the AcceptButton
, or create a GotFocus
method that all of the controls that don't use a specific AcceptButton
call.
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button.PerformClick();
// these last two lines will stop the beep sound
e.SuppressKeyPress = true;
e.Handled = true;
}
}
Bind this KeyDown event to the textbox, then when ever you press a key, this event will be fired. Inside the event we check whether user has pressed "Enter key", if so, you can perform you action
I came across this whilst looking for the same thing myself, and what I note is that none of the listed answers actually provide a solution when you don't want to click the 'AcceptButton' on a Form when hitting enter.
A simple use-case would be a text search box on a screen where pressing enter should 'click' the 'Search' button, not execute the Form's AcceptButton behaviour.
This little snippet will do the trick;
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
if (!textBox.AcceptsReturn)
{
button1.PerformClick();
}
}
}
In my case, this code is part of a custom UserControl derived from TextBox, and the control has a 'ClickThisButtonOnEnter' property. But the above is a more general solution.
Simply set form "Accept Button" Property to button that you want to hit by Enter key.
Or in load event write this.acceptbutton = btnName;
Most beginner friendly solution is:
In your Designer, click on the text field you want this to happen. At the properties Window (default: bottom-right) click on the thunderbolt (Events). This icon is next to the alphabetical sort icon and the properties icon.
Scroll down to keyDown
. Click on the Dropdown field right to it. You'll notice there's nothing in there so simply press enter. Visual Studio will write you the following code:
private void yourNameOfTextbox_KeyDown(object sender, KeyEventArgs e)
{
}
Then simply paste this between the brackets:
if (e.KeyCode == Keys.Enter)
{
yourNameOfButton.PerformClick();
}
This will act as you would have clicked it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With