Press enter in textbox to and execute button command

前端 未结 10 689
时光取名叫无心
时光取名叫无心 2021-01-30 13:40

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 vi

相关标签:
10条回答
  • 2021-01-30 14:11

    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"  ... />
    '''
    
    0 讨论(0)
  • 2021-01-30 14:16

    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());
             }
       }
    
    0 讨论(0)
  • 2021-01-30 14:18

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

    this.AcceptButton = this.buttonSearch;
    
    0 讨论(0)
  • 2021-01-30 14:20

    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();
            }
    
    0 讨论(0)
提交回复
热议问题