I was wondering if there was a simple way that I can check to see if a
specific handler exists for a control. I know I can simply create and set a boolean
variable to true once I create the handler, but I was hoping that there
was a simple way, without the use of additional variables, to check whether or not the handler has been created:
My control is a TreeView, and is called tvRobot. I create the
BeforeSelect handler, tvRobot_AlwaysBeforeSelect, within another event.
The problem is, is that I have two handlers for the BeforeSelect event,
both of which get created and destroyed throughout the program, so I
can't simple check to see if "a" handler exists, I have to check for
the specific name, "tvRobot_AlwaysBeforeSelect" in this case.

Checking if a specific handler exists
Peter B.L. Rasmussen
Delamater
Hmm... interesting.
I tried the following in C# 2.0, Visual Studio 2005 and I get an
error CS0070: The event 'Delegates.Publisher.MyEvent' can only appear on the left hand side of += or -= (except when used from within the type 'Delegates.Publisher')
class Subscriber{
public Subscriber(Publisher publisher){
new EventHandler(publisher_MyEvent); foreach (Delegate del in publisher.MyEvent.GetInvocationList()){
Console.WriteLine(del.Method.Name);}
}
void publisher_MyEvent(object sender, EventArgs e){
}
}
Bernd
Maju V Poulose
BMF
er1067
gb99
Alex,
what you can do is accessing the invocation list (list of handlers) of an event in the class the publishes the events and then find out the target object and the method for every entry in the list:
class Publisher{
public event EventHandler MyEvent; public void PrintEventHandlers(){
foreach (Delegate del in MyEvent.GetInvocationList()){
Console.WriteLine(del.Method.Name);}
}
}
But this is not possible from outside the class that defines the event. The only thing you could do is wrapping the event of the TreeView by an event of your own and using this event instead of the event of the TreeView.Bernd
bobe59
I have a handler (see below) that will get added and subtracted throughout my program. What I want to do, is set up my program to test and see if the handler has been added, so that it doesn't get added more than once.
tvRobot.BeforeSelect += new TreeViewCancelEventHandler(tvRobot_AlwaysBeforeSelect);
Fizgig
Error 1 The event 'System.Windows.Forms.TreeView.BeforeSelect' can only appear on the left hand side of += or -= C:\Alex\FlashDrive\Projects\RobotRenderer\RoboArmRobotRenderer13.5\XmlEditor\frmXMLArmEditor.cs 812 58 XmlEditor
And it is highlighting "BeforeSelect" in "tvRobot.BeforeSelect.GetInvocationList()"
Is there a reference or a namespace that I'm leaving out I can't seem to get any members to come up in Intellisense when I type dot (.) after "BeforeSelect".
DylanG3893
public void WireBeforeSelectEvent( TreeViewCancelEventHandler invocation )
{
bool allreadyWired = false;
foreach( Delegate wiredInvocation in tvRobot.BeforeSelect.GetInvocationList())
{
MethodInfo invocationMethod = invocation.Method;
if( wiredInvocation.Method.Name.Equals( invocation.Method.Name ))
{
allreadyWired = true;
break;
}
}
if( !allreadyWired )
{
tvRobot.BeforeSelect += invocation;
}
}
When you want to wire you method you use it like this:
TreeViewCancelEventHandler invocation = new TreeViewCancelEventHandler(tvRobot_AlwaysBeforeSelect);
WireBeforeSelectEvent( invocation );
Newbie2007
Thanks though for all of your help, I really appreciate it:) You taught me some things I never knew about.
vcboy
ricoxxx
Yaron Lavi
You can make a method like this:
private bool _wired = false;
public void EnsureWired()
{
if( !_wired )
{
object.Event += new EventHandler( MyMethod );
_wired = true;
}
}