Skip to content

Latest commit

 

History

History
62 lines (43 loc) · 3.2 KB

how-to-connect-multiple-events-to-a-single-event-handler-in-windows-forms.md

File metadata and controls

62 lines (43 loc) · 3.2 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
How to: Connect Multiple Events to a Single Event Handler
Learn how to connect multiple events to a single event handler in Windows Forms by using the Events view of the Properties window in C#.
03/30/2017
vb
events [Windows Forms], connecting multiple to single event handler
event handlers [Windows Forms], connecting events to
menus [Windows Forms], event-handling methods for multiple menu items
Windows Forms controls, events
menu items [Windows Forms], multicasting event-handling methods
5a20749a-41b5-4acc-8eb1-9e5040b0a2c4

How to: Connect Multiple Events to a Single Event Handler in Windows Forms

In your application design, you may find it necessary to use a single event handler for multiple events or have multiple events perform the same procedure. For example, it is often a powerful time-saver to have a menu command raise the same event as a button on your form does if they expose the same functionality. You can do this by using the Events view of the Properties window in C# or using the Handles keyword and the Class Name and Method Name drop-down boxes in the Visual Basic Code Editor.

To connect multiple events to a single event handler in Visual Basic

  1. Right-click the form and choose View Code.

  2. From the Class Name drop-down box, select one of the controls that you want to have the event handler handle.

  3. From the Method Name drop-down box, select one of the events that you want the event handler to handle.

  4. The Code Editor inserts the appropriate event handler and positions the insertion point within the method. In the example below, it is the xref:System.Windows.Forms.Control.Click event for the xref:System.Windows.Forms.Button control.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
    ' Add event-handler code here.  
    End Sub  
  5. Append the other events you would like handled to the Handles clause.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click  
    ' Add event-handler code here.  
    End Sub  
  6. Add the appropriate code to the event handler.

To connect multiple events to a single event handler in C#

  1. Select the control to which you want to connect an event handler.

  2. In the Properties window, click the Events button (Events Button).

  3. Click the name of the event that you want to handle.

  4. In the value section next to the event name, click the drop-down button to display a list of existing event handlers that match the method signature of the event you want to handle.

  5. Select the appropriate event handler from the list.

    Code will be added to the form to bind the event to the existing event handler.

See also