A TabControl may only display one of its TabPages at a time. A TabControl can show/hide a TabPage's Text but it cannot show more than one TabPage at a time.
A TabControl contains a collection of TabPages. For that reason, we use Add and Remove to show and hide individual TabPages.
I'm not sure why you are using CheckBoxes to control the display of the individual TabPages. The TabControl recognizes a click on a TabPage tag and brings that TabPage to the front. I think that your use of CheckBoxes makes for an unnecessarily complex UI.
In the designer for the following code:
- A Form (Form1) was declared
- A TabControl (tab_control) was added to the Form
- Into the TabControl's TabPage collection were added the TabPages
(tb1, tb2, tb3) - On each TabPage, an appropriate Label was added (tb1_LAB, tb2_LAB,
tb3_LAB) - Three CheckBoxes (chk1, chk2, chk3) were added to the Form
using System;
using System.Windows.Forms;
namespace TabPageExample
{
public partial class Form1 : Form
{
void initialize_tb1 ( )
{
tb1_LAB.Text = @"This is TabPage tb1";
}
void initialize_tb2 ( )
{
tb2_LAB.Text = @"This is TabPage tb2";
}
void initialize_tb3 ( )
{
tb3_LAB.Text = @"This is TabPage tb3";
}
void initialize_GUI ( )
{
tab_control.TabPages.Remove ( tb1 );
tab_control.TabPages.Remove ( tb2 );
tab_control.TabPages.Remove ( tb3 );
chk1.Checked = false;
chk2.Checked = false;
chk3.Checked = false;
initialize_tb1 ( );
initialize_tb2 ( );
initialize_tb3 ( );
chk1.Checked = true;
chk2.Checked = true;
chk3.Checked = false;
}
public Form1 ( )
{
InitializeComponent ( );
initialize_GUI ( );
}
void chk_CheckedChanged ( object sender,
EventArgs e )
{
CheckBox check_box = ( CheckBox ) sender;
bool is_checked = check_box.Checked;
string tag = check_box.Tag.ToString ( ).
ToLower ( ).
Trim ( );
switch ( tag )
{
case "chk1":
if ( is_checked )
{
tab_control.TabPages.Add ( tb1 );
}
else
{
tab_control.TabPages.Remove ( tb1 );
}
break;
case "chk2":
if ( is_checked )
{
tab_control.TabPages.Add ( tb2 );
}
else
{
tab_control.TabPages.Remove ( tb2 );
}
break;
case "chk3":
if ( is_checked )
{
tab_control.TabPages.Add ( tb3 );
}
else
{
tab_control.TabPages.Remove ( tb3 );
}
break;
default:
throw new ApplicationException (
String.Format (
"Unrecognized CheckBox tag ({0})",
tag ) );
}
}
}
}
The project files for this example may be found at TabPageControl.zip (11.4 KB)[^].
Hope that helps