tags:

views:

230

answers:

2

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

Is this possible?

+5  A: 

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);
SLaks
This is very helpful, thank you. Do I need to include any additional libraries for this example to work?
BlindMatoya
No; this is the standard WinForms TabControl. However, you will need to create a custom control.
SLaks
+1  A: 

You can't directly load a Form into the content of a TabPage.

However, if all your controls are contained within a UserControl, the UserControl can be placed both inside the tab page and/or in a separate form.

Jon Seigel