.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Handiling a button's event

Posted By:Abhisek Panda       Posted Date: October 27, 2009    Points: 25    Category: C#    URL: http://www.dotnetspark.com  
 

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 Initialise 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";
}
}
}
You can download the sample project.
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend