Create TabContent dynamic in DevExpress

Create TabContent dynamic in DevExpress

September 19, 2017 0 By Nam Vu

Hàm tạo TabContent:

#region /************* Ađ tab****************/
private void addNewTab(string strTabName, UserControl ucContent)
{
DevExpress.XtraTab.XtraTabPage newTabPage = new DevExpress.XtraTab.XtraTabPage();
    newTabPage.Name = "xtraTabName" + strTabName;
    //Check exist tabContent
    foreach (DevExpress.XtraTab.XtraTabPage tabpage in tabContent.TabPages)
    {
        if (tabpage.Name == newTabPage.Name)
        {
            tabContent.SelectedTabPage = tabpage;
            return;
        }
    }
    //them tab moi
    tabContent.TabPages.Add(newTabPage);
    tabContent.SelectedTabPageIndex = tabContent.TabPages.Count - 1;
    //tao thuoc tinh cho tab moi
    newTabPage.Appearance.PageClient.BackColor = System.Drawing.Color.Cyan;
    newTabPage.Appearance.PageClient.Options.UseBackColor = true;
    //tao thuoc tinh cho UCcontent
    ucContent.Dock = DockStyle.Fill;
    newTabPage.Controls.Add(ucContent);
    newTabPage.ShowCloseButton = DevExpress.Utils.DefaultBoolean.True;
    newTabPage.Size = new System.Drawing.Size(929, 458);
    newTabPage.Text = strTabName;
}
private void tabContent_CloseButtonClick(object sender, EventArgs e)
{
    if (tabContent.SelectedTabPageIndex != 0)
    {
        try
        {
            int index = tabContent.SelectedTabPageIndex;
            tabContent.TabPages.RemoveAt(index);
            tabContent.SelectedTabPageIndex = index - 1;
        }
        catch (ThreadInterruptedException)
        {
            Console.WriteLine("[{0}] Interrupted! Cleaning up...", Thread.CurrentThread.Name);
        } 
    }
}
#endregion

Sau khi tạo hàm, muốn add bằng cách tạo UserControl và dùng câu lệnh sau để gọi tab:

SinhDayNhiPhan uc = new SinhDayNhiPhan();

addNewTab(“BT sinh dãy nhị phân”, uc);

Kết quả: