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 that were introduced in classic VB-6 or GUI in the late ’90s. However, this will make us obsolete in the 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 the 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 an 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.     public partial class MDIParent: Form {  
    6.         public MDIParent() {  
    7.             InitializeComponent();  
    8.         }  
    9.   
    10.         private void ShowNewForm(object sender, EventArgs e) {  
    11.             Form1 frm = new Form1();  
    12.             frm.MdiParent = this;  
    13.             frm.Show();  
    14.         }  
    15.   
    16.         private void ExitToolsStripMenuItem_Click(object sender, EventArgs e) {  
    17.             this.Close();  
    18.         }  
    19.     }  

      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.     public static class Common {  
        5.         public static EventHandler speed;  
        6.     }  

          Step 8
           
          Now, go to the 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.     public partial class MDIParent: Form {  
            6.         public MDIParent() {  
            7.             InitializeComponent();  
            8.             Common.speed += speed;  
            9.         }  
            10.   
            11.         void speed(object s, EventArgs e) {  
            12.             MessageBox.Show("Speed Reached to 100 KM/H");  
            13.         }  
            14.   
            15.         private void ShowNewForm(object sender, EventArgs e) {  
            16.             Form1 frm = new Form1();  
            17.             frm.MdiParent = this;  
            18.             frm.Show();  
            19.         }  
            20.   
            21.         private void ExitToolsStripMenuItem_Click(object sender, EventArgs e) {  
            22.             this.Close();  
            23.         }  
            24.     }  

              Step 9
               
              Finally, we will trigger the event using form1 btnEvent.
                1. using System;  
                2. using System.Windows.Forms;  
                3.   
                4. namespace LearnEventHandling {  
                5.     public partial class Form1: Form {  
                6.         public Form1() {  
                7.             InitializeComponent();  
                8.         }  
                9.   
                10.         private void btnEvent_Click(object sender, EventArgs e) {  
                11.             Common.speed(sender, e);  
                12.         }  
                13.     }  

                  Step 10
                   
                  Now, run the application and load form1 using the MDI menu option and then click the btnEvent button.
                   
                  .NET
                   
                  Similarly, we can add multiple forms to the project and call them anywhere.
                   
                  .NET
                   
                  Block Diagram
                   
                  .NET

                  Recommended Ebook

                  Diving Into Microsoft .NET Entity Framework

                  Download Now!
                  Similar Articles