C#使用Form的控件在运行时添加选项卡

时间:2022-08-16 15:51:06

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

编辑我正在使用3.5

3 个解决方案

#1


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

首先创建一个用户控件并像常规表单一样设计它,然后将其添加到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:

如果您要做的就是将控件从一个窗体上的TabControl复制到另一个窗体上的TabControl:

  1. Instantiate the form you want to copy from
  2. 实例化要从中复制的表单

  3. Find the TabControl on that form
  4. 在该表单上找到TabControl

  5. Iterate through Controls on the TabPages inside that TabControl
  6. 通过TabControl中的TabPages迭代控件

  7. Add each Control you find in that collection to the tab(s) in the control you want to copy to
  8. 将您在该集合中找到的每个控件添加到要复制到的控件中的选项卡

  9. Close and Dispose of the form you created in step 1
  10. 关闭并处置您在步骤1中创建的表单

#3


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

对的,这是可能的。您必须将控件添加到TabPage,然后将TabPage添加到TabControl.TabPages

#1


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

首先创建一个用户控件并像常规表单一样设计它,然后将其添加到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:

如果您要做的就是将控件从一个窗体上的TabControl复制到另一个窗体上的TabControl:

  1. Instantiate the form you want to copy from
  2. 实例化要从中复制的表单

  3. Find the TabControl on that form
  4. 在该表单上找到TabControl

  5. Iterate through Controls on the TabPages inside that TabControl
  6. 通过TabControl中的TabPages迭代控件

  7. Add each Control you find in that collection to the tab(s) in the control you want to copy to
  8. 将您在该集合中找到的每个控件添加到要复制到的控件中的选项卡

  9. Close and Dispose of the form you created in step 1
  10. 关闭并处置您在步骤1中创建的表单

#3


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

对的,这是可能的。您必须将控件添加到TabPage,然后将TabPage添加到TabControl.TabPages