abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Child c = new Child();
c.MyEventHappened += new Child.MyEvent(c_MyEventHappened);
}
void c_MyEventHappened(int i)
{
textBox1.Text = "My event happened";
}
}
class Child
{
public delegate void MyEvent(int i);
public event MyEvent MyEventHappened;
public Child()
{
}
public void DoSomething()
{
//I've finished doing something I should let anyone who cares know
MyEventHappened(1);
}
}
That should be basically what you need to do. The Child class has a delegate called 'MyEvent' with a specific signature. Then the Child class has an event called 'MyEventHappened" which uses MyEvent. So, when 'MyEventHappened' is called in 'DoSomething' anything attached to 'MyEventHappened" will also be called. And if you look in the 'Form1' class in the constructor 'c_MyEventHappened" is wrapped in the 'MyEvent' delegate and added to the 'MyEventHappened' event.
Make all the stuff in your child class first. Then go back to your form and attach to the event. Just a heads up, in Visual Studio when you type '+=' after referencing an event, it will automatically offer to write the code for you. Keep in mind that the signature of the delegate is up to you.
kimbokasteniv
Junior Poster in Training
50 posts since Nov 2006
Reputation Points: 13
Solved Threads: 4