Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
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
 
Old October 4th, 2007, 03:52 PM
Authorized User
 
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

 
Old October 4th, 2007, 04:09 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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 -/
 
Old October 4th, 2007, 04:25 PM
Authorized User
 
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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();
        }
 
Old October 4th, 2007, 04:29 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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 -/
 
Old October 4th, 2007, 04:34 PM
Authorized User
 
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

 
Old October 4th, 2007, 04:37 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Code:
foreach (Form childForm in this.MdiParent.MdiChildren)
/- Sam Judson : Wrox Technical Editor -/
 
Old October 4th, 2007, 04:46 PM
Authorized User
 
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you!

 
Old October 4th, 2007, 04:52 PM
Authorized User
 
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

 
Old October 5th, 2007, 01:28 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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 -/
 
Old October 9th, 2007, 08:28 AM
Authorized User
 
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Open ChildForm from UserControl (MDIParent) juzjess C# 2005 2 November 13th, 2007 05:07 AM
Horizontal scroll bar when childform is maximize juzjess C# 2005 1 November 7th, 2007 05:04 AM
Open document, Open second doc and copy to first justabeginner Word VBA 1 March 7th, 2007 02:47 AM
Open the "Open File" dialogue box piratelordx Access VBA 4 March 14th, 2006 10:08 PM
Attempted to open a database that is already open person747 Access 10 September 3rd, 2004 04:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.