For Answers, see/post comments

Calling MDI Child Methods from MDI Parent Form

Dear All,
I require your assistance in the following:
I have a main MDI Form, frmMain (Parent)
This form has a Menu which has Save, Add Delete and other commands
I want that if someone click on this save menu item... then... save method of my active child form is run. ( frmEmployee )
Obviously i will have to first find which form is active then will see whether this form has any save method or not!
I will be very very thankful and appreciate any help .... any technique and ways to do it... please explain with examples if possibles ... please!!!

1 comment:

Anonymous said...

Hi,

If you have same name for save function in all mdi child forms then


You should create an interface and have each form implement the
interface


public interface ISaveable
{
void Save();



}


public class MyForm : System.Windows.Forms.Form, ISavable
{
public void Save()
{
//Do something
}


}


When save is clicked, you can test to see if the
System.Windows.Forms.Form.ActiveForm implements ISaveabe

if (System.Windows.Forms.Form.ActiveForm is ISaveable)
{
((ISaveable)System.Windows.Forms.Form.ActiveForm).Save();



}


- If you dont want to go for this then try the code below: [Note: all
mdi child form have same function name for save]

MethodInfo mi = mdiMainForm.ActiveMdiChild.GetMethod("Save");


mi.Invoke(...);


- If you think that it is more complicated then you need to write long
code and swich case for finding which MDI child form is current.