C# 在Panel上加载窗体

在Panel上加载一个窗体,类似MDI窗体,原以为像Delphi一样直接把form的parent设置为panel就可以实现.在写代码时没问题,运行时弹出error:Top-level control can not be added to a control(无法将顶级控件添加到控件),查看form的属性后发现要把TopLevel = false才能实现.最后的代码如下:

private void ShowFormAsChild(XtraForm AFrom, Control AParent)
        {
            this.Text = "Product Listing - " + AFrom.Text;
            for (int i = 0; i < panelControl1.Controls.Count; i++)
                if (panelControl1.Controls[i] is XtraForm)
                    ((XtraForm)panelControl1.Controls[i]).Hide();
            AFrom.FormBorderStyle = FormBorderStyle.None;
            AFrom.TopLevel = false;                       
            AFrom.Parent = AParent;
            AFrom.Dock = DockStyle.Fill;
            AFrom.Show();
            AFrom.BringToFront();
            AFrom.Focus();
        }

private ShowerFrm showerfrm = null;
private void navBarItem1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (showerfrm == null || showerfrm.IsDisposed)
                showerfrm = new ShowerFrm();
            ShowFormAsChild(showerfrm, panelControl1);
        } 

你可能感兴趣的:(Panel)