Working with events in C#
First create a EventClass class.Define ur delegate and event here.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fireevent
{
public class EventClass
{
here is ur delegate
public delegate void MYDelegate();
here is ur event, named same as ur delegate
public event MYDelegate Fire;
method for checking that event is null or not and then call the event
public void show()
{
if (Fire != null)
Fire();
}
}
}
After this create another class in which u want to fire ur event.Here iam taking Form1 class and i am going to fire my event on page load.
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 Fireevent
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
create object of ur class EventClass and then call ur fire event and function show();
EventClass ec = new EventClass();
ec.Fire += new EventClass.MYDelegate(ec_Fire);
ec.show();
After this controller will go to void ec_Fire()
}
void ec_Fire()
{
}
}
}
Enjoy N FIRE EVENT...................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fireevent
{
public class EventClass
{
here is ur delegate
public delegate void MYDelegate();
here is ur event, named same as ur delegate
public event MYDelegate Fire;
method for checking that event is null or not and then call the event
public void show()
{
if (Fire != null)
Fire();
}
}
}
After this create another class in which u want to fire ur event.Here iam taking Form1 class and i am going to fire my event on page load.
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 Fireevent
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
create object of ur class EventClass and then call ur fire event and function show();
EventClass ec = new EventClass();
ec.Fire += new EventClass.MYDelegate(ec_Fire);
ec.show();
After this controller will go to void ec_Fire()
}
void ec_Fire()
{
}
}
}
Enjoy N FIRE EVENT...................
Labels: Working with events in C#
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home