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

Responding to Windows Forms Button Clicks

Visual Studio .NET 2003

The most basic use of a Windows Forms Button control is to run some code when the button is clicked.

Clicking a Button control also generates a number of other events, such as the MouseEnter, MouseDown, and MouseUp events. If you intend to attach event handlers for these related events, be sure that their actions do not conflict. For example, if clicking the button clears information that the user has typed in a text box, pausing the mouse pointer over the button should not display a tool tip with that now-nonexistent information.

If the user attempts to double-click the Button control, each click will be processed separately; that is, the control does not support the double-click event.

To respond to a button click

  • In the button's Click event handler write the code to run. Button1_Click must be bound to the control; for more information, see Creating Event Handlers at Run Time for Windows Forms and Creating Event Handlers in the Visual Basic Code Editor. Example:
    ' Visual Basic
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       MessageBox.Show("Button1 was clicked")
    End Sub
    
    // C#
    private void button1_Click(object sender, System.EventArgs e)
    {
       MessageBox.Show("button1 was clicked");
    }
    
    // C++
    private:
       System::Void button1_Click(System::Object *  sender,
          System::EventArgs *  e)
       {
          MessageBox::Show("button1 was clicked");
       }
    

See Also

Introduction to the Windows Forms Button Control | Ways to Select a Windows Forms Button Control | Button Control (Windows Forms)

Show:
© 2015 Microsoft