user141594
user141594

Reputation: 239

C# Adding tabs at runtime using Form's controls

I have thought of this idea where you could have a tab control on a form, but instead of defining each tabs controls before runtime, inherit or use another form's controls.

Basically a tab control is in place on the main form that has no tabs on it before runtime. When runtime comes along I want to create tabs, but the controls that would be on each tab would be from seperate already created forms.

Each form would be a seperate tab that had been created prior to runtime.

Is this possible? Is so, how?

Thanks in advance

EDIT I'm using 3.5

Upvotes: 2

Views: 5351

Answers (3)

SwDevMan81
SwDevMan81

Reputation: 50038

Yes, it is possible. You have to add the controls onto the TabPage, then add the TabPage to the TabControl.TabPages

Upvotes: 1

Michael Todd
Michael Todd

Reputation: 17081

If all you'll be doing is copying controls from a TabControl on one form to a TabControl on another form:

  1. Instantiate the form you want to copy from
  2. Find the TabControl on that form
  3. Iterate through Controls on the TabPages inside that TabControl
  4. Add each Control you find in that collection to the tab(s) in the control you want to copy to
  5. Close and Dispose of the form you created in step 1

Upvotes: 1

Stan R.
Stan R.

Reputation: 16065

first create a User Control and design it as you would a regular form, then add it to your TabControl.TabPages

TabPage page = new TabPage("Title");
page.Controls.Add(new CustomUserControl()); //your user control

this.tabControl1.TabPages.Add(page);

Upvotes: 4

Related Questions