Using TabControl Programmatically
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?
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);
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.
精彩评论