0

I would like to know if it is possible to show/place a form1 INSIDE form2. So that form1 is stuck inside form2.

Any help will be appreciated. thanks in advance.

4

1 回答 1

2

是的。您要查找的词是MDI或多文档界面。

只需将 form2 的IsMdiContainer属性设置为true 并将 form1 的MdiParent属性设置为 form2,如下所示:

form2.IsMdiContainer = true;
form1.MdiParent = form2;

当然,您可以通过在属性部分设置属性来让视觉设计器为您编写第一行:

在此处输入图像描述

编辑

这是一个简单的例子。假设您有 2 个表格。FormContainerFormChildFormContainer是应用程序的主要形式。

您需要做的就是确保FormContainer' 的IsMdiContainer属性设置为true,然后您可以通过设置这些实例的MdiParent属性来将其他表单的实例添加到此表单中。除了主窗体,Form类或子类的任何实例默认情况下都是不可见的。

public partial class FormContainer : Form {

    public FormContainer() {
        InitializeComponent();
        this.IsMdiContainer = true;
        // if you're not excited about the new form's backcolor
        // just change it back to the original one like so
        // note: The dark gray color which is shown on the container form
        //       is not it's actual color but rather a misterious' control's color.
        //       When you turn a plain ol' form into an MDIContainer
        //       you're actually adding an MDIClient on top of it

        var theMdiClient = this.Controls
            .OfType<Control>()
            .Where(x => x is MdiClient)
            .First();
        theMdiClient.BackColor = SystemColors.Control;
    }

    private void FormContainer_Load(object sender, EventArgs e) {
        var child = new FormChild();
        child.MdiParent = this;
        child.Show();

        // if you wish to specify the position, size, Anchor or Dock styles
        // of the newly created child form you can, like you would normally do
        // for any control
        child.Location = new Point(50, 50);
        child.Size = new Size(100, 100);
        child.Anchor = AnchorStyles.Top | AnchorStyles.Right;
    }

}
于 2013-09-17T15:05:58.353 回答