简体   繁体   English

在c#中停靠mdi控件

[英]Docking mdi controls in c#

I have a main form in a panel on the left thats clickable, depending on what you click a new type of form opens. 我在左侧的面板中有一个主要表单可点击,具体取决于您单击一种新类型的表单打开。 on the righti have another panel where i want to dock the forms that have been opened from clicking on the left. 在righti有另一个面板,我想停靠点击左侧打开的表格。

How can i get the forms to add in a list under one another in the panel on the right? 如何在右侧面板中将表单添加到另一个列表中? the issue with the code below is that it adds the first element fine. 下面的代码的问题是它添加第一个元素罚款。 However when i add the second element they both dissapear behind the panel :/ 但是,当我添加第二个元素时,它们都会在面板后面消失:/

private void addToPanel2(Form o)
{
    if (o is Form)
    {

        if (panel2.Controls.Count == 0)
        {
            o.MdiParent = this;
            panel2.Controls.Add(o);
            o.Dock = DockStyle.Top;
            o.Show();
        }
        else
        {
            //then we know that this is an addable data item
            foreach (Form obj in panel2.Controls)
            {
                if(obj.GetType().Name.Equals(o.GetType().Name))
                {
                    //we dont want to add it as the data type is already open
                    MessageBox.Show("This data item must already be open. Please Check.");
                }
                else
                {
                    // add it as its not in there
                    Form f = (Form)obj;
                    f.MdiParent = this;
                    f.Dock = DockStyle.Top;
                    f.Show();
                }
            }
        }
    }

thanks 谢谢

This is not possible, an MDI child form cannot be a child control of a panel. 这是不可能的,MDI子表单不能是面板的子控件。 Adding a non-MDI form to a panel is an iffy proposition as well but is supported. 向面板添加非MDI表单也是一个不确定的主张,但是受到支持。 Call its SetTopLevel() method, passing false, set its Visible property to true. 调用其SetTopLevel()方法,传递false,将其Visible属性设置为true。 You also have to set its FormBorderStyle property to None, it no longer behaves properly as a top-level window. 您还必须将其FormBorderStyle属性设置为None,它不再作为顶级窗口正常运行。

This just turns it into a UserControl. 这只是将它变成UserControl。 You are better off actually making it a UserControl, that uses a lot less resources and is much better documented. 你最好把它变成一个UserControl,它使用更少的资源并且记录得更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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