none
Show/Hide TabPage programmatically RRS feed

  • Question

  • I need to show/hide tabpage dynamically at runtime.

    I know TabPage desn't have show/hide and should remove/add.

    I already put controls in the tabpages and want to remove/add at runtime.

    I can add/remove tabpage but can't handle the controls in it.

    Anybody have idea about this?

    Monday, December 10, 2018 8:42 PM

Answers

  • Greetings Jeff.

    Remove and Add should work, provided you keep the page you want to hide/show, and don't go creating a 'new' one every time.

          // The page we want to hide/show.
          TabPage pageToHide;
    
          // A flag to say if the page is currently hidden or not.
          bool hidden = false;
    
          // A button click to show or hide the page.
          private void button1_Click(object sender, EventArgs e)
          {
             // Set the page to hide/show. Hardcoded in this case to the third page on the control.
             if (pageToHide == null)
             {
                pageToHide = tabControl1.TabPages[2];
             }
    
             // Perform the appropriate action.
             if (hidden)
                tabControl1.TabPages.Add(pageToHide);
             else
                tabControl1.TabPages.Remove(pageToHide);
    
             // Toggle the flag to say if the page is hidden or not, ready for next time.
             hidden = !hidden;
          }
    


    • Marked as answer by Jeff0803 Tuesday, December 11, 2018 5:20 AM
    Monday, December 10, 2018 11:16 PM

All replies

  • Greetings Jeff.

    Remove and Add should work, provided you keep the page you want to hide/show, and don't go creating a 'new' one every time.

          // The page we want to hide/show.
          TabPage pageToHide;
    
          // A flag to say if the page is currently hidden or not.
          bool hidden = false;
    
          // A button click to show or hide the page.
          private void button1_Click(object sender, EventArgs e)
          {
             // Set the page to hide/show. Hardcoded in this case to the third page on the control.
             if (pageToHide == null)
             {
                pageToHide = tabControl1.TabPages[2];
             }
    
             // Perform the appropriate action.
             if (hidden)
                tabControl1.TabPages.Add(pageToHide);
             else
                tabControl1.TabPages.Remove(pageToHide);
    
             // Toggle the flag to say if the page is hidden or not, ready for next time.
             hidden = !hidden;
          }
    


    • Marked as answer by Jeff0803 Tuesday, December 11, 2018 5:20 AM
    Monday, December 10, 2018 11:16 PM
  • If tabpage's count is 4, then page order is changed like page1/page2/page4/page3.

    Can I order like page1/page2/page3/page4?

    Tuesday, December 11, 2018 6:11 AM
  • I resolved this using TabControl1.TabPages.Remove() and TabControl1.TabPages.InsertAt().
    Tuesday, December 11, 2018 4:15 PM