0

I am working on Windows Forms app using C#. The main form contains TabControl and checkboxes. The tab pages of the TabControl contains child forms. Then the checkboxes shall open and close specific tab pages on check and uncheck, respectively. The tab pages initially do not exist on load.

Here is what I did (child form is Form3 and the concerned TabControl is tabForms):

private void checkBox1_CheckStateChanged(object sender, EventArgs e)
    {

        Form1 f1 = new Form1();
        f1.TopLevel = false;
        TabPage tp1 = new TabPage(f1.Text);


        if (checkBox1.Checked == true)
        {
            tabForms.TabPages.Add(tp1);
            tp1.Show();
            f1.Parent = tp1;
            f1.Show();
        }
        else
        {
            tp1.Hide();
            tabForms.TabPages.Remove(tp1);
            f1.Dispose();
        }
    }

With this code, opening the tab was not a problem. However, when I unchecked checkBox1, the tab page won't close and when I checked it again, it opened another of the same tab page.

What did I miss or what shall I do to rectify this (if my aim was possible that is)?

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
eleu
  • 27
  • 3
  • `new TabPage(f1.Text);` creates a *new" TabPage. No point in hiding it if it was never added to a TabControl. You have to find the existing TabPage. You probably don't want to make a new Form1 either, I'm guessing. Always hold and pass the references of existing objects if you need them. – LarsTech Jan 01 '18 at 18:20
  • The `tp1` object you are trying to hide is the one you just created at the top of the method not the one created the first time it was checked. Why do you need to put a form in a tab - they are both container controls – Ňɏssa Pøngjǣrdenlarp Jan 01 '18 at 18:22
  • I see. From what I have understood, I should make `tp1` created already but hidden on load? – eleu Jan 01 '18 at 18:26
  • [TabControl with Close and Add Button](https://stackoverflow.com/q/36895089/3110834) – Reza Aghaei Jan 02 '18 at 05:17

1 Answers1

1

Your code creates a brand new TabPage control instance whenever the CheckBox state changes. This is fine as long as you have to add a TabPage, but not when you are attempting to remove an existing tab.

In the second case, you try to remove a new TabPage instance from the pool of pages contained within the TabControl. This obviously produces nothing, since the new has never been added to the TabControl:

TabPage tp1 = new TabPage(f1.Text);
tabForms.TabPages.Remove(tp1); //  instance not found, nothing is removed

Use the following approach instead, which hides the existing TabPage and then reuses it as needed:

private TabPage m_MyTabPage = new TabPage();

private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
    Form1 f1 = new Form1();
    f1.TopLevel = false;

    if (checkBox1.Checked)
    {
        m_MyTabPage.Text = f1.Text;
        tabForms.TabPages.Add(m_MyTabPage);

        tp1.Show();
        f1.Parent = tp1;
        f1.Show();
    }
    else
    {
        tp1.Hide();
        tabForms.TabPages.Remove(m_MyTabPage);
        f1.Dispose();
    }
}
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98