TabPage Visable/hide howto?

Neither Hide() nor setting Visible=False will work with TabPages. You can remove them through the TabControl’s Controls collection or the TabPages collection using the Remove method.

For example, use one of the two lines below (they do the same thing):

// OR

Keep in mind that if you want to add the tabpages back in later, you have to add them in the order you want them. That may mean removing them all and adding them back in one at a time.

// Add the TabPage add the end

// Reset the order of the tabpages
  New System.Windows.Forms.Control()
     Me.TabPage3, Me.TabPage2, Me.TabPage1

The next code can be found @ Hiding & Showing Tabpages

private void HideTabPage(TabPage tp)
  if (tabControl1.TabPages.Contains(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.");
  if (index < tabControl1.TabCount - 1)
            (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;

Leave a Reply