C# Adding tabs at runtime using Form's control

2019-05-25 08:48发布

问题:

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

回答1:

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);


回答2:

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


回答3:

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