469,306 Members | 2,398 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

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 22381
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by ivanhoe | last post: by
5 posts views Thread by Carlo Marchesoni | last post: by
3 posts views Thread by Imran Aziz | last post: by
reply views Thread by Tom Edelbrok | last post: by
19 posts views Thread by Daniela Roman | last post: by
6 posts views Thread by erdos | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.