User1119078696 posted
You really should be using a event delegate this will help you track your event easier. Here is an overly simple expample of how event should be captured. You are taking the right approach and subscribing to the event in each of your classes.
Is the parent control capturing the event. Here's a way I know that works.
Let's start with the delegate. A delegate is a way of defining the signature of method you want to call when the event is triggered. For example take this code: (I always store this code in it's own cs file. It is not contained
in a class. It just sits on its own.)
public
delegate
void ButtonEventDelegate(object source, System.EventArgs
e);
Above we have declared a delegate called ButtonEventDelegate that will take two arguments. When you create your method that you want to call when the event is triggered, it must have those two arguments. The signatures must match.
Next in your form (class) you declare an event and attach it to that delegate. This is declared in a class refered to as the event publisher.
public
event
ButtonEventDelegate ButtonClick;
Now, here we go. Your button will subscribe to ButtonClick by doing this. This is called the subscriber
myCommandButton.Click += MyCalledMethod;
When your button is clicked it will call MyCalledMethod. Of cource be for it can compile or call that method we have to create it. "Do any of you methods match the signature of the delegate?" (childish but i think you get the point)
private void MyCalledMethod(object sender,
EventArgs e)
{
if (ButtonClicked !=
null)
{
ButtonClicked(sender, e);
}
}
Please note the parameters in MyCalledMethod match the delegate (very important) and you see a little logic in there. The event is only fired if something subscribed to it
Those are the main pieces. Delegate events are of course most useful between classes so make sure you include a reference to the class containing the event declareation (public
event ButtonEventDelegate ButtonClick;) in your class that will fire the event and put the method that matches the signature.
Ok here's the big example:
In subscriber class: