C# – Using TabControl Programmatically

ctabcontrol

If I have a standard TabControl element in Form design view, how can I programmatically create a new Tab with a Button which contains the preset elements such as TextBox, Button etc., or how can I set the Tab to load another Form within itself?

Is this possible?

Best Answer

You can create a new tab by calling tabControl.TabPages.Add.

You can then add other controls to the TabPage's Controls collection.

The simplest way to do this would be to make your own custom control, then add a new instance of the custom control to the TabPage, probably docked Fill.

For example:

var tabPage = tabControl.TabPages.Add("My Custom Tab");
var control = new MyCustomControl();
control.Dock = DockStyle.Fill;
//Set other properties if you want to.
tabPage.Controls.Add(control);
Related Topic