Question

I am learner in c# and I have a small doubt.

In my windows form page, I have a tabControl contains 3 tabPages.I also have a comboBox outside the tabControl with 3 items listing the names of tab pages.

What i want to do is select the name of the first tabPage from the comboBox, and only that tabPage should be displayed by hiding the other tabPages.

Visibility property is not applicable to tabPages. So how can i do this?

Thanks in advance.

Was it helpful?

Solution

According to Hiding and Showing Tabpages in a Tabcontrol by Debasmit Samal:

Visiblity property has not been implemented on the TabControl, and there is no Insert method also. Some workaround on this

private void HideTabPage(TabPage tp)
{
    if (tabControl1.TabPages.Contains(tp))
        tabControl1.TabPages.Remove(tp);
}


private void ShowTabPage(TabPage tp)
{
    ShowTabPage(tp, tabControl1.TabPages.Count);
}


private void ShowTabPage(TabPage tp , int index)
{
    if (tabControl1.TabPages.Contains(tp)) return;
    InsertTabPage(tp, index);
}


private void InsertTabPage(TabPage tabpage, int index)
{
    if (index < 0 || index > tabControl1.TabCount)
        throw new ArgumentException("Index out of Range.");
    tabControl1.TabPages.Add(tabpage);
    if (index < tabControl1.TabCount - 1)
        do 
        {
            SwapTabPages(tabpage, (tabControl1.TabPages[tabControl1.TabPages.IndexOf(tabpage) - 1]));
        }
        while (tabControl1.TabPages.IndexOf(tabpage) != index);
    tabControl1.SelectedTab = tabpage;
}


private void SwapTabPages(TabPage tp1, TabPage tp2)
{
    if (tabControl1.TabPages.Contains(tp1) == false || tabControl1.TabPages.Contains(tp2) == false)
        throw new ArgumentException("TabPages must be in the TabControls TabPageCollection.");

    int Index1 = tabControl1.TabPages.IndexOf(tp1);
    int Index2 = tabControl1.TabPages.IndexOf(tp2);
    tabControl1.TabPages[Index1] = tp2;
    tabControl1.TabPages[Index2] = tp1;

    //Uncomment the following section to overcome bugs in the Compact Framework
    //tabControl1.SelectedIndex = tabControl1.SelectedIndex; 
    //string tp1Text, tp2Text;
    //tp1Text = tp1.Text;
    //tp2Text = tp2.Text;
    //tp1.Text=tp2Text;
    //tp2.Text=tp1Text;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top