Showing posts with label event handler in c#. Show all posts
Showing posts with label event handler in c#. Show all posts

Sunday, April 3, 2016

What is event in c#?



Events are the members of the class that rises when something happens a class, which have a message that contain the information about the event, Event handler is a method that contain the same signature as the event and this method is execute when the event occurs.

When we define an event then first define a delegate that contains the methods that will be called when the event raised , the example of the event is given below : 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication11
{
    public delegate void mydel();
   public  class Class1
    {
        public event mydel myevent;
        public void fire()
    {
        myevent();
    }
    }
}

.cs code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            obj.myevent+=new mydel(obj_myevent);
            obj.fire();
        }
        void obj_myevent();
    {
        MessageBox.show("work done");
    }
    }

Output :
work done