Skip to content

Latest commit

 

History

History
51 lines (39 loc) · 2.71 KB

event-handlers-overview-windows-forms.md

File metadata and controls

51 lines (39 loc) · 2.71 KB
title description ms.date ms.topic dev_langs helpviewer_keywords ms.assetid
Event Handlers Overview
Learn about the standard Windows Forms event handler format, including which parameters are passed to the handlers.
03/30/2017
overview
csharp
vb
cpp
Windows Forms, event handling
event handling [Windows Forms], Windows Forms
event handlers [Windows Forms], about event handlers
228112e1-1711-42ee-8ffa-ff3555bffe66

Event Handlers Overview (Windows Forms)

An event handler is a method that is bound to an event. When the event is raised, the code within the event handler is executed. Each event handler provides two parameters that allow you to handle the event properly. The following example shows an event handler for a xref:System.Windows.Forms.Button control's xref:System.Windows.Forms.Control.Click event.

Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click  
  
End Sub  
private void button1_Click(object sender, System.EventArgs e)
{  
  
}  
private:  
  void button1_Click(System::Object ^ sender,  
    System::EventArgs ^ e)  
  {  
  
  }  

The first parameter,sender, provides a reference to the object that raised the event. The second parameter, e, in the example above, passes an object specific to the event that is being handled. By referencing the object's properties (and, sometimes, its methods), you can obtain information such as the location of the mouse for mouse events or data being transferred in drag-and-drop events.

Typically each event produces an event handler with a different event-object type for the second parameter. Some event handlers, such as those for the xref:System.Windows.Forms.Control.MouseDown and xref:System.Windows.Forms.Control.MouseUp events, have the same object type for their second parameter. For these types of events, you can use the same event handler to handle both events.

You can also use the same event handler to handle the same event for different controls. For example, if you have a group of xref:System.Windows.Forms.RadioButton controls on a form, you could create a single event handler for the xref:System.Windows.Forms.Control.Click event and have each control's xref:System.Windows.Forms.Control.Click event bound to the single event handler. For more information, see How to: Connect Multiple Events to a Single Event Handler in Windows Forms.

See also