Calling method in child form from MDI main form

  • Thread starter Thread starter Randy
  • Start date Start date
R

Randy

The following code, which runs when I click File->Save in my main form,
finds my save button in my mdi child for just fine. The question is: how do
I invoke the Save method in the child form?

Thanks....

protected void SaveMenuSelect(Object sender, System.EventArgs e)
{
Form form = (Form)this.ActiveMdiChild;
foreach(Control c in form.Controls)
{
if(c.Name == "btnSave")
{
Console.WriteLine("control is btnSave");
}
}
}
 
Randy,

The better way to do is

Form [] allChilds =myMainForm.MdiChildren;

foreach(Form pChild in allChilds)
{
pChild.Save(); //your public method.
}

Keep a public method in child form. After getting child forms, got through
each form and fire save method.

Shak.
 
Thanks for the response. If I put Console.Writeline(pChild.Name.ToString());
inside the foreach loop, it prints out the correct form name. If I put
pChild.mySave(), where mySave() is a public method inside the child form, I
get a compile error stating that mySave() is not defined. Any thoughts?

Thanks....


Shakir Hussain said:
Randy,

The better way to do is

Form [] allChilds =myMainForm.MdiChildren;

foreach(Form pChild in allChilds)
{
pChild.Save(); //your public method.
}

Keep a public method in child form. After getting child forms, got through
each form and fire save method.

Shak.

Randy said:
The following code, which runs when I click File->Save in my main form,
finds my save button in my mdi child for just fine. The question is: how do
I invoke the Save method in the child form?

Thanks....

protected void SaveMenuSelect(Object sender, System.EventArgs e)
{
Form form = (Form)this.ActiveMdiChild;
foreach(Control c in form.Controls)
{
if(c.Name == "btnSave")
{
Console.WriteLine("control is btnSave");
}
}
}
 
Randy,

What you get from MdiChildren is Form[].

You have to cast it to your Childform name to get access to the methods

foreach(Form pChild in allChilds)
{
try
{
MyChildForm pForm = (MyChildForm )pChild;
if(pForm != null)
pForm.mySave();
}
catch(Exception e)
{
//invalid cast exception
}
}

Randy said:
Thanks for the response. If I put Console.Writeline(pChild.Name.ToString());
inside the foreach loop, it prints out the correct form name. If I put
pChild.mySave(), where mySave() is a public method inside the child form, I
get a compile error stating that mySave() is not defined. Any thoughts?

Thanks....


Shakir Hussain said:
Randy,

The better way to do is

Form [] allChilds =myMainForm.MdiChildren;

foreach(Form pChild in allChilds)
{
pChild.Save(); //your public method.
}

Keep a public method in child form. After getting child forms, got through
each form and fire save method.

Shak.

Randy said:
The following code, which runs when I click File->Save in my main form,
finds my save button in my mdi child for just fine. The question is:
how
do
I invoke the Save method in the child form?

Thanks....

protected void SaveMenuSelect(Object sender, System.EventArgs e)
{
Form form = (Form)this.ActiveMdiChild;
foreach(Control c in form.Controls)
{
if(c.Name == "btnSave")
{
Console.WriteLine("control is btnSave");
}
}
}
 
That's got it. Many thanks for all the help!

Randy

Shakir Hussain said:
Randy,

What you get from MdiChildren is Form[].

You have to cast it to your Childform name to get access to the methods

foreach(Form pChild in allChilds)
{
try
{
MyChildForm pForm = (MyChildForm )pChild;
if(pForm != null)
pForm.mySave();
}
catch(Exception e)
{
//invalid cast exception
}
}

Randy said:
Thanks for the response. If I put Console.Writeline(pChild.Name.ToString());
inside the foreach loop, it prints out the correct form name. If I put
pChild.mySave(), where mySave() is a public method inside the child
form,
I
get a compile error stating that mySave() is not defined. Any thoughts?

Thanks....


Shakir Hussain said:
Randy,

The better way to do is

Form [] allChilds =myMainForm.MdiChildren;

foreach(Form pChild in allChilds)
{
pChild.Save(); //your public method.
}

Keep a public method in child form. After getting child forms, got through
each form and fire save method.

Shak.

The following code, which runs when I click File->Save in my main form,
finds my save button in my mdi child for just fine. The question is: how
do
I invoke the Save method in the child form?

Thanks....

protected void SaveMenuSelect(Object sender, System.EventArgs e)
{
Form form = (Form)this.ActiveMdiChild;
foreach(Control c in form.Controls)
{
if(c.Name == "btnSave")
{
Console.WriteLine("control is btnSave");
}
}
}
 
Hi Randy,

Supposing that the class name of the form with the public Save method is
ChildForm, and supposing all your MDI child forms are instances of
ChildForm, you could write the code like this:

foreach(ChildForm childForm in this.MdiChildren)
{
childForm.Save();
}

If not all of the MDI child forms are instances of ChildForm, you could
write it like this:

foreach(Form form in this.MdiChildren)
{
ChildForm childForm = form as ChildForm;

if (childForm != null)
{
childForm.Save();
}
...
}


Randy said:
Thanks for the response. If I put Console.Writeline(pChild.Name.ToString());
inside the foreach loop, it prints out the correct form name. If I put
pChild.mySave(), where mySave() is a public method inside the child form, I
get a compile error stating that mySave() is not defined. Any thoughts?

Thanks....


Shakir Hussain said:
Randy,

The better way to do is

Form [] allChilds =myMainForm.MdiChildren;

foreach(Form pChild in allChilds)
{
pChild.Save(); //your public method.
}

Keep a public method in child form. After getting child forms, got through
each form and fire save method.

Shak.

Randy said:
The following code, which runs when I click File->Save in my main form,
finds my save button in my mdi child for just fine. The question is:
how
do
I invoke the Save method in the child form?

Thanks....

protected void SaveMenuSelect(Object sender, System.EventArgs e)
{
Form form = (Form)this.ActiveMdiChild;
foreach(Control c in form.Controls)
{
if(c.Name == "btnSave")
{
Console.WriteLine("control is btnSave");
}
}
}
 
Back
Top