Home arrow C# arrow Page 4 - Event Handling With C#

Event Handling With C#

C#'s event handling mechanisms are powerful and don't have to be complicated. In this article Deepak introduces us to the concepts of delegates, event handlers and GUI event handling using C#.

Author Info:
By: Deepak Dutta
Rating: 4 stars4 stars4 stars4 stars4 stars / 30
July 23, 2002
  1. · Event Handling With C#
  2. · Delegates in C#
  3. · Event Handlers in C#
  4. · GUI Event Handling
  5. · Conclusion

print this article

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(){
private void initializeComponents(){
m_nameLabel=new Label();
m_nameButton = new Button();
m_clearButton = new Button();


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

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

blog comments powered by Disqus

- 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#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials