Search code examples
c#.neteventsraise

Raising external object's events in C#


If actions is a Panel, am I able to raise the Click event of it's parent? I have this code at the moment, but the Click event isn't a method, so this code is invalid.
Does anyone know how I can achieve this?

actions.Click += delegate(object Sender, EventArgs e)
{
    ((Panel)Sender).Parent.Click();
}

Solution

  • It's not possible to raise a C# event directly from another class (even if it's public). You could provide a method (with a sufficient access modifier) that raises the event on your behalf and call that method in the other class.

    By the way, this is possible with reflection, but I consider that a dirty hack.