473,394 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Calling method in child form from MDI main form

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");
}
}
}
Nov 16 '05 #1
5 3831
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" <rb***@sumaria.net> wrote in message
news:7Z***************@nwrdny01.gnilink.net...
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");
}
}
}

Nov 16 '05 #2
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" <sh**@fakedomain.com> wrote in message
news:ei*************@tk2msftngp13.phx.gbl...
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" <rb***@sumaria.net> wrote in message
news:7Z***************@nwrdny01.gnilink.net...
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");
}
}
}


Nov 16 '05 #3
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" <rb***@sumaria.net> wrote in message
news:Ul***************@nwrdny01.gnilink.net...
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" <sh**@fakedomain.com> wrote in message
news:ei*************@tk2msftngp13.phx.gbl...
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" <rb***@sumaria.net> wrote in message
news:7Z***************@nwrdny01.gnilink.net...
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");
}
}
}



Nov 16 '05 #4
That's got it. Many thanks for all the help!

Randy

"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:eC**************@TK2MSFTNGP12.phx.gbl...
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" <rb***@sumaria.net> wrote in message
news:Ul***************@nwrdny01.gnilink.net...
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" <sh**@fakedomain.com> wrote in message
news:ei*************@tk2msftngp13.phx.gbl...
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" <rb***@sumaria.net> wrote in message
news:7Z***************@nwrdny01.gnilink.net...
> 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");
> }
> }
> }
>
>



Nov 16 '05 #5
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" <rb***@sumaria.net> wrote in message
news:Ul***************@nwrdny01.gnilink.net...
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" <sh**@fakedomain.com> wrote in message
news:ei*************@tk2msftngp13.phx.gbl...
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" <rb***@sumaria.net> wrote in message
news:7Z***************@nwrdny01.gnilink.net...
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");
}
}
}



Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jon Hyland | last post by:
Ok, I'm a little rusty on this, it should be a simple problem but I can't figure it out. How can I handle form events in my main code page?? I'm creating a Windows App in C#. Rather than make...
2
by: VM | last post by:
How can I call a method in a child Form from within the parent MDI form? The child form has a Save method but I'd also like to give the user the opportunity to "save" from the MDI form also. They'd...
6
by: Gary Miller | last post by:
Does anyone know how to detect a modeless form on closing by the form that invoked the modeless form? form.Show();
5
by: Earl | last post by:
I need to call a method on an owned child form, and am wondering if the best way of doing this is to capture the Closing event of the form that passes control back to the form where I have the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.