Powered by Blogger.

Friday, July 16, 2010

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";
}
}
}



Download


 Download source code for Handling a buttons Event in C#
 


by Abhisek 

No comments:

Post a Comment

  ©Template by Dicas Blogger.

TOPO