C#动态增加Tab和关闭选项卡

作者:袖梨 2022-06-25

记录一下以免以后忘了

添加using DevExpress.XtraTab;

双击listview增加相应的tabpage,禁止重复添加:

 代码如下 复制代码

private void listView_index_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listView_index.SelectedItems.Count > 0)
            {
                ListViewItem lvi = listView_index.SelectedItems[0];//得到选中的项
                string name = lvi.Text;
                //判断是否已创建过
                foreach (XtraTabPage page in xtraTabControl.TabPages)
                {
                    if (page.Text == name)
                    {
                        xtraTabControl.SelectedTabPage = page;//显示该页
                        return;
                    }
                }
                //增加tabpage
                XtraTabPage xpage = new XtraTabPage();
                xpage.Name = name;
                xpage.Text = name;
                xpage.Controls.Add(ListView_Tec);//添加要增加的控件
                xtraTabControl.TabPages.Add(xpage);
                xtraTabControl.SelectedTabPage = xpage;//显示该页
            }
        }

若要tabpage显示关闭按钮,把xtraTabControl的ClosePageButtonShowMode属性设为InAllTabPageHeaders;若想要首页不显示关闭按钮:

xtraTab_index.ShowCloseButton = DevExpress.Utils.DefaultBoolean.False;//首页不显示关闭按钮

添加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;
                }
            }
        }

相关文章

精彩推荐