You are correct, you can use events here. When you create your child form inside your parent form, simply attach to the child form's FormClosing or FormClosed event. As an example...
- ChildForm childForm = new ChildForm();
-
childForm.FormClosed += new FormClosedEventHandler(childForm_FormClosed);
-
childForm.Show();
... you can then create the childForm_FormClosed method and do whatever you like in it. Alternatively, this is a good time for you to learn about delegates :)
Create a delegate on your child form and call it ParentUpdateDelegate (or something like that). Next, create a public property, perhaps called UpdateParent, on the child form that allows access to that delegate and provides get and set functionality. In your form closing or form closed event, whichever you prefer, you can call the delegate with the following code...
- if (UpdateParent != null)
-
UpdateParent();
It's important to do the null check because it's quite possible the programmer may not have set the delegate, leaving it null when it gets called.
In your main form, when you create the child window, you simply assign the property you created to a method with a matching signature to your delegate. You can name this method anything you like.
As an example, I created a child form object, ChildForm, with an UpdateParentDelegate delegate and an UpdateParent property. I put the call to UpdateParent in the form closed event. In my main form I have a button. This button, when clicked, creates a new instance of ChildForm, assigns its UpdateParent property to a method called UpdateMe, disables the button, then shows the form. In the UpdateMe method, I simply re-enable the button.
The result is when I click the button it opens the child form and disables the button. When the child form closes it will make a call to the delegate, which points to the UpdateMe method on the parent form. The UpdateMe method runs and the button is enabled again.
Both methods work just fine. The advantage of a delegate is that you can send parameters along, which will let you pass data that your parent may need. If you do the event way you can still get the data, it just needs to be available via public properties. In both cases you'll need to make sure you get the data before the child form disposes itself.
I hope that helps you!