//选项卡 Dictionary<string, XtraTabPage> dictXtraTabPage = new Dictionary<string, XtraTabPage>(); Dictionary<string, Form> dictXtraForm = new Dictionary<string, Form>(); public void ShowMDIForm(string cText, Form frm) { //判断是否已创建过 if (dictXtraTabPage.ContainsKey(cText)) { xtraTabControl1.SelectedTabPage = dictXtraTabPage[cText]; return; } /*使用以下这段注释,需要把上面的if语句注释掉。 //判断是否已创建过,排除已创建的选项卡,重新加载窗体和传参。 if (dictXtraTabPage.ContainsKey(cText)&& frm.Text != "选项卡名称") { xtraTabControl1.SelectedTabPage = dictXtraTabPage[cText]; return; } //已经创建过选项卡,先移除它,重新加载,达到刷新窗体数据的目的) if (dictXtraTabPage.ContainsKey(cText) && frm.Text=="选项卡名称") { xtraTabControl1.TabPages.Remove(dictXtraTabPage["选项卡名称"]); dictXtraTabPage.Remove(cText); dictXtraForm.Remove(cText); } */ frm.Visible = true; frm.Dock = DockStyle.Fill; frm.FormBorderStyle = FormBorderStyle.None; frm.WindowState = FormWindowState.Maximized; frm.TopLevel = false;//注意这里,否则加载不出来 XtraTabPage xpage = new XtraTabPage(); xpage.Controls.Add(frm);//添加要增加的控件 xpage.Text = cText;//添加名称 xpage.ShowCloseButton = DevExpress.Utils.DefaultBoolean.True; xtraTabControl1.TabPages.Add(xpage); xtraTabControl1.SelectedTabPage = xpage;//显示该页 dictXtraTabPage.Add(cText, xpage);//加入XtraTabPage字典 dictXtraForm.Add(cText, frm);//加入XtraForm字典 } //关闭选项卡 private void xtraTabControl1_CloseButtonClick(object sender, EventArgs e) { ClosePageButtonEventArgs a = (ClosePageButtonEventArgs)e; string cText = a.Page.Text; if (dictXtraForm.ContainsKey(cText)) { Form form = dictXtraForm[cText] as Form; form.Close(); form.Dispose(); dictXtraForm.Remove(cText); xtraTabControl1.SelectedTabPageIndex = xtraTabControl1.SelectedTabPageIndex >= 1 ? xtraTabControl1.SelectedTabPageIndex - 1 : 0; } if (dictXtraTabPage.ContainsKey(cText)) { xtraTabControl1.TabPages.Remove((XtraTabPage)a.Page); dictXtraTabPage.Remove(cText); } } //菜单栏点击窗体 private void ToolStripMenuItem1_Click(object sender, EventArgs e) { Form form = new Form(); ShowMDIForm("选项卡名称",from); }
标签:xtraTabControl1,选项卡,Devexpress,frm,xpage,dictXtraTabPage,cText From: https://www.cnblogs.com/lmh6825/p/17624585.html