Library tutorials & articles

Event Handling in .NET Using C#

GUI Event Handling

Event handling in Windows Forms (.NET frame work that supports GUI application) employ the .NET event handling model described earlier. We will now apply that model to write a simple application. The application has one class, MyForm, derived from System.Windows.Forms.Form class. Class MyForm is derived from Form class. If you study the code and the three comment lines, you will observe that you do not have to declare the delegates and reference those delegates using event keyword because the events (mouse click, etc.) for the GUI controls (Form, Button, etc.) are already available to you and the delegate is System.EventHandler. However, you still need to define the method, create the delegate object (System.EventHandler) and plug in the method, that you want to fire in response to the event (e.g. a mouse click), into the delegate object.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class MyForm  Form{
   private Button m_nameButton;
   private Button m_clearButton;
   private Label  m_nameLabel;

   private Container m_components = null;
   
   public MyForm(){
       initializeComponents();
   }
   private void initializeComponents(){
       m_nameLabel=new Label();
       m_nameButton = new Button();
       m_clearButton = new Button();

       SuspendLayout();

       m_nameLabel.Location=new Point(16,16);
       m_nameLabel.Text="Click NAME button, please";
       m_nameLabel.Size=new Size(300,23);

       m_nameButton.Location=new Point(16,120);
       m_nameButton.Size=new Size(176, 23);
       m_nameButton.Text="NAME";
       //Create the delegate, plug in the method, and attach the delegate to the Click event of the button
       m_nameButton.Click += new System.EventHandler(NameButtonClicked);

       m_clearButton.Location=new Point(16,152);
       m_clearButton.Size=new Size(176,23);
       m_clearButton.Text="CLEAR";
       //Create the delegate, plug in the method, and attach the delegate to the Click event of the button
       m_clearButton.Click += new System.EventHandler(ClearButtonClicked);

       this.ClientSize = new Size(292, 271);
       this.Controls.AddRange(new Control[] {m_nameLabel,m_nameButton,m_clearButton});
       this.ResumeLayout(false);
   }
   //Define the methods whose signature exactly matches with the declaration of the delegate
   private void NameButtonClicked(object sender, EventArgs e){
       m_nameLabel.Text="My name is john, please click CLEAR button to clear it";
   }
   private void ClearButtonClicked(object sender,EventArgs e){
       m_nameLabel.Text="Click NAME button, please";
   }
   public static void Main(){
       Application.Run(new MyForm());
   }
}

Comments

  1. 25 Apr 2007 at 04:18

    While it's very nice to know the "How" when explaining syntax, more importantly is they "why" in terms of practical real-life use within a C# application.  It would be nice of you would have included some true examples of when you would need to use one and why.

  2. 24 Nov 2005 at 15:04

    This is one of the best articles i have come across on Delegates and Event.. Good Job!

  3. 02 Jun 2004 at 12:03

    Line:


    public class MyEventArgs  EventArgs {


    should read:


    public class MyEventArgs : EventArgs {


    Otherwise, great article!

  4. 22 Apr 2004 at 17:24

    Can anyone help me pls!!!


    I am facing a big problem to code the gui buttons!! I am doing a C# project. this project consists of a telephone graphical user interface!!


    can anyone tell me what can i do to code the number buttons to display the telephone number into a textfield...


    I need this urgent plsssss!!!!!!


    Thanks for your help!!!!!!

  5. 28 Jul 2003 at 16:32

    I need to know how can I trap an Outlook Event (Saving an Item) using C#. Any example will be highly appreciated.


    Thanks in advance!!

  6. 12 Jul 2003 at 03:52

    How can I create asynchrous events?
    Using delegates and events I get synchronous calls?
    Please advise.

  7. 28 Apr 2003 at 17:34
    Hi,

    I am using certain, COM Components, I need to know the delegate signature, to create event handler. Is there any tools to do this? or if I need to do manually which is the easiast way to know the exact signature of those events raised by the COM components.
  8. 20 Mar 2003 at 06:56

    Very good example. it can be better hadling browser events, with .NET of course.

  9. 01 Jan 1999 at 00:00

    This thread is for discussions of Event Handling in .NET Using C#.

Leave a comment

Sign in or Join us (it's free).

Deepak Dutta

Related discussion

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

Events coming up

  • Dec 7

    An Event Apart San Francisco 2009

    San Francisco, United States

    From the makers of A List Apart: An Event Apart is an intensely educational two-day conference for passionate practitioners of standards-based web design. If you care about code as well as content, usability as well as design, An Event Apart is th...

We'd love to hear what you think! Submit ideas or give us feedback