Tuesday 10 June 2014

Create own button click event in C#.net

Create a windows form (frmClickEvent)

Drag and drop the button inside the form and name it as btnExisting



 Double click the button(btnExisting) you will find the below code



 Click  is an Event
 btnExisting_Click(object sender,EventArgs e)  is an Method

In the form designer page you will find the below code(frmClickEvent.Designer.cs



 Click  is an Event Name
 EventHandler  is a Delegate  (Holds methods reference[this.btnExisting_Click])

Create a user control (MyButton)


In the code behind(MyButton.cs) you will find the below code initially



Modify the code (MyButton.cs) as below

namespace Click_Event
{
    public partial class MyButton :Button
    {
        public delegate void MyEventHandler(object sender, EventArgs e);
        public event MyEventHandler MyEvent;
        public MyButton()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Override the Onclick event
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClick(EventArgs e)
        {
            MyEvent(this, e);
        }
        /// <summary>
        /// Add new property
        /// </summary>
        public Color MyColor
        {
            get
            {
                return this.BackColor;
            }
            set
            {
                this.BackColor = value;
            }
        }
    }
}

MyColor-->Property
OnClick --> Click Event

When you built the project  ,will find the error in the below line

Delete that line and rebuild again




Now MyButton appears in the Toolbox  

Drag and Drop , name it as btnUserControl



Select the button(btnUserControl) and view the properties

you will find the new property (MyColor) highlighted below



 Double click the button(btnUserControl) you will find the below code




 Click  is an event name of the existing button(btnExisting)

 To achieve event name of user control button(btnUserControl) we need to add [DefaultEvent("MyEvent")] before the   class which is shown below





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Click_Event
{
    [DefaultEvent("MyEvent")]
    public partial class MyButton :Button
    {
        public delegate void MyEventHandler(object sender, EventArgs e);
        public event MyEventHandler MyEvent;
        public MyButton()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Override the Onclick event
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClick(EventArgs e)
        {
            MyEvent(this, e);
        }
        /// <summary>
        /// Add new property
        /// </summary>
        public Color MyColor
        {
            get
            {
                return this.BackColor;
            }
            set
            {
                this.BackColor = value;
            }
        }
    }
}

 
Now delete  the button(btnUserControl) in the form and again add user control button(MyButton) with the same name(btnUserControl)

 Double click the button(btnUserControl) you will find the below code





  MyEvent --> Event Name mentioned in the DefaultEvent("MyEvent")

Now build and run the project your user control will fire its own event.This is how i created my own button click event in windows form

2 comments:

181 said...

thanks for the clear explanation...Keep rocking

Anonymous said...

Good post