C#—Dev XtraTabControl动态增加Tab和关闭选项卡方法
点击关闭按钮关闭
若要tabpage显示关闭按钮,把xtraTabControl的ClosePageButtonShowMode属性设为InAllTabPageHeaders;若想要首页不显示关闭按钮:
添加xtraTabControl的CloseButtonClick事件:
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; } } }
2.双击标签关闭
得到你现在鼠标所在的Page
XtraTabPage CurrentPage = new XtraTabPage(); private void xtraTabControl_HotTrackedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e) { try { CurrentPage = e.Page; } catch (System.Exception ex) { CurrentPage = null; } }
private void xtraTabControl_MouseDoubleClick(object sender, MouseEventArgs e) { if (CurrentPage != null) { xtraTabControl.TabPages.Remove(CurrentPage); CurrentPage.Dispose(); } }