Partilhar via


How to: Designate a Windows Forms Button as the Accept Button

On any Windows Form, you can designate a Button control to be the accept button, also known as the default button. Whenever the user presses the ENTER key, the default button is clicked regardless of which other control on the form has the focus.

NoteNote

The exceptions to this are when the control with focus is another button — in that case, the button with the focus will be clicked — or a multiline text box, or a custom control that traps the ENTER key.

To designate the accept button

  • Set the form's AcceptButton property to the appropriate Button control.

    Private Sub SetDefault(ByVal myDefaultBtn As Button)
      Me.AcceptButton = myDefaultBtn 
    End Sub
    
    private void SetDefault(Button myDefaultBtn)
    {
       this.AcceptButton = myDefaultBtn;
    }
    
    private void SetDefault(Button myDefaultBtn) 
    {
       this.set_AcceptButton(myDefaultBtn);
    }
    
    private:
       void SetDefault(Button ^ myDefaultBtn)
       {
          this->AcceptButton = myDefaultBtn;
       }
    

See Also

Tasks

How to: Respond to Windows Forms Button Clicks
How to: Designate a Windows Forms Button as the Cancel Button

Reference

Button Control Overview (Windows Forms)
AcceptButton

Concepts

Ways to Select a Windows Forms Button Control

Other Resources

Button Control (Windows Forms)