connectseekers.com

Friday, 23 May 2014

Connect Multiple Events to a Single Event Handler in Windows Forms Using C#.Net and VB.Net

5/23/2014 - By Pranav Singh 0

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#.



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


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top