Q1. Is it possible to check if an event is being hooked
for eg. cbxXXX.selectedvaluechanged += delegate { .....;}
Any way to check the above event is being hooked
Q2. How is it possible to unhook the above event once hooked i tried -= but it doesnt work correctly.
Thanks
Regards
Alu

Hook and unhook and event using delegate
Sam_res03
Yes, you can. This way you'll avoid hooking the event twice and it does not raise an exception if the even was not already hooked. However, I'd say that you should not make this a "programming practice". You should have other means to prevent hooking an event twice if possible and reasonable.
gokce
oh sorry one last question....do you think it's necessary to hook this event like the combo box's selectedvaluechanged ..since this event will always be triggered 'internally' ie i dont even need the += and -= and just move the codes into the event generated by vs2005....what do you think
Regards
Alu
xishan shigri
Predator14567
I use to add event handlers from the designer whenever I can.
The usual situation when you may want to avoid using the designer to add events is then you need to intialize a control (adding nodes to a tree and selecting a node) and you want the event to be raised only after the initialization is done. For example for tree you may want to select some node initially and this will raise AfterSelect event but you only want this event to raised after you have set the initial node so you actually receieve events for selections done by the user.
erikkl2000
gshaf
cbxXXX.SelectedValueChanged -= cbxXXX_SelectedValueChanged;
and immediately ...
cbxXXX.SelectedValueChanged += cbxXXX_SelectedValueChanged;
..without any checking in the invocationlist that you mention..:)
Regards
Alu
BrianXXX
Q1. Depends on what you mean by "check the above event is being hooked". It is possible to get the "invocation list" of a delegate using its method called "GetInvocationList". However in this case you don't have access to the delegate because it is a private field in the (I assume) ComboBox class. You may be able to do something using reflection. There are various articles on the subject and here's one of them: http://www.bobpowell.net/eventsubscribers.htm
Q2. Since you're using anonymous delegates then you can't remove it because you don't know the method that was hooked to it. Using something like SelectedValueChanged -= delegate { same code as in += delegate; }; won't work because the compiler will generate another method.
Of course, using what I said in Q1 (reflection) there may be a way probably... but I'd rather choose to use a named delegate instead of an anonymous one...
Klaxas
sorry but if you can a link or a simple example on the named delegate which i hope that it can replace the anym. delegate that i have been using..
Thanks.
Regards
Alu
Worf
And now that i have move the above codes to the event generated by the designer...it will definitely move into the codes even before the -+ or += is called.So that means its usleess to use it anymore....
Regards
Alu
gvraovc
Hmm.. sure.. if that cbxXXX is a ComboBox then:
// the method that will be invoked when the event is raised
private void cbxXXX_SelectedValueChanged(object sender, EventArgs e)
{
// the code that was in += delegate { ... } ;
}
// to hook event
cbxXXX.SelectedValueChanged += cbxXXX_SelectedValueChanged;
// to unhook event
cbxXXX.SelectedValueChanged -= cbxXXX_SelectedValueChanged;
Zep--
well in that case this means i have to be careful when the application load initially and make sure it wont move into the events ...right :) maybe a focused check may help Thanks!.
Regards
Alu