C#
  Home arrow C# arrow Page 4 - Event Handling With C#
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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#

Event Handling With C#
By: Deepak Dutta
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 23
    2002-07-23

    Table of Contents:
  • Event Handling With C#
  • Delegates in C#
  • Event Handlers in C#
  • GUI Event Handling
  • Conclusion

  • 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


    Event Handling With C# - GUI Event Handling


    (Page 4 of 5 )

    Event handling in Windows Forms (the .NET framework that supports GUI applications) employs 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 the 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 the 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):

    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());
    }
    }

    More C# Articles
    More By Deepak Dutta


       · Author says "Declare a delegate object with a signature that exactly matches the...
     

    C# ARTICLES

    - Introduction to Objects and Classes in C#, P...
    - Visual C#.NET, Part 1: Introduction to Progr...
    - C# - An Introduction
    - Hotmail Exposed: Access Hotmail using C#
    - Razor Sharp C#
    - Introduction to Objects and Classes in C#
    - Making Your Code CLS Compliant
    - Programming with MySQL and .NET Technologies
    - Socket Programming in C# - Part II
    - Socket Programming in C# - Part I
    - Creational Patterns in C#
    - Type Conversions
    - Creating Custom Delegates and Events in C#
    - Inheritance and Polymorphism
    - Understanding Properties in C#







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek