Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Designating a Windows Forms Button as the Accept Button

Visual Studio .NET 2003

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. (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 in the designer

  1. Select the form on which the button resides.
  2. In the Properties window, set the form's AcceptButton property to the Button control's name.

To designate the accept button programmatically

  • Set the form's AcceptButton property to the appropriate Button control.
    ' Visual Basic
    Private Sub SetDefault(ByVal myDefaultBtn As Button)
      Me.AcceptButton = myDefaultBtn 
    End Sub
    
    // C#
    private void SetDefault(Button myDefaultBtn)
    {
       this.AcceptButton = myDefaultBtn;
    }
    
    // C++
    private:
       void SetDefault(Button * myDefaultBtn)
       {
          this->AcceptButton = myDefaultBtn;
       }
    

See Also

Introduction to the Windows Forms Button Control | Ways to Select a Windows Forms Button Control | Responding to Windows Forms Button Clicks | Form.AcceptButton Property | Designating a Windows Forms Button as the Cancel Button | Button Control (Windows Forms)

Show:
© 2015 Microsoft