0

I have a tab control that has many tabs. I only want to show 3 tabs to the user and which three depends on which combination of choices they make with some radio buttons. I decided that in the radio buttons when the user clicks I will call a function and that will pass some data to create the tab page. Below is what I thought could have worked but it seems that I am creating a brand new tab rather than showing one in the collection that I hid.

   private void rdobtn_protocol_CheckedChanged(object sender, EventArgs e)
    {
       if (rdobtn_prot1.Checked) Showtab("tab_prot1");
       if (rdobtn_prot2.Checked) Showtab("tab_prot2");
       if (rdobtn_prot3 .Checked) Showtab("tab_prot3");
    }       


    private void ShowTab(string tab)
    {
            tabControl1.SuspendLayout(); //Stop it running
            tabControl1.TabPages.Clear(); // remove all the tabs

            // Add controls in correct order
            tabControl1.TabPages.Add(tab_control_centre); //always 1st tab
            
            TabPage myTabPage = new TabPage(tab); //show the correct one
            tabControl1.TabPages.Add(myTabPage);

            tabControl1.TabPages.Add(tab_data_centre); //always last tab

            tabControl1.ResumeLayout(); //Restart it running
     }

Next I removed this

           TabPage myTabPage = new TabPage(tab); //show the correct one

and changed this

            tabControl1.TabPages.Add(tab);

Still no good I still create a new Tab. If I add the name direct as it is in the collection as below it works but then I cannot pass the string in. This will mean I have to hard code it and there are potentially a lot of tabs.

          tabControl1.TabPages.Add(tab_prot1);

My question is how do I show tabs I have hidden by calling them in a function and passing in the "tab" I want to show. Hope that makes sense.

user3884423
  • 556
  • 1
  • 5
  • 20

2 Answers2

2

As stated in How to hide TabPage from TabControl you cannot hide tabs in winforms; you have to add them and remove them the way you are trying. I would just suggest to use a dictionary of tabs and a method that creates tabs automatically if they don't exist to manage all the created tabs so you don't have to create them each time. Something like:


   
    private void rdobtn_protocol_CheckedChanged(object sender, EventArgs e)
    {
       List<TabPage> ListTabsShown = new List<TabPage>();

       //If these tabs are exclusive, use elses 
       if (rdobtn_prot1.Checked)
          ListTabsShown.Add(GetTabPage("tab_prot1"));
       /*else*/ if (rdobtn_prot2.Checked) 
          ListTabsShown.Add(GetTabPage("tab_prot2"));
       /*else*/ if (rdobtn_prot3 .Checked)
          ListTabsShown.Add(GetTabPage("tab_prot3"));

       Showtabs(ListTabsShown.ToArray());
    }       

   private Dictionary<string, TabPage> _DictTabPages = new Dictionary<string, TabPage>();

   private TabPage GetTabPage(string tabName)
   {
        TabPageSelectedTabPage;
        if(!_DictTabPages.TryGetValue(tabName, out SelectedTabPage))
        {
             SelectedTabPage = new TabPage(tabName);
             _DictTabPages.Add(tabName, SelectedTabPage);
        }

         return SelectedTabPage;
    }



    private void ShowTabs(params TabPage[] ListTabToShow)
    {
          tabControl1.SuspendLayout(); 
          tabControl1.TabPages.Clear(); 

          // Add controls in correct order
          tabControl1.TabPages.Add(tab_control_centre); //always 1st tab
            
          foreach(TabPage tp in ListTabToShow)
              tabControl1.TabPages.Add(tp);

          tabControl1.TabPages.Add(tab_data_centre); //always last tab

          tabControl1.ResumeLayout(); 
     }

Hope this answers your question

Amo Robb
  • 810
  • 5
  • 11
0

Thanks for the answer. I must admit I never thought of doing that so thanks for showing me something always keen to learn. I now have two ways of doing the same thing which is good.

I also played round with the code some more today and had a lightbulb moment. I was not looking the tab but creating a new one so what I was doing was working just not doing what I wanted it to and the more I tried the more I convinced myself it was wrong. Nothing like finding I am actually wrong but then I am human so now I tried the following:

private void rdobtn_ax_protocol_CheckedChanged(object sender, EventArgs e)
{

    TabPage myTabPage = tab_ax_codenet;

    if (rdobtn_ax_ic0.Checked) myTabPage = tab_prot1;
    if (rdobtn_ax_ic5.Checked) myTabPage = tab_prot2;

    Showtab(myTabPage);
}

private void Showtab(TabPage tab)
{
    tabControl1.SuspendLayout(); //Stop it running
    tabControl1.TabPages.Clear(); // remove all the tabs

    // Add controls in correct order
    tabControl1.TabPages.Add(tab_control_centre);
    tabControl1.TabPages.Add(tab);
    tabControl1.TabPages.Add(tab_data_centre);
    

    tabControl1.ResumeLayout(); //Restart it running
}
user3884423
  • 556
  • 1
  • 5
  • 20