-1

A method has an input parameter that is a forms control object (say a ComboBox). I want to determine if the control has a particular event registered and, if so, trigger the event. I tried to enter the following code but the compiler rejects it because the selected event (in the example SelectedIndexChanged) can only appear to the left of the += or -= operators.

private void DoSearch(ComboBox cb) {

     // Once the search is complete, I want to call the event

     // Does this control have a SelectedIndexChanged event registered?
     if (cb.SelectedIndexChanged != null) {
        // call the event
        cb.SelectedIndexChanged(cb, EventArgs.Empty);
     }
}
  • Where is your actual _question_? There is not enough detail in your post to understand even how the "search routine the caller desired" is specified, nor how an event handler is "registered" for a control. Please provide [a good, _minimal_, _complete_ code example](https://stackoverflow.com/help/mcve) that clearly illustrates the question, along with a precise explanation of what you've tried in that code, what happened, and how that was different from what you want. – Peter Duniho Oct 25 '15 at 19:26
  • The code I provided does not work. The compiler rejects it because SelectedIndexChanged event can only appear to the left of a += or -+ – Marvin Thompson Oct 26 '15 at 01:27
  • Then you should say so in your post. It's still not clear what your actual _question_ is, but if I am understanding correctly, your post is a duplicate of [Raise an event of a class from a different class in C#](http://stackoverflow.com/q/4378339). If you agree, please close this question as a duplicate of that one. If you don't agree, please edit your question so that it describes clearly how it's different from the other question, providing [a good, _minimal_, _complete_ code example](https://stackoverflow.com/help/mcve) and a precise explanation of why that code doesn't do what you want. – Peter Duniho Oct 26 '15 at 01:43
  • See also https://stackoverflow.com/help/how-to-ask for advice on how to present your question in a clear, answerable way. – Peter Duniho Oct 26 '15 at 01:43
  • I wasn't looking to raise the event, just call the method if it existed. Setting the 'SelectedIndex' will solve the issue though. Thanks. – Marvin Thompson Oct 26 '15 at 12:31
  • @MarvinThompson - I think you have a terminology issue. You "raise" events, you can't "call" them. There is no "method" to "call". – Enigmativity Oct 27 '15 at 00:59

1 Answers1

0

You can't do what you're asking. Only the class in which the event is defined can raise the event.

Not even subclasses can call a parent class's events. You might see, in a subclass, methods called On* (i.e. OnSelectedIndexChanged). These are used to allow the subclass to call into the base class where the event is defined to raise the event. These methods are usually marked as protected so that only child classes can call them.

Otherwise, the way to get SelectedIndexChanged to be raised is to set the SelectedIndex property.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172