473,395 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

toolStripTextBox action when Enter is pressed

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
2 13438
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Blue Bell Trading - Customer Services | last post by:
Hi, I have a text box, when a user presses enter I wish for the event on a command button on the form to be performed -- Kind Regards, Customer Services Blue Bell Trading
4
by: Brian Keating | last post by:
hi there, does anyone know how to prevent the context menu on a ToolStripText box from appearing? when i right click on the toolstriptextbox i get presented with the system undo ------ cut...
2
by: Husam | last post by:
Hi EveryBody: How can I use raise event statment to get the same action when I press the enter button in the key bord ? Some body give me this: SendKeys "{ENTER}" Note that where you must...
0
by: Gianmaria Iaculo - NVENTA | last post by:
Hi, i need a custom toolstriptextbox... something that is similar to the standard one but witha button on the right. I want to bypass the user "ENTER" and let him press a button with something...
0
by: mcurros | last post by:
Too late but it may be of some help to others... me.ActiveControl=me.ToolStripTextBox.Control Cheers! Mariano
4
by: Lloyd Sheen | last post by:
I just want to select the complete text when someone enters the textbox (mouse click etc.). When I am executing within the IDE (VS 2005 Pro) no problem. Execute outside the IDE and no select...
2
by: Lloyd Sheen | last post by:
I am just trying to get the event when a user clicks into the ToolStripTextBox to allow me to select all the text. I got an answer to handle the click event as an enter. Well if I do that then I...
6
by: Stanimir Stamenkov | last post by:
I have a form without a submit button like: <form name="form1" action="" onsubmit="alert('submit ' + this.name);"> <div> <label>Field 1: <input type="text" name="field1"...
1
by: daonho | last post by:
I tried to use javascript to trigger up the button click function when user press enter key from the textbox. This function work fine with a single button click such has login page. However, if the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.