An event is an action which respond handle in code. Events
can be generated by a user action, such as clicking the mouse or pressing a
key; by program code; or by the system.
Here in this article i will show you how you can How to
connect multiple events to a single event handler in windows forms using c#.
This will help you to dynamically assign the single button click event to
multiple buttons in your windows application using C#.Net and VB.Net.
In my previous article I have shown Event
Handling in windows programming using C# | How To Add Events at run time in
windows application controls using C#.Net and VB.Net. Now in this article I
will show you how to connect multiple events to a single event handler in
windows forms using c#.
Some of the eBooks which you may
like to download for asp.net: Free
Download eBook for Fast ASP.NET Websites Development | Fast ASP.NET Websites
Online Free eBook , Professional
ASP.NET 4.5 in C# and VB - Free download eBooks .
So for this article her is the code. First create a new windows application as shown blow:
C#.Net
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace ProjectDemo_Windows
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
this.button1.Click
+= new System.EventHandler(this.button1_Click);
this.button2.Click
+= new System.EventHandler(this.button1_Click);
}
private
void button1_Click(object
sender, EventArgs e)
{
Button
_objbutton = (Button)sender;
lbmessage.Text = "You have clicked :" +
_objbutton.Name;
}
}
}
|
Here is the code responsible for
operation
public Form1()
{
InitializeComponent();
this.button1.Click
+= new System.EventHandler(this.button1_Click);
this.button2.Click
+= new System.EventHandler(this.button1_Click);
}
|
VB.Net
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Collections
Namespace ProjectDemo_Windows
Partial Public Class Form1
Inherits
Form
Public
Sub New()
InitializeComponent()
Me.button1.Click
+= New System.EventHandler(AddressOf Me.button1_Click)
Me.button2.Click
+= New System.EventHandler(AddressOf Me.button1_Click)
End Sub
Private
Sub button1_Click(ByVal
sender As Object,
ByVal e As
EventArgs)
Dim
_objbutton As Button = DirectCast(sender, Button)
lbmessage.Text = "You have clicked :" &
_objbutton.Name
End Sub
End Class
End Namespace
|
Her e below mention code is
responsible for performing the operation
Public Sub New()
InitializeComponent()
Me.button1.Click
+= New System.EventHandler(AddressOf Me.button1_Click)
Me.button2.Click
+= New System.EventHandler(AddressOf Me.button1_Click)
End Sub
|
Now run the application:
DOWNLOAD
0 comments:
Please let me know your view