Counting the number of subscribers to a controls events

G

Guest

Is there any method I can use to determine the number of subscribers to a
controls events.

In particular I'm looking at the ComboBox.SelectedIndexChanged event, I have
a scenario where I need to cancel the SelectedIndexChanged event and restore
the original SelectedIndex property. To do this I unsubscribe the
SelectedIndexChanged event, update the SelectedIndex property and then
re-subscribe. I need to do this to prevent the SelectedIndexChanged event
from firing twice (i.e once when the user changes the control and twice when
the code restores the original value).

I don't want to maintain a flag as the entire user interface is generated
dynamically.

Therefore I need a method to check how many subscribers there are to the
SelectedIndexChanged event before I attempt to add another subscription.

Any ideas would be appreciated.

Steve
 
N

Nicholas Paldino [.NET/C# MVP]

Steve,

You aren't going to be able to do this without deriving from a class and
overriding the OnSelectedIndexChanged method, not calling the base class
(which fires the event). Removing all of the subscribers then re-attaching
them is something you really shouldn't be doing, as you are wasting a ton of
extra processing cycles.

Hope this helps.
 
G

Guest

Nicholas,

Thanks for the reply.

I will have to think of another approach.

Steve

Nicholas Paldino said:
Steve,

You aren't going to be able to do this without deriving from a class and
overriding the OnSelectedIndexChanged method, not calling the base class
(which fires the event). Removing all of the subscribers then re-attaching
them is something you really shouldn't be doing, as you are wasting a ton of
extra processing cycles.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve Wilkinson said:
Is there any method I can use to determine the number of subscribers to a
controls events.

In particular I'm looking at the ComboBox.SelectedIndexChanged event, I
have
a scenario where I need to cancel the SelectedIndexChanged event and
restore
the original SelectedIndex property. To do this I unsubscribe the
SelectedIndexChanged event, update the SelectedIndex property and then
re-subscribe. I need to do this to prevent the SelectedIndexChanged event
from firing twice (i.e once when the user changes the control and twice
when
the code restores the original value).

I don't want to maintain a flag as the entire user interface is generated
dynamically.

Therefore I need a method to check how many subscribers there are to the
SelectedIndexChanged event before I attempt to add another subscription.

Any ideas would be appreciated.

Steve
 

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