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

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Java is to JavaScript what Car is to Carpet.” - Chris Heilmann