Wednesday, 21 May 2014

Event Handling in windows programming using C# | How To Add Events at run time in windows application controls using C#.Net and VB.Net

5/21/2014 - By Pranav Singh 0

Windows application event handling is used to perform different type of operation like click event, mouse click events and many more. So in this article I will show you how you can dynamically add the different events on your button control.

In this I have covered mouse event and button click event. In this I have used C#.Net, But I will provide code for VB.Net also.


So here is the code to add event at run time in your button.

public Form1()
        {
            InitializeComponent();
            //mouse down event
            this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDown);
            //mouse move event
            this.button1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMove);
            //button click event
            this.button1.Click += new System.EventHandler(this.OnButtonclick);
        }

The above written piece of code is used for adding mousedown, mousemove and button click event of the button.

So here is the complete code

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;

namespace Eventhandler
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //mouse down event
            this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDown);
            //mouse move event
            this.button1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMove);
            //button click event
            this.button1.Click += new System.EventHandler(this.OnButtonclick);
        }
        public void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            lblmessage1.Text = "Mouse Down Event Occur";
        }
        public void OnMouseMove(object sender, System.EventArgs  e)
        {
            lblmessage.Text = "Mouse Move Event Occur";
        }
        public void OnButtonclick(object sender, System.EventArgs e)
        {
            lblmessage2.Text = "Click Event Occur";
        }
    }
}

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

Namespace Eventhandler
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
            'mouse down event
            Me.button1.MouseDown += New System.Windows.Forms.MouseEventHandler(AddressOf Me.OnMouseDown)
            'mouse move event
            Me.button1.MouseMove += New System.Windows.Forms.MouseEventHandler(AddressOf Me.OnMouseMove)
            'button click event
            Me.button1.Click += New System.EventHandler(AddressOf Me.OnButtonclick)
        End Sub
        Public Overloads Sub OnMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            lblmessage1.Text = "Mouse Down Event Occur"
        End Sub
        Public Overloads Sub OnMouseMove(ByVal sender As Object, ByVal e As System.EventArgs)
            lblmessage.Text = "Mouse Move Event Occur"
        End Sub
        Public Sub OnButtonclick(ByVal sender As Object, ByVal e As System.EventArgs)
            lblmessage2.Text = "Click Event Occur"
        End Sub
    End Class
End Namespace

Now run the application



DOWNLOAD



Tags: , ,
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

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


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top