1
votes

Situation: I'm writing a program in which the user clicks a button to create tabs in a TabControl. The user has created 5 tabs and then wants to delet Tab 3. This leaves tabs 1, 2, 4 and 5.

How can I re-order the tabs to fill the gap (Tab 4 becomes the new Tab 3, Tab 5 becomes Tab 4, etc.)? Does the TabControl have a built-in function for this?

1

1 Answers

0
votes

refer to linke : MSDN Add And Remove Tab From Tabcontrol

// Removes the selected tab:
tabControl1.TabPages.Remove(tabControl1.SelectedTab);
// Removes all the tabs:
tabControl1.TabPages.Clear();

if you want remove tab by index

tabControl1.TabPages.RemoveAt(i);

use a Tabpage Array that is collection of all tabpages , Search in TabPage array by TabControl.SelecetedIndex , to find Index of TabPage After Deletion then you can do anything this means that you just delete Tabpage from TabControl , but not from TabPage Array and when need one of them search in TabControl.you can relate Selected Tabpage to Tabpage array by set Tag Property to Real Index of Array. use this 2 function :

TabPage[] tabPages;
private void MyForm_Load(object sender, EventArgs e)
{ 
       tabPages = new TabPage[tabServices.TabPages.Count];
        for (int i = 0; i < tabServices.TabPages.Count; i++)
        {
            tabServices.TabPages[i].Tag=i;//for relation between TabControl and Array
            tabPages[i] = tabServices.TabPages[i];
        }
 }

private int GetTabIndex(TabPage pg)
    {
        return int.Parse(tabServices.TabPages[tabServices.SelectedIndex].Tag.ToString());
    }


    private TABPAGE GetTABPAGE(TabPage pg)
    {
        return (TABPAGE)int.Parse(tabServices.TabPages[tabServices.SelectedIndex].Tag.ToString());
    }

    private void tabServices_SelectedIndexChanged(object sender, EventArgs e)
    {
            switch (GetTABPAGE(tabServices.TabPages[tabServices.SelectedIndex]))
            {
                 case TABPAGE.TabP1:
                   //Do Works;
                  break;  
                 case TABPAGE.TabP2:
                   //Do Works;
                  break;  
                 case TABPAGE.TabP3:
                   //Do Works;
                  break;  
                 case TABPAGE.TabP4:
                         //Do Works;
                  break;  
            }
    }

and define an Enum for All TabPages (for ReadAbility and Index to Tabpage meaningful name ):

public enum TABPAGE { TabP1 = 0, TabP2 = 1, TabP3 = 2};