Winfrom多文档界面实现
闲来无事,研究了下比较常用的多文档界面风格,网上找了好多例子,最终实现。记录一下
废话不多说,先上个效果:
默认打开我的桌面,首页不允许关闭,xtraTabPage动态添加。
下面来看下实现过程:
1、先创建一个主窗体
拖入xtraTabControl控件
设置xtraTabControl,Dock属性为Fill
设置ClosePageButtonShowMode属性为InAllTabPageHeaders
2、再创建多个XtraUserControl页面
代码:
打开默认显示首页
private void Ribbonmain_Load(object sender, EventArgs e)
{
Ucs2 ucs2 = new Ucs2();
ucs2.Dock = DockStyle.Fill;
ucs2.Visible = true;
xtraTabPage1.Controls.Add(ucs2);
//xtraTabControl1.Controls.Add(ucs2);
xtraTabControl1.SelectedTabPage = xtraTabPage1;
xtraTabPage1.ShowCloseButton = DevExpress.Utils.DefaultBoolean.False;//首页不显示关闭按钮
}
动态创建Page方法
void OpenUserControlXtab(UserControl userControl)
{
//String name = userControl.Text;
String name = userControl.Name;
foreach (XtraTabPage XTpage in xtraTabControl1.TabPages)
{
if (XTpage.Text == name)
{
xtraTabControl1.SelectedTabPage = XTpage;
return;
}
}
XtraTabPage page = new XtraTabPage();
page.Controls.Add(userControl);
//page.Text = userControl.Text;
page.Text = userControl.Name;
xtraTabControl1.TabPages.Add(page);
xtraTabControl1.SelectedTabPage = page;
//设置关闭事件
xtraTabControl1.CloseButtonClick += new EventHandler(XtraTabControl1CloseButtonClick);
}
//在程序关闭后 要释放资源
void XtraTabControl1CloseButtonClick(object sender,EventArgs e)
{
ClosePageButtonEventArgs a = (ClosePageButtonEventArgs)e;
string tabpagename = a.Page.Text;
foreach (Control xtp in xtraTabControl1.TabPages)
{
if (xtp.Text == tabpagename)
{
xtp.Dispose();
return;
}
}
}
按钮事件:
private void navBarItem1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
{
Ucs1 ucs1 = new Ucs1()
{
Visible = true,
Dock = DockStyle.Fill,
//BorderStyle = FormBorderStyle.None,
//TopLevel = false//在这里一定要注意 否则是加载不出来的
};
OpenUserControlXtab(ucs1);
}