Handling a buttons Event in C#

Abhisek
Posted by in C# category on for Beginner level | Views : 6093 red flag

This article describes about the events and shows how to handle different events of a button in C# with a simple example.


 Download source code for Handling a buttons Event in C#

EVENTS IN C#:-


Windows form controls generate the events.These events are usually associated with user actions,e.g.When the user clicks a button,that button generates an event indicating what just happened to its.Handling the event is the means by which the programmer can provide some functionality for that button.

The "control" class defines a number of events that are common to many controls.

How to handle an event:-

There are three basic ways to handle a particular event.The first way is to double click a control,which takes you to the event handler for the control's default event.This event is different for different controls,if that is the event you want then it is fine.If you want an event another than the default,then you have two possible ways to proceed.

One way is to use the Events list in the properties window, which is displayed when you click the lightning bold button. The greyed event is the controls default event. To add a handler for a particular event, double click that event in the event list and the code to subscribe the event is generated along with the method signature to handle the events. Alternatively you can type a name for the method to handle the particular event next to that event and when you press enter the handle will be generated with your chosen name.

Another option is to add the code to subscribe to the event yourself-do this by adding the code to the form's constructor after the Initialize Component() call.

Each of these two options requires two steps -subscription to the event and the correct signature for the method handler. If you double click a control and try to handle another event by editing the method signature of the default event, then you will fail. You also need to alter the event subscription code in "Initialize Component()", So this is not a quick method.


Check The Following Code Snippet


Create a windows form application using C#. It shows how to handle an event of a button. It uses events like mouse enter, mouse leave, Click, etc..

There is a form with a button and a label which shows the event we are dealing with.


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 WindowsFormsApplication1
{
public partial class Events : Form
{
public Events()
{
InitializeComponent();
}

private void btnEvents_Click(object sender, EventArgs e)
{
lblEvents.Text = "Button is clicked";
}

private void btnEvents_Enter(object sender, EventArgs e)
{
lblEvents.Text = "Button is Active";
}

private void btnEvents_Leave(object sender, EventArgs e)
{
lblEvents.Text = "Button is Inactive";
}

private void btnEvents_MouseEnter(object sender, EventArgs e)
{
lblEvents.Text = "Mouse is on button";
}

private void btnEvents_MouseLeave(object sender, EventArgs e)
{
lblEvents.Text = "Mouse is on form";
}

private void Events_Resize(object sender, EventArgs e)
{
lblEvents.Text = "resizing the form";
}
}
}


I have attached the code you can also download it.
Page copy protected against web site content infringement by Copyscape

About the Author

Abhisek
Full Name: Abhisek Panda
Member Level: Bronze
Member Status: Member
Member Since: 10/11/2009 6:25:59 AM
Country: India
Abhisek Panda

Thanks and Regards Abhisek Panda Go Green And Save Your Future

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)