Click here to Skip to main content
15,886,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Dears,
I have a textbox and a button on a dialog.
What I want following function:
if i press Enter on the textbox, the button Ok is clicked
if I press ESC ,the button cancel is clicked.

I want to pass the same event arguments and objects as passed by button click event

Please help me on this.
Thanks in advance
Posted
Updated 29-May-10 4:54am
v2

A simple google search would provide you with what you need. If you had googled "C# textbox button click on enter" you should have been able to find the information.

All you have to do is handle the KeyDown event of the TextBox. The Button has a method called PerformClick() as well. In the KeyDown, just check for Enter or escape

C#
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape)
    {
        cmdCancel.PerformClick();
    }
    else if (e.KeyCode == Keys.Enter)
    {
        cmdOk.PerformClick();
    }
}


It's that simple.
 
Share this answer
 
v2
Comments
mengyi@ynjtt.com 25-May-10 22:04pm    
I think textbox1_keydown event could only be ignited while texbox1 control is having the focus.
So you should establish a hook routine to hook keyboard messages first.
you can set AcceptButton Property of form to you ok button (btnOkButton)
and CancelButton to your cancel button at design time

Thanks
:)
 
Share this answer
 
J imran wrote:
I have a textbox and a button


ok

Your requirement is

J imran wrote:
if i press Enter on the textbox, the button Ok is clicked
if I press ESC ,the button cancel is clicked.


so whats the big deal in that imran it was not that hard to do so

hers your baked code but let me explain u what i have done,
I have generated click event of both the buttons and have wrote code to display messageboxes u may modify the code as per your need

private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button Ok is clicked");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button cancel is clicked");
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                button2_Click(sender, e);
            }
            if (e.KeyCode == Keys.Enter)
            {
                button1_Click(sender, e);
            }
        }


Do rate my answer once you find it useful

Thanks & Regards
Radix :rose:
 
Share this answer
 
ok
first way is key event , u can use key event as answer describe for u
another way is hook that work like key event but is more profissinal
 
Share this answer
 
Comments
Henry Minute 29-May-10 10:56am    
Reason for my vote of 1
A dollar short and at least two days late

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900