Event Handling In .NET

Event Handling

Today, we will discuss a very important but confusing topic, which is, “event Handling”. NET. We can easily understand the "low-level" events like mouse click events 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 a 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 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.

Define event

Step 2. Define method.

Define method

.NET

Step 3. Bind the event to the method.

Bind the event

.NET tier

Step 4. Trigger the event.

Trigger the event

Trigger

Step 5. Output

Output

Yahoo

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

Forms application

Step 2. Add an MDI form.

MDI form

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

Image below

Step 4. Now, using the Load Form menu, load the form 1.

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 form. This button will trigger the event handler.

Event handler

Step 6. Now, we will add one class to that project with the name common. cs.

Project

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 the 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 form using the MDI menu option and then click the btnEvent button.

MDI menu option

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

Multiple forms

Block Diagram

Block diagram


Similar Articles