what is event in c# code example
Example 1: c# events
class Publisher
{
public delegate void MyEventHandler (object source, EventArgs args);
public event MyEventHandler myEvents;
protected virtual void Method1()
{
if(myEvents != null)
{
myEvents(this, EventArgs.Empty);
}
}
public void Method2()
{
Console.WriteLine("This method is called from the Main function...");
Method1();
}
}
public class Subscriber
{
public void Method1(object source, EventArgs e)
{
Console.WriteLine("The work done by the subscriber after the event was raised...");
}
}
class Program
{
static void Main(string[] args)
{
var myPublisher = new Publisher();
var mySubscriber = new Subscriber();
myPublisher.myEvents += mySubscriber.Method1;
myPublisher.Method2();
}
}
public class MyNewEventArgs : EventArgs
{
public string newData {get; set;}
}
public delegate void EventHandler (object source, MyNewEventArgs args);
myEvents(this, new MyNewEventArgs() { newData = "Hello World!"} );
public void Method1(object source, MyNewEventArgs e)
Example 2: c# event
private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
}
private void panel1_MouseEnter(object sender, System.EventArgs e)
private void panel1_MouseHover(object sender, System.EventArgs e)
private void panel1_MouseLeave(object sender, System.EventArgs e)
private void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
private void panel1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
private void clearButton_Click(object sender, System.EventArgs e)
0