hex c
hex c

Reputation: 745

Press enter in textbox to and execute button command

I want to execute the code behind my Search Button by pressing Enter. I have the Accept Button property to my search button. However, when i place my button as NOT visible my search doesn't execute.

I want to be able to press Enter within my text box and execute my button while its not visible. Any suggestions would be great! Below is one attempt at my code in KeyDown Event

if (e.KeyCode == Keys.Enter)
    {
        buttonSearch_Click((object)sender, (EventArgs)e);
    }

Upvotes: 53

Views: 213137

Answers (11)

Cwenga Zozo
Cwenga Zozo

Reputation: 71

private void jobNoToolStripTextBox_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == Convert.ToChar( Keys.Enter))
            {
                searchJobNo();
            }
        }

sometimes you will have to use the Key Char class, worked the same for me

Upvotes: 2

Noctis
Noctis

Reputation: 11763

Since everybody covered the KeyDown answers, how about using the IsDefault on the button?

You can read this tip for a quick howto and what it does: http://www.codeproject.com/Tips/665886/Button-Tip-IsDefault-IsCancel-and-other-usability

Here's an example from the article linked:

<Button IsDefault = "true" 
        Click     = "SaveClicked"
        Content   = "Save"  ... />
'''

Upvotes: 3

titol
titol

Reputation: 1149

There are some cases, when textbox will not handle enter key. I think it may be when you have accept button set on form. In that case, instead of KeyDown event you should use textbox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)

Upvotes: 1

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

You can handle the keydown event of your TextBox control.

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode==Keys.Enter)
    buttonSearch_Click(sender,e);
}

It works even when the button Visible property is set to false

Upvotes: 4

volody
volody

Reputation: 7189

If buttonSearch has no code, and only action is to return dialog result then:

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
            DialogResult = DialogResult.OK;
    }

Upvotes: 0

Ricardo
Ricardo

Reputation: 1

there you go.

private void YurTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            YourButton_Click(this, new EventArgs());
        }
    }

Upvotes: 0

sreejith dinesan
sreejith dinesan

Reputation: 1

In WPF apps This code working perfectly

private void txt1_KeyDown(object sender, KeyEventArgs e)
  {
     if (Keyboard.IsKeyDown(Key.Enter) )
         {
              Button_Click(this, new RoutedEventArgs());
         }
   }

Upvotes: 0

keysl
keysl

Reputation: 2167

If you're just gonna click the button when Enter was pressed how about this?

private void textbox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            buttonSearch.PerformClick();
        }

Upvotes: 6

Keramat
Keramat

Reputation: 63

    private void textbox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            //cod for run
        }
    }

    private void buttonSearch_Click(object sender, EventArgs e)
    {
        textbox1_KeyDown(sender, new KeyEventArgs(Keys.Enter));
    }

Upvotes: 0

Carol
Carol

Reputation: 1930

Alternatively, you could set the .AcceptButton property of your form. Enter will automcatically create a click event.

this.AcceptButton = this.buttonSearch;

Upvotes: 23

Philipp Eger
Philipp Eger

Reputation: 2275

You could register to the KeyDown-Event of the Textbox, look if the pressed key is Enter and then execute the EventHandler of the button:

private void buttonTest_Click(object sender, EventArgs e)
{
    MessageBox.Show("Hello World");
}

private void textBoxTest_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        buttonTest_Click(this, new EventArgs());
    }
}

Upvotes: 111

Related Questions