GET WINFORM SUBSCRIPTIONS TO EVENTS

J

Jose Fernandez

Hello.

I would like to know how could i get all the subscriptions that my form has
with the events of their controls.

For example.

I have a form with a textbox, a button and a dropdown. I create a
subscription to the Click event of the Button and another to the
SelectedIndexChanged del dropDown.

so, automatically it gets created the two event handler methods for these
subscriptions.
my point is that i would like to be able in runtime to find those two
subscriptions, for example, and delete them in runtime. does it make sense?
 
G

Guest

Hi Jose,
it is not possible for you to get to the MulticastDelegate instance which
is being used for the events in your winform controls to see which delegates
are currently in it's invocation list. You can use the -= syntax to remove
delegates, so if you know which delegates you want to remove you can use -=
to remove them this will work even if the delegates are not currently in the
events underlying Multicast delegate. You best bet would be to store the
delegates you want to remove in your form and then just call -= when you want
to remove them.

Mark.
 
J

Jose Fernandez

Well..

I want something that i can write once, and use in every form, in every
control.
I have already implementedIExtenderProvider to set the security values in
design time for every control in any forms that i load. Then, what i do, is
go to the database, fetch the security values for that user, store them in a
IPrincipal object that i created and everytime i load a form, i compare the
security values of the user with those in every control. That works
perfect!. I created a method in a base form, all my forms inherits from it.
So, when i load a form, i call that method, pass the parameters that i need
and voila... my controls appear enabled, disabled, visible... depends on the
criteria. So, one piece of code, 5 lines, just works for every form and
every control.

I wanted then, due to the ActionSecurityLevel, to be able to change in
runtime the method to be called when you click on a button. right? 'Cause i
don't wanna be writing code everytime i create a subscription to an event. I
know how to add in runtime an eventHandler to a control... as easy as
Control.[event]+=new System.EventHandler [or any other
handler](the_method_to_call). That's fine. But doesn't prevent the code to
perform any other method that had a subscription to that event.

So, i know also that you can programmatically unsubscribe an event by
Control.[event]-=.......... the problem is that when i am iterating thru
controls, i don't have the way to pass or know which handler[method] is
dealing with the subscription. I could do some trick. Like... once i hace
the control (in my iteration security checking code) i can find its instance
name, ok? lets say button1. ok. then i can ASSUME that when i created that
subscription it took by default the _Click suffix... so, it would be
button1_Click.
Next step would be to cast to Handler this string "button1_Click". So, i
would have to create a Method, returning a handler after i pass a parameter
type string with the "name" i "guess" it's going to have the handler. does
it makes sense?

let's put some pseudo code.

let's assume all controls are button.
foreach(Control c in form.Controls)
if(c.ActionSecurityLevel > User.ActionSecurityLevel)
c.click+=new system.EventHandler(myNonActionCode); --> we have here
added a new handler
c.click-=new system.EventHandler(????????); --> i can't write a
handler manually since you are iterating. i don't know the handler method
since i don't "know" its list of handlers. we can't access list of
suscription from the form to that control event.

MY "SILLY" SOLUTION

in that line of code (???????).. well i would set a method in there that
would return the eventhandler.

private EventHandler getEventHandler(string assumedName) -> remember,
assumed name is the concatenation of instance name of the control and
"_Click"

so, this method supposedly will return a handler with the name by default
that VS gives to a subscription to a click event. for example, for button
instance button1, would be button1_Click.
 
B

Bob Powell [MVP]

Hi Mark and Jose,
Actually this is what I thought too before I decided to delve into the
problem.

See the article in Windows Forms Tips and Tricks for enlightenment.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top