T
tshad
I have a sample program here that has one event with 2 different event
handlers (onButtonAction and onButtonAction2).
I had defined the event as:
public event ButtonEventHandler ButtonClick;
where ButtonEventHandler is a delegate type with 2 parameters: one is an
object and the other is an int.
I then do:
b.ButtonClick += new ButtonEventHandler(onButtonAction);
b.ButtonClick += new ButtonEventHandler(onButtonAction2);
b.ButtonClick += new ButtonEventHandler(onButtonAction);
b.ButtonClick += new ButtonEventHandler(onButtonAction2);
I have added 4 event handlers to the ButtonClick event and they all have the
same type (object and int) as defined by the delegate:
delegate void ButtonEventHandler(object source, int clickCount);
I was just curious if you an event handler can only have one delegate (same
parameters)?
I can have as many handlers as I want (not sure why I would need to) but
they all have to be the same delegate type.
Is this always the case?
****************************************
using System;
//declaring the event handler delegate
delegate void ButtonEventHandler(object source, int clickCount);
class Button
{
//declaring the event
public event ButtonEventHandler ButtonClick;
//Function to trigger the event
public void clicked(int count)
{
Console.WriteLine("\nInside Clicked !!!");
//Invoking all the event handlers
if (ButtonClick != null) ButtonClick(this, count);
}
}
public class Dialog
{
public Dialog()
{
Button b = new Button();
//Adding an event handler
b.ButtonClick += new ButtonEventHandler(onButtonAction);
//Triggering the event
b.clicked(1);
b.ButtonClick += new ButtonEventHandler(onButtonAction2);
b.clicked(1);
//Removing an event handler
b.ButtonClick += new ButtonEventHandler(onButtonAction);
b.clicked(1);
b.ButtonClick += new ButtonEventHandler(onButtonAction2);
b.clicked(1);
}
static void Main()
{
new Dialog();
Console.Read();
}
//Event Handler function
public void onButtonAction(object source, int clickCount)
{
Console.WriteLine("Inside Event Handler !!!");
}
public void onButtonAction2(object source, int clickCount)
{
Console.WriteLine("Inside Event Handler 2 !!!");
}
}
****************************************
Thanks,
Tom
handlers (onButtonAction and onButtonAction2).
I had defined the event as:
public event ButtonEventHandler ButtonClick;
where ButtonEventHandler is a delegate type with 2 parameters: one is an
object and the other is an int.
I then do:
b.ButtonClick += new ButtonEventHandler(onButtonAction);
b.ButtonClick += new ButtonEventHandler(onButtonAction2);
b.ButtonClick += new ButtonEventHandler(onButtonAction);
b.ButtonClick += new ButtonEventHandler(onButtonAction2);
I have added 4 event handlers to the ButtonClick event and they all have the
same type (object and int) as defined by the delegate:
delegate void ButtonEventHandler(object source, int clickCount);
I was just curious if you an event handler can only have one delegate (same
parameters)?
I can have as many handlers as I want (not sure why I would need to) but
they all have to be the same delegate type.
Is this always the case?
****************************************
using System;
//declaring the event handler delegate
delegate void ButtonEventHandler(object source, int clickCount);
class Button
{
//declaring the event
public event ButtonEventHandler ButtonClick;
//Function to trigger the event
public void clicked(int count)
{
Console.WriteLine("\nInside Clicked !!!");
//Invoking all the event handlers
if (ButtonClick != null) ButtonClick(this, count);
}
}
public class Dialog
{
public Dialog()
{
Button b = new Button();
//Adding an event handler
b.ButtonClick += new ButtonEventHandler(onButtonAction);
//Triggering the event
b.clicked(1);
b.ButtonClick += new ButtonEventHandler(onButtonAction2);
b.clicked(1);
//Removing an event handler
b.ButtonClick += new ButtonEventHandler(onButtonAction);
b.clicked(1);
b.ButtonClick += new ButtonEventHandler(onButtonAction2);
b.clicked(1);
}
static void Main()
{
new Dialog();
Console.Read();
}
//Event Handler function
public void onButtonAction(object source, int clickCount)
{
Console.WriteLine("Inside Event Handler !!!");
}
public void onButtonAction2(object source, int clickCount)
{
Console.WriteLine("Inside Event Handler 2 !!!");
}
}
****************************************
Thanks,
Tom