Event Handling In .NET

Event Handling

Today, we will discuss a very important but confusing topic, that is, “event Handling” in .NET.  We can easily understand the "low-level" events like mouse click event or mouse move over events etc. but when we talk about Event Handling in .NET, some difficult concepts like delegates confuse us. Especially, those who don't have a strong OOP background or those who have never implemented OOP modules in the real programming environment, get very confused. So, I will not use the delegate word.

We can perform our daily programming tasks without understanding “.NET Event Handling” easily, using old techniques or using low-level events which were introduced in classic VB-6 or GUI in late 90’s. However, this will make us obsolete in next few years. So, it’s very important to learn and implement at least simple event handling.

Why do we use Event Handling?

  • Reduce programming code.
  • Better accuracy
  • Reusability of an event at multiple places throughout projects.
  • Trigger events without using low-level events.

Let's learn event handling taking a real-world example where we want to achieve a speed of 100 Km/h of the car.

Step 1

Define event.

 .NET 

Step 2

Define method.

.NET .NET 

Step 3

Bind the event to method.

.NET.NET 

Step 4

Trigger the event.

.NET.NET 

Step 5

Output

.NET.NET


Now, we will apply the same concept in C# using Visual Studio 2015.

Note

We will use MDI form with two child forms.

Now, open a new project using Windows.Forms application

Step 1

.NET

Step 2

Add an MDI form.

.NET

Step 3

Now, modify the menu as shown in the image below.

.NET

Step 4

Now, using the Load Form menu, load the form1.

  1. using System;  
  2. using System.Windows.Forms;  
  3.    
  4. namespace LearnEventHandling  
  5. {  
  6.     public partial class MDIParent : Form  
  7.     {  
  8.         public MDIParent()  
  9.         {  
  10.             InitializeComponent();  
  11.         }  
  12.           
  13.         private void ShowNewForm(object sender, EventArgs e)  
  14.         {  
  15.             Form1 frm = new Form1();  
  16.             frm.MdiParent = this;  
  17.             frm.Show();  
  18.         }  
  19.    
  20.           
  21.         private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)  
  22.         {  
  23.             this.Close();  
  24.         }  
  25.           
  26.     }  
  27. }  

Step 5

Add the btnEvent on form1. This button will trigger the event handler.

.NET

Step 6

Now, we will add one class in that project with the name common.cs.

.NET

Step 7

Add a public static event handler in the class, as shown below.

  1. using System;  
  2.    
  3. namespace LearnEventHandling  
  4. {  
  5.     public static class Common  
  6.     {  
  7.         public static EventHandler speed;  
  8.     }  

Step 8

Now, go to MDI form code window and define the method. Then, bind it to the event handler.

  1. using System;  
  2. using System.Windows.Forms;  
  3.    
  4. namespace LearnEventHandling  
  5. {  
  6.     public partial class MDIParent : Form  
  7.     {  
  8.         public MDIParent()  
  9.         {  
  10.             InitializeComponent();  
  11.             Common.speed += speed;  
  12.         }  
  13.    
  14.         void speed(object s, EventArgs e)  
  15.         {  
  16.             MessageBox.Show("Speed Reached to 100 KM/H");  
  17.    
  18.         }  
  19.    
  20.         private void ShowNewForm(object sender, EventArgs e)  
  21.         {  
  22.             Form1 frm = new Form1();  
  23.             frm.MdiParent = this;  
  24.             frm.Show();  
  25.         }  
  26.    
  27.         private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)  
  28.         {  
  29.             this.Close();  
  30.         }  
  31.    
  32.     }      
  33. }

Step 9

Finally, we will trigger the event using form1 btnEvent.

  1. using System;  
  2. using System.Windows.Forms;  
  3.    
  4. namespace LearnEventHandling  
  5. {  
  6.     public partial class Form1 : Form  
  7.     {  
  8.         public Form1()  
  9.         {  
  10.             InitializeComponent();  
  11.         }  
  12.    
  13.         private void btnEvent_Click(object sender, EventArgs e)  
  14.         {  
  15.             Common.speed(sender, e);  
  16.         }  
  17.     }  

Step 10

Now, run the application and load form1 using MDI menu option and then click the btnEvent button.

.NET

Similarly, we can add multiple forms in the project and call them anywhere.

.NET

Block Diagram

.NET