如何在选项卡式子窗体中解决此问题

本文关键字:窗体 解决 问题 选项 | 更新日期: 2023-09-27 18:06:12

这是什么错误:指定为此窗体的MdiParent的窗体不是MdiContainer。参数名称:value

是代码公共部分类Form5: Form{公共Form5 (){InitializeComponent ();}

    private void Form1_MdiChildActivate(object sender, EventArgs e)
    {
        if (this.ActiveMdiChild == null)
            tabForms.Visible = false; // If no any child form, hide tabControl
        else
        {
            this.ActiveMdiChild.WindowState = FormWindowState.Maximized; // Child form always maximized
            // If child form is new and no has tabPage, create new tabPage
            if (this.ActiveMdiChild.Tag == null)
            {
                // Add a tabPage to tabControl with child form caption
                TabPage tp = new TabPage(this.ActiveMdiChild.Text);
                tp.Tag = this.ActiveMdiChild;
                tp.Parent = tabForms;
                tabForms.SelectedTab = tp;
                this.ActiveMdiChild.Tag = tp;
                this.ActiveMdiChild.FormClosed += new FormClosedEventHandler(ActiveMdiChild_FormClosed);
            }
            if (!tabForms.Visible) tabForms.Visible = true;
        }
    }
    // If child form closed, remove tabPage
    private void ActiveMdiChild_FormClosed(object sender, FormClosedEventArgs e)
    {
        ((sender as Form).Tag as TabPage).Dispose();
    }
    private void tabForms_SelectedIndexChanged(object sender, EventArgs e)
    {
        if ((tabForms.SelectedTab != null) && (tabForms.SelectedTab.Tag != null))
            (tabForms.SelectedTab.Tag as Form).Select();
    }

    private void projectsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Form7 f7 = new Form7();
        f7.MdiParent = this;
        f7.Show();
    }

}

如何在选项卡式子窗体中解决此问题

问题可能是在您的Form5类中,您没有指定FormMdiContainer

尝试将IsMdiContainer属性设置为true或在构造函数中调用InitializeComponent后手动设置该属性