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)?