i have a main form that has a button, when click a subform1 will open. in subform1 theres another button that will open subform2 in main form..i have this code in subform1.

{
            profilefrm prf = null;
            foreach (Form existingForm in this.MdiChildren)
            {
                prf = existingForm as profilefrm;
                if (prf != null)
                    break;
            }
            if (prf == null)
            {
                prf = new profilefrm();
                prf.MdiParent = this;
                prf.Show();
                prf.Location = new Point(101, 90);
                this.Text = "MyPersonalDietaryGuidance-MY PDG";
            }
            else
                prf.Activate();
            this.Text = "MyPersonalDietaryGuidance-MY PDG";
        }

then got this error...
Form that was specified to be the MdiParent for this form is not an MdiContainer.
Parameter name: value

main form is a container...property is set---> isMDIcontainer=true;

my goal is open subform via other subform i main form,, THANKS

Try that one... You'll need to check which one is Mdi or not...

Form m;
            if (Exists("xxx", out m))
            {
                m.Activate();
            }
            else
            {
               m = new xxx();
                m.Name = "xxx";
                m.MdiParent = this.MdiParent; //this.MdiParent.mdiparent.mdiparent bla bla               
 m.Show();
            }
        }
        public bool Exists(string name, out Form found)
        {
            foreach (Form subform in this.MdiParent.MdiChildren)
            {
                if (subform.Name == name)
                {
                    found = subform;
                    return true;
                }
            }
            found = null;
            return false;
        }

try giving subform toplevel as false

subform.TopLevel = false;

I am having a similar problem. I would like for 2 different sub forms to open in the mdi form. I can get 1 to open but not the 2nd.

public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Subform1 apple = new Subform1(this);
            apple.Show();
        }
public partial class Subform1 : Form
    {
        
        public Subform1(MainForm parent)
        {
            InitializeComponent();
            MdiParent = parent;
        }
        
        
    }

I would like for subform2 to be created by a button from subform1 with mainform as the mdi parent. How do I code this.

By writing the code that I have posted above! LOL... :D You didn't even look at it??

whats

"xxx"?????in if (exist("xxx", out m))??????

I still can't get the 2nd subform into the mdi form. I would like the 2nd subform to be created after double clicking an item in subform1 listbox.

public partial class Subform1 : Form
    {
     
    public Subform1(MainForm parent)
    {
    InitializeComponent();
    MdiParent = parent;
    }
     private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            subform2 melon = new subform2();            
            melon.TopLevel = false;
            melon.Show();
        }
     
    }

If I set TopLevel to true then then a window pops up but a window shows on the taskbar also. If I set it to false then no taskbar window appears but I don't see any new window within my mdi form.

Try that one... You'll need to check which one is Mdi or not...

Form m;
            if (Exists("xxx", out m))
            {
                m.Activate();
            }
            else
            {
               m = new xxx();
                m.Name = "xxx";
                m.MdiParent = this.MdiParent; //this.MdiParent.mdiparent.mdiparent bla bla               
 m.Show();
            }
        }
        public bool Exists(string name, out Form found)
        {
            foreach (Form subform in this.MdiParent.MdiChildren)
            {
                if (subform.Name == name)
                {
                    found = subform;
                    return true;
                }
            }
            found = null;
            return false;
        }

excuse me but what is this "xxx"????

excuse me but what is this "xxx"????

xxx = form name

I finally got it too work by using the code posted by symeramon above

m.MdiParent = this.MdiParent; //this.MdiParent.mdiparent.mdiparent bla bla

Not sure how I overlooked it.

I finally got it too work by using the code posted by symeramon above

m.MdiParent = this.MdiParent; //this.MdiParent.mdiparent.mdiparent bla bla

Not sure how I overlooked it.

can you post your code here mr magnetom???? thanks...

public partial class MainForm : Form
{
    public MainForm()
    {
         InitializeComponent();
    }
     // create the 1st child form
    private void button1_Click(object sender, EventArgs e)
    {
         Subform1 apple = new Subform1(this);
         apple.Show();
    }
}
public partial class Subform1 : Form
{
     //  constructor for 1st child form
    public Subform1(MainForm parent)
    {
         InitializeComponent();
         MdiParent = parent;
    }
    
    //create the 2nd child form
    private void listBox1_DoubleClick(object sender, EventArgs e)
    {
         subform2 melon = new subform2();
         melon.MdiParent = this.MdiParent;
         melon.Show();
    }
     
}

I think the indented is off but this is the code

are your forms not duplicating?? i mean when u click the button again and again it will continue to open subforms right???

Yes they duplicate

use my code on the first page.. that will work.. the for each code :p

Could you please mark this problem as solved if my code helps you???

how do you mark this as solved????

At the end of the Thread form you'll see that link...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.