[DevExpress]DevExpress XtraTabControl 加载Form

代码:

private void treeList1_DoubleClick(object sender, EventArgs e)
        {
          TreeListNode clickedNode = this.treeList1.FocusedNode;       
            
           // string disPlayText = clickedNode.GetDisplayText("FORMNAME");


            string name = clickedNode.GetDisplayText("NAME");
            string formname = clickedNode.GetDisplayText("FORMNAME");

            if (name != "")        //trvMeun.SelectedNode.LastNode == null
            {
                //判断是否已创建过
                foreach (XtraTabPage page in xtraTabControl.TabPages)
                {
                    if (page.Text == name)
                    {
                        xtraTabControl.SelectedTabPage = page;//显示该页
                        return;
                    }
                }

                XtraTabPage xpage = new XtraTabPage();
                ObjectHandle obj = Activator.CreateInstance(null, formname);   //需要带全局项目
                //obj.Unwrap返回被包装的对象
                if (obj != null)
                {
                    Form frm = (Form)obj.Unwrap();

                    frm.Visible = true;
                    frm.Dock = DockStyle.Fill;
                    frm.FormBorderStyle = FormBorderStyle.None;
                    frm.TopLevel = false;//在这里一定要注意  负责是加载不出来的

                    xpage.Controls.Add(frm);//添加要增加的控件
                    xpage.Text = name;     //添加名称
                    xtraTabControl.TabPages.Add(xpage);
                    xtraTabControl.SelectedTabPage = xpage;//显示该页
                }

       
            }
        }
        XtraTabPage CurrentPage = new XtraTabPage();
        private void xtraTabControl_CloseButtonClick(object sender, EventArgs e)
        {
            DevExpress.XtraTab.ViewInfo.ClosePageButtonEventArgs EArg = (DevExpress.XtraTab.ViewInfo.ClosePageButtonEventArgs)e;
            string name = EArg.Page.Text;//得到关闭的选项卡的text
            foreach (XtraTabPage page in xtraTabControl.TabPages)//遍历得到和关闭的选项卡一样的Text
            {
                if (page.Text == name)
                {
                    xtraTabControl.TabPages.Remove(page);
                    page.Dispose();
                    return;
                }
            }
        }

        private void xtraTabControl_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (CurrentPage != null)
            {
                xtraTabControl.TabPages.Remove(CurrentPage);
                CurrentPage.Dispose();
            }
        }

效果:

[DevExpress]DevExpress XtraTabControl 加载Form_第1张图片

你可能感兴趣的:([DevExpress]DevExpress XtraTabControl 加载Form)