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

By | May 24, 2013

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.Text)
    {
        case "+":
            // Performs add
            break;
        case "-":
             // Performs subtract
            break;
        case "*":
            // Performs multiply
            break;
        case "/":
            // 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.Text
        Case ("+")
            MsgBox(btn.Text)
            'Performs add
        Case ("-")
            'Performs subtract
        Case ("*")
            'Performs multiply
        Case ("/")
            '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

<asp:Button ID="btnAdd" runat="server" Text="+" OnClick="btn_Click" />
<asp:Button ID="btnSubtract" runat="server" Text="-" OnClick="btn_Click" />
<asp:Button ID="btnMultiply" runat="server" Text="*" OnClick="btn_Click" />
<asp:Button ID="btnDivide" runat="server" Text="/" OnClick="btn_Click" />

Write following in the code behind file

C#:

private void btn_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    switch (btn.ID)
    {
        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)
    Dim btn As Button = sender
    Select Case btn.ID
        Case ("btnAdd")
            'Performs add
        Case ("btnSubtract")
            'Performs subtract
        Case ("btnMultiply")
            'Performs multiply
        Case ("btnDivide")
            '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 *