SunQuest
 
       C#
  Home arrow C# arrow Page 2 - C# Events Explained
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# Events Explained
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 41
    2007-02-13

    Table of Contents:
  • C# Events Explained
  • The Windows Form Example
  • The Steps of Creating an Event
  • The Bookshop Class and the AddBookEventHandler Delegate
  • The Notifier and the Sys Classes
  • Event Without an Event

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    C# Events Explained - The Windows Form Example


    (Page 2 of 6 )

    Copy the following code into Notepad and save it as windowsapp.cs in Drive C:

    using System;
    using System.Windows.Forms;

    public class SimpleForm: Form
    {
      private Button button1;

      public SimpleForm()
      {
        this.button1 = new Button();
        this.button1.Name = "button1";
        this.button1.Text = "Click Here";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        this.Controls.Add(this.button1);
      }

      private void button1_Click(object sender, System.EventArgs e)
      {
        MessageBox.Show("You clicked the button");
      }

      static void Main( )
      {
        Application.Run(new SimpleForm( ));
      }
    }

    Compile the code using the C# compiler with the command: csc /t:winexe windowsapp.cs

    Double click on the file windowsapp.exe then click on the button of the form, and you will get a message box as shown in the next figure:

    The SimpleForm class inherits the System.Windows.Forms.Form class which represents a window. The Main method of this class begins running a message loop then makes the form visible, using the method Application.Run(). Actually we are not interested in the code of the Main method because it's beyond the scope of this article. The Constructor of the SimpleForm class creates an instance of the class System.Windows.Forms.Button and sets some properties (Name and Text), then Subscribes to the Click event of this button using the += operator. As we have said before, the implementers of the button class don't know which method of which object needs to be notified of the occurrence of the click event.

    Through the use of delegates they guarantee that a watcher object (like a SimpleForm instance) defines a method, an event handler method, then creates an instance of a specific delegate type that encapsulates this method and assigns (subscribes) the delegate instance to the Multicast delegate maintained by the click event. As you can see in the code, we assign a new instance of the delegate type System.EventHandler to the click event of the button1 object. The EventHandler delegate encapsulates methods that accepts two parameters, the first of type object and the second of type EventArgs, and return void.

    public delegate void EventHandler(Object sender, EventArgs e);

    The EventArgs class is the base class for the classes that carry event data. You may not understand how useful this mechanism is until you complete the article.

    The method button1_Click matches the signature of the delegate so we use it to subscribe to the Click event through the EventHandler delegate instance. When you click on the button the click event takes place, which invokes its multicast delegate which in turn calls the subscribers' event handler methods like our button1_Click, which shows a message box. Let's discuss how we can create our own event.

    More C# Articles
    More By Michael Youssef


       · I work with C# for 9 months now and I couldn't understand events until I read your...
       · I always liked Michael intuitive writing style about various .NET topics and this...
       · Thanks for your comments about my article. Actually, each and every one of us who...
       · Mr.Michael I haven't read any of your articles and my first one was C# events. I'm...
       · I'm happy that you liked my articles and yes I plan to write about ASP.NET 2.0 very...
       · I work with different programming languages, and never meet such understanddable...
       · Glad you liked my articles. Could you please tell me what are those topics you want...
       · First, fantastic article. The best explanation of events that I have read to...
       · Hi,Thanks for your comment, I tried to do my best explaining those basics to...
       · Thanks Michael,That is what I was asking. I did not realise that there would...
       · You are welcome
       · Excellent article as well as the article on delegates which is a must read. Can you...
       · Thanks, I hope I can write more articles about C# soon. Anyway, I'm writing more...
       · You might also want to read my articles about C# Properties and Indexers. You will...
     

    C# ARTICLES

    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained

    BlackBerry VTS




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway