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.
Step 2
Define method.
Step 3
Bind the event to method.
Step 4
Trigger the event.
Step 5
Output
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
Step 2
Add an MDI form.
Step 3
Now, modify the menu as shown in the image below.
Step 4
Now, using the Load Form menu, load the form1.
- using System;
- using System.Windows.Forms;
-
- namespace LearnEventHandling
- {
- public partial class MDIParent : Form
- {
- public MDIParent()
- {
- InitializeComponent();
- }
-
- private void ShowNewForm(object sender, EventArgs e)
- {
- Form1 frm = new Form1();
- frm.MdiParent = this;
- frm.Show();
- }
-
-
- private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
- {
- this.Close();
- }
-
- }
- }
Step 5
Add the btnEvent on form1. This button will trigger the event handler.
Step 6
Now, we will add one class in that project with the name common.cs.
Step 7
Add a public static event handler in the class, as shown below.
- using System;
-
- namespace LearnEventHandling
- {
- public static class Common
- {
- public static EventHandler speed;
- }
- }
Step 8
Now, go to MDI form code window and define the method. Then, bind it to the event handler.
- using System;
- using System.Windows.Forms;
-
- namespace LearnEventHandling
- {
- public partial class MDIParent : Form
- {
- public MDIParent()
- {
- InitializeComponent();
- Common.speed += speed;
- }
-
- void speed(object s, EventArgs e)
- {
- MessageBox.Show("Speed Reached to 100 KM/H");
-
- }
-
- private void ShowNewForm(object sender, EventArgs e)
- {
- Form1 frm = new Form1();
- frm.MdiParent = this;
- frm.Show();
- }
-
- private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
- {
- this.Close();
- }
-
- }
- }
Step 9
Finally, we will trigger the event using form1 btnEvent.
- using System;
- using System.Windows.Forms;
-
- namespace LearnEventHandling
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void btnEvent_Click(object sender, EventArgs e)
- {
- Common.speed(sender, e);
- }
- }
- }
Step 10
Now, run the application and load form1 using MDI menu option and then click the btnEvent button.
Similarly, we can add multiple forms in the project and call them anywhere.
Block Diagram