By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,652 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,652 IT Pros & Developers. It's quick & easy.

toolStripTextBox action when Enter is pressed

P: n/a
In Internet Explorer, for example, when you hit return in the address
bar, the Go button is pressed. In my program, I have a toolstrip with a
textbox and button. How do I ensure the button is 'clicked' when Enter
is pressed in the text box.

I can't set the form's AcceptButton property to the toolStripButton -
it doesn't allow toolStripButtons to be set as the AcceptButton.

As a last resort, I could create a keypress event handler for the
textbox that called button_click() if return was pressed but that's not
a great solution.

TIA

Jul 28 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"matthewr" wrote:
In Internet Explorer, for example, when you hit return in the address
bar, the Go button is pressed. In my program, I have a toolstrip with a
textbox and button. How do I ensure the button is 'clicked' when Enter
is pressed in the text box.

I can't set the form's AcceptButton property to the toolStripButton -
it doesn't allow toolStripButtons to be set as the AcceptButton.
Set the Form's KeyPreview property to true, handle the Form's KeyPress
property, then in the KeyPress handler, check if your textbox is focused and
the Enter key was pressed. If so, tell the Go button to PerformClick and
handle the keypress.

private void Form1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
// if textbox is focused and Enter key was pressed
if (this.textBox1.Focused && e.KeyChar == '\r')
{
// click the Go button
this.button1.PerformClick();
// don't allow the Enter key to pass to textbox
e.Handled = true;
}
}

--
Timm Martin
Mini-Tools
..NET Components and Windows Software
http://www.mini-tools.com

Jul 28 '06 #2

P: n/a
Thanks, Timm. I was hoping to avoid keypress handling and find a way to
use a toolStripButton as the form's AcceptButton, but it doesn't look
like that's possible/easy.
Mini-Tools Timm wrote:
"matthewr" wrote:
In Internet Explorer, for example, when you hit return in the address
bar, the Go button is pressed. In my program, I have a toolstrip with a
textbox and button. How do I ensure the button is 'clicked' when Enter
is pressed in the text box.

I can't set the form's AcceptButton property to the toolStripButton -
it doesn't allow toolStripButtons to be set as the AcceptButton.

Set the Form's KeyPreview property to true, handle the Form's KeyPress
property, then in the KeyPress handler, check if your textbox is focused and
the Enter key was pressed. If so, tell the Go button to PerformClick and
handle the keypress.

private void Form1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
// if textbox is focused and Enter key was pressed
if (this.textBox1.Focused && e.KeyChar == '\r')
{
// click the Go button
this.button1.PerformClick();
// don't allow the Enter key to pass to textbox
e.Handled = true;
}
}

--
Timm Martin
Mini-Tools
.NET Components and Windows Software
http://www.mini-tools.com
Jul 28 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.