Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/how-to-respond-to-windows-forms-button-clicks.md
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

2.7 KiB

title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title description ms.date dev_langs helpviewer_keywords ms.assetid
Respond to Button Clicks Learn 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. 03/30/2017
csharp
vb
cpp
buttons [Windows Forms], responding to Click events
events [Windows Forms], Click events
Click event [Windows Forms], Button control
MouseDown event
Button control [Windows Forms], click response
double-clicks
examples [Windows Forms], controls
Click event [Windows Forms], responding to
7a4951bd-369c-4662-b246-28ad83eda484

How to: Respond to Windows Forms Button Clicks

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

Clicking a xref:System.Windows.Forms.Button control also generates a number of other events, such as the xref:System.Windows.Forms.Control.MouseEnter, xref:System.Windows.Forms.Control.MouseDown, and xref:System.Windows.Forms.Control.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 xref:System.Windows.Forms.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 xref:System.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