473,408 Members | 1,759 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,408 software developers and data experts.

How to fire the Click event of a button by hitting Enter in a Text

I have a single button on my form, and when I press the Enter key while a
TextBox has focus I want the Button.Click event to fire.

Is there a simple way to do this ?
Jan 8 '06 #1
5 22683
New to this myself, but surely the 'KeyPress' event.. you can detect if the
keypress was an enter, and then Button.click();
"Dundealing" <Du********@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
I have a single button on my form, and when I press the Enter key while a
TextBox has focus I want the Button.Click event to fire.

Is there a simple way to do this ?

Jan 8 '06 #2
You can call the event handler by calling something like
this.button1_Click(this.button1, null); from the textbox Keypress
eventhandler if the KeyChar value of the KeyPressEventArgs e is equal to
(char)13. However you might want to consider not placing the code to
be executed in the event handler itself but instead in a private method
and call that method from both the button Click and textbox Keypress
events. This concept can be extended by using the Command design pattern
which I have found leads to much cleaner code in winform applications.
For more information about the Command design pattern see:
http://www.dofactory.com/Patterns/PatternCommand.aspx

Regards,

Olle

Dundealing wrote:
I have a single button on my form, and when I press the Enter key while a
TextBox has focus I want the Button.Click event to fire.

Is there a simple way to do this ?


Jan 8 '06 #3
Hello

Can consider this for matching
if (e.KeyChar==Convert.ToChar(Keys.Enter))

this.button1_Click(sender,null);
--
ALI RAZA SHAIKH
MCAD.net

alirazashaikh.blogspot.com
www.programmersparadise.cjb.net
"Dundealing" <Du********@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
I have a single button on my form, and when I press the Enter key while a
TextBox has focus I want the Button.Click event to fire.

Is there a simple way to do this ?

Jan 8 '06 #4
Set the forms .AcceptButton to the button you want. You may also want to
make use of .CancelButton.

--

Derek Davis
dd******@gmail.com

"Dundealing" <Du********@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
I have a single button on my form, and when I press the Enter key while a
TextBox has focus I want the Button.Click event to fire.

Is there a simple way to do this ?

Jan 9 '06 #5
Create an onkeypress event on the button. When catched check which
button is pressed and if its the enter button call the 'button1_Click'
method.

For example

private void button1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == (char) 13)
{
this.button1_Click(sender, new EventArgs());
}
}

Jan 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ivanhoe | last post by:
All articles and tutorials I've seen on subject of forms say that if there's name and value for submit button(input tag with type="submit"), they'll get submitted along with the rest of the...
5
by: Carlo Marchesoni | last post by:
From an aspx page (A.aspx) I open another one (B.aspx - for table lookup). When the user selects an entry in B.aspx I would like to force a button's event in A.aspx to be fired. I guess the only...
3
by: Imran Aziz | last post by:
Hello All, I have a search text and button that post data and my button handler filters the repeater control. However when the button is clicked the first time. The page_load event is being called...
4
by: sowencheung | last post by:
Hi, all The scenario is like this: I have a master page, contains two user controls, one is a search control, another is a login control. The server-side <form> is in the master page,...
0
by: Tom Edelbrok | last post by:
I'm using VS 2005 to develop an intranet asp.net web application and I get a weird situation. If I start out with any ASPX page that contains an ImageButton control followed by a TextBox control,...
19
by: Daniela Roman | last post by:
Hello, I try to fire an event under a button click event and maybe anybody can give a clue please. I have let's say a WEB grid with PageIndexChanged event: private void...
17
by: Eric | last post by:
I'm new to JavaScript and I wrote this code to play with. Oddly, if I enter text in a box and then press the button, I only get the onChange event for the text box and not the button's onclick...
6
by: erdos | last post by:
I have an asp.net 2.0 button with server and client side click events. On the first click, both events will fire. On the next click, only the client side event fires. How do I make the server...
2
by: Joergen Bech | last post by:
Hope someone has a solution or some suggestions for this. This cannot be right?!? Problem: I have multiple non-modal forms open at the same time. One or more of these forms have a...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.