devExpress,xtraTabControl标签页关闭与添加(删除与添加)


using DevExpress.XtraTab;
using System;
using System.Windows.Forms;

namespace devExpress标签页
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UserControl1 uc1 = new UserControl1();
            AddControlToPage("测试第一页",uc1);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            UserControl2 uc2 = new UserControl2();
            AddControlToPage("测试第二页", uc2);
        }
        /// <summary>
        /// 将控件添加到标签页中
        /// </summary>
        /// <param name="PageName"></param>
        /// <param name="ctrl"></param>
        private void AddControlToPage(string PageName, Control ctrl)
        {
            try
            {
                bool isExists = false;
                XtraTabPage tempPage = null;

                for (int i = 0; i < this.xtraTabControl1.TabPages.Count; i++)
                {
                    if (this.xtraTabControl1.TabPages[i].Name == PageName)
                    {
                        tempPage = this.xtraTabControl1.TabPages[i];
                        isExists = true;
                        break;
                    }
                }

                if (!isExists)
                {
                    XtraTabPage page = new XtraTabPage();
                    page.Name = PageName;
                    page.Text = PageName;
                    ctrl.Dock = DockStyle.Fill;
                    page.Controls.Add(ctrl);

                    this.xtraTabControl1.TabPages.Add(page);
                    this.xtraTabControl1.SelectedTabPage = page;
                }
                else
                {
                    this.xtraTabControl1.SelectedTabPage = tempPage;
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void xtraTabControl1_CloseButtonClick(object sender, EventArgs e)
        {
            this.xtraTabControl1.TabPages.Remove(this.xtraTabControl1.SelectedTabPage);
        }
    }
}



最后还要注意一下 ,xtraTabControl控件的属性要像以下设置,不然删除不了页面


评论