Bagikan melalui


How to: Respond to Windows Forms Button Clicks

 

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'sClick EventHandler write the code to run. Button1_Click must be bound to the control. For more information, see How to: Create Event Handlers at Run Time for Windows Forms.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
       MessageBox.Show("Button1 was clicked")  
    End Sub  
    
    private void button1_Click(object sender, System.EventArgs e)  
    {  
       MessageBox.Show("button1 was clicked");  
    }  
    
    private:  
       void button1_Click(System::Object ^ sender,  
          System::EventArgs ^ e)  
       {  
          MessageBox::Show("button1 was clicked");  
       }  
    

See Also

Button Control Overview
Ways to Select a Windows Forms Button Control
Button Control