Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a form within a form?

Tags:

c#

winforms

I have a Parent form and i like to open a child form within the the parent form.

Can this be done? If yes please reply me with sample code .

Thanks !

like image 627
cmthakur Avatar asked Jun 22 '11 04:06

cmthakur


People also ask

How do you call one form from another form?

There are two common ways to call a form from another form within Dynamics 365 for Finance and Operations. You can use a menu item button. Or you can use use X++ code to call another form. In this article I will show you how to use an Menu Item Button.


3 Answers

Following is the code to do what you want:

Assume that button1 is in the parent form.

private void button1_Click(object sender, EventArgs e)
        {
            this.IsMdiContainer = true;
            Form Form2 = new Form();
            Form2.MdiParent = this;
            Form2.Show();
        }

Also the following link will provide you more better details of what you want to do:

http://www.codeproject.com/KB/cs/mdiformstutorial.aspx

Hope this helps...

like image 67
Dulini Atapattu Avatar answered Oct 17 '22 23:10

Dulini Atapattu


I note that all the answers here assume the OP intended to use MDI Form architecture, although that's never explicitly stated.

And there is another way a Form can be made a 'Child' of another Form: by simply setting its 'TopLevel property to 'False, and then setting its 'Parent property to the other Form.

Form2 f2 = new Form2();
f2.TopLevel = false;
f2.Parent = someOtherForm;
f2.Show();

By the way I think the whole idea of 'Forms within Forms' is a BAD idea, and MDI Architecture is now, justifiably, deprecated by MS.

Much better, I believe, to make secondary Forms 'Owned, and if you must have other Containers inside a Form, use UserControls, Panels, etc.

like image 24
BillW Avatar answered Oct 17 '22 23:10

BillW


It depends on what you mean by "within the form". If you need to have the child form shown as a control of the parent form I guess you could try ParentForm.Controls.Add(new ChildForm()). Or maybe even place the child form in an existing container in the parent form by again using the containing control's Controls collection.

HTH

like image 6
Eben Roux Avatar answered Oct 17 '22 22:10

Eben Roux