简体   繁体   中英

C# Winform Forms in Form

i want to open multiple instances of one form and display it in another form or paenel of another form. how to do it

If you're not using MDI, you can still add a form to another form, or to a panel on a form.

public Form1()
{
    InitializeComponent();

    Form2 embeddedForm = new Form2();
    embeddedForm.TopLevel = false;
    Controls.Add(embeddedForm);
    embeddedForm.Show();
}

You will need to set the FormBorderStyle to None , unless you want to have an actual movable form inside your form.

If you want to do this to create a reusable "template" to use in multiple forms, you should consider creating a user control instead. Not to be confused with a custom control, which is intended for when you need to do your own drawing instead of using collections of standard Windows controls.

I'm not entirely sure what your intentions are, but MDI (as mentioned in one of the other answers) might actually be what you're looking for.

You should:

1) Choose for MDI, that means accepting the complete MDI style for your GUI

or

2) Not embed Forms at all, it's better (and easier) to use UserControls. Try them out before you make a choice. If you do use Forms, make sure shortcut keys etc are all working like you want.

Short intro: You design a UserControl like a Form and then place it on a Form like any other Control.

Form1 fChild = new Form1();
fChild.MdiParent = this;
fChild.Show();

And the IsMDIContainer of the parent should be set to True.

For a complete tutorial, you can refer to : Introduction to MDI Forms with C# @ CodeProject .

I came across this older thread looking for something similar. In case anyone else is looking - I used Add New Item >Windows Forms > Inherited Form. It allows you to choose a form to embed as the starting point for another form. This was is in VS 2015 Community Edition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM