Using single event handler for multiple control’s event in Windows Forms and ASP.NET using C#, VB.NET

Introduction:
In this post I will explain how to use single event handler for click event of multiple Button controls in Windows Forms and ASP.NET using C# and VB.NET

Description:

Event handling in Windows Forms

Create a new Windows Forms application and four buttons on the form, “btnAdd”, “btnSubtract”, “btnMultiply” and “btnDivide”.

Write following event handler in the code behind of form

C#:

private void btn_Click(object sender, EventArgs e)
{
     Button btn = (Button)sender;
     switch (btn.Name)
     {
          case "btnAdd":
               // Performs add
               break;
          case "btnSubtract":
               // Performs subtract
               break;
         case "btnMultiply":
               // Performs multiply
               break;
         case "btnDivide":
               // Performs divide
               break;
     }
}

VB.NET:

Protected Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _
btnSubtract.Click, btnMultiply.Click, btnDivide.Click, btnAdd.Click
     Dim btn As Button = sender
     Select Case btn.Name
          Case ("btnAdd")
               'Performs add
          Case ("btnSubtract")
               'Performs subtract
          Case ("btnMultiply")
               'Performs multiply
          Case ("btnDivide")
               'Performs divide
     End Select
End Sub

Assign this event handler (btn_Click) to the click events of all four buttons using property box. Right click on the button and select “Properties”, go to Events tab and select “btn_Click” from the dropdown next to the “Click” event

You can also assign events to a control using form’s code behind file or Designer file in C#. For example you can write following code inside form’s load event to assign events to buttons in C#.

C#:

private void Form1_Load(object sender, EventArgs e)
{
     this.btnAdd.Click += new System.EventHandler(this.btn_Click);
     this.btnSubtract.Click += new System.EventHandler(this.btn_Click);
     this.btnMultiply.Click += new System.EventHandler(this.btn_Click);
     this.btnDivide.Click += new System.EventHandler(this.btn_Click);
}

In VB.NET, event is assigned to a control using “Handles” keyword in the event handler signature. “Handles” keyword is followed by a comma separated list of “<ControlName>.<Event>”

Event handling in ASP.NET

In ASP.NET, write following in the HTML source file design a form and write following for the buttons

&lt;asp:Button ID=&quot;btnAdd&quot; runat=&quot;server&quot; Text=&quot;+&quot; OnClick=&quot;btn_Click&quot; /&gt;
&lt;asp:Button ID=&quot;btnSubtract&quot; runat=&quot;server&quot; Text=&quot;-&quot; OnClick=&quot;btn_Click&quot; /&gt;
&lt;asp:Button ID=&quot;btnMultiply&quot; runat=&quot;server&quot; Text=&quot;*&quot; OnClick=&quot;btn_Click&quot; /&gt;
&lt;asp:Button ID=&quot;btnDivide&quot; runat=&quot;server&quot; Text=&quot;/&quot; OnClick=&quot;btn_Click&quot; /&gt;

Write following in the code behind file

C#:

private void btn_Click(object sender, EventArgs e)
{
     Button btn = (Button)sender;
     switch (btn.ID)
     {
          case &quot;btnAdd&quot;:
               // Performs add
               break;
          case &quot;btnSubtract&quot;:
               // Performs subtract
               break;
          case &quot;btnMultiply&quot;:
               // Performs multiply
               break;
          case &quot;btnDivide&quot;:
               // Performs divide
               break;
     }
}

VB.NET:

Protected Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs)
     Dim btn As Button = sender
     Select Case btn.ID
          Case (&quot;btnAdd&quot;)
               'Performs add
          Case (&quot;btnSubtract&quot;)
               'Performs subtract
          Case (&quot;btnMultiply&quot;)
               'Performs multiply
          Case (&quot;btnDivide&quot;)
               'Performs divide
     End Select
End Sub

In ASP.NET, we use button’s ID property to compare in the switch case instead of button’s Name property like in Windows Forms.

In ASP.NET with VB, we have flexibility while assigning event handler to a control. Here, either we can use “Handles” keyword to assign the event handler like in a Windows Forms application or we can just assign it through “OnClick” attribute in the HTML source.

Leave a Reply

Your email address will not be published. Required fields are marked *