Calling method in child form from MDI main form

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");
}
}
}
 
S

Shakir Hussain

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.
 
R

Randy

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");
}
}
}
 
S

Shakir Hussain

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");
}
}
}
 
R

Randy

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");
}
}
}
 
D

Daniel Pratt

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");
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top