 |
| C# 2005 For discussion of Visual C# 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

October 4th, 2007, 03:52 PM
|
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Open ChildForm from another ChildForm in MDIParent
I'm working on WinForm application and created MDIParent form. I have no problem launching child form from the MDIParent form itself, however I couldn't figure out a way to launch ChildForm2 from ChildForm1. Here's what I have in ChildForm1 code:
Code:
private void btnChild2_Click(object sender, EventArgs e)
{
frmChild2 Child2 = new frmChild2();
frmParent frmParent = new frmParent();
frmParent.IsMdiContainer = true;
Child2.MdiParent = frmParent;
Child2.Show();
}
Please advice.
|
|

October 4th, 2007, 04:09 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Surely if you want them to have the same parent then you don't want to create a new parent form.
Code:
frmChild2 c2 = new frmChild2();
c2.MdiParent = this.MdiParent;
c2.Show();
/- Sam Judson : Wrox Technical Editor -/
|
|

October 4th, 2007, 04:25 PM
|
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you so much for your answer, that's work!
However, I have one more quick question:
Let say when Child2 is launch, I want to check if there's any other child form still open and close them.
Code:
public void childClose()
{
foreach (Form childForm in this.MdiChildren)
{
childForm.Close();
}
}
private void btnChild2_Click(object sender, EventArgs e)
{
childClose();
frmChild2 Child2 = new frmChild2();
Child2.MdiParent = this.MdiParent;
Child2.Show();
}
|
|

October 4th, 2007, 04:29 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
You haven't actually asked a question?
But you are aware that that code will try to close the form you are currently running the code in as well?
/- Sam Judson : Wrox Technical Editor -/
|
|

October 4th, 2007, 04:34 PM
|
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes, I wanted to close Child1 or any other child form that still open when Child2 is launch, but the code that I have doesn't close it. How do you do that?
|
|

October 4th, 2007, 04:37 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Code:
foreach (Form childForm in this.MdiParent.MdiChildren)
/- Sam Judson : Wrox Technical Editor -/
|
|

October 4th, 2007, 04:46 PM
|
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thank you!
|
|

October 4th, 2007, 04:52 PM
|
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
It still doesn't work. It close all the child forms, but instead of opening Child2 as a child form, it open as an independent form instead. Do you know a way for when Child2 is launch, it close all the "other" child form and open Child2 as a childform?
|
|

October 5th, 2007, 01:28 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
I can only assume that because you are closing the current form before opening the new form that the MdiParent property is being blanked. Check this is not null before you are using it.
You could try this:
Code:
frmChild2 Child2 = new frmChild2();
Child2.MdiParent = this.MdiParent;
Child2.Show();
foreach (Form childForm in this.MdiParent.MdiChildren)
{
if( childForm != Child2 ) childForm.Close();
}
/- Sam Judson : Wrox Technical Editor -/
|
|

October 9th, 2007, 08:28 AM
|
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
That function work when I call from another child form, thank you! However, I actually want to use a user control inside of that frmChild1 and call frmChild2 so with this.mdiParent doesn't work. I try with frmParent.MdiParent, but it doesn't work either. Please advice!
|
|
 |