C#制作选项卡,向选项卡添加控件,然后使用该类型的多个选项卡

时间:2022-08-16 15:51:18
TabPage newpage = new TabPage();
Tabs.TabPages.Add(newpage);
newpage.Controls.Add(this.tableLayoutPanel41);
newpage.Location = new System.Drawing.Point(4, 26);
newpage.Name = "AddMaintAgreement" + offset;
newpage.Size = new System.Drawing.Size(736, 523);
newpage.TabIndex = 10;
newpage.Text = "Add Maintenance Agreement";
newpage.UseVisualStyleBackColor = true;
offset++;

Basically thats what i have at the moment, I added the offset in there because i thought it might affect my problem.

基本上就是我现在所拥有的,我在那里添加了偏移因为我认为它可能会影响我的问题。

Basically, this code here works okay for adding one "addmaintagreement" tab. After that only the latest tab has any controls on it!

基本上,这里的代码可以添加一个“addmaintagreement”选项卡。之后,只有最新的选项卡上有任何控件!

Basically I'm stumped. Any help would be appreciated. Thanks.

基本上我很难过。任何帮助,将不胜感激。谢谢。

3 个解决方案

#1


I think short example should stay here:

我想简短的例子应该留在这里:

        TextBox tmpLog = new TextBox(); // create new control of textbox type
        tmpLog.Text = "some text here";

        TabPage tb = new TabPage("my brand new tab"); //create tab
        tabControl.TabPages.Add(tb); // add tab to existed TabControl
        tb.Controls.Add(tmpLog); // add textBox to new tab

        tabControl.SelectedTab = tb;     // activate tab

#2


Derive from TabPage and add the controls you want in that derived class. Then use your derived class instead of TabPage.

从TabPage派生并在该派生类中添加所需的控件。然后使用派生类而不是TabPage。

#3


Controls can only be parented to one control, but it looks like you are trying to parent your tableLayoutPanel41 in every TabPage instance. You need to create new copies of the controls for each instance of the tab. There are various ways to fix this.

控件只能作为一个控件的父级,但看起来你试图在每个TabPage实例中为tableLayoutPanel41提供父级。您需要为选项卡的每个实例创建控件的新副本。有多种方法可以解决这个问题。

  1. Programmatically create your tab page and its contents multiple times.
  2. 以编程方式多次创建标签页及其内容。

  3. Have the contents of your TabPage implemented as a user control that you dock fill on a tab page. Then recreate one of those for each page duplicate your require.
  4. 将TabPage的内容实现为用户控件,并将其填充到选项卡页面上。然后为每个页面重新创建其中一个复制您的要求。

  5. Create a class derived from TabPage that implements your tab page and create new instances of that for each use.
  6. 创建一个派生自TabPage的类,该类实现您的标签页并为每次使用创建新的实例。

#1


I think short example should stay here:

我想简短的例子应该留在这里:

        TextBox tmpLog = new TextBox(); // create new control of textbox type
        tmpLog.Text = "some text here";

        TabPage tb = new TabPage("my brand new tab"); //create tab
        tabControl.TabPages.Add(tb); // add tab to existed TabControl
        tb.Controls.Add(tmpLog); // add textBox to new tab

        tabControl.SelectedTab = tb;     // activate tab

#2


Derive from TabPage and add the controls you want in that derived class. Then use your derived class instead of TabPage.

从TabPage派生并在该派生类中添加所需的控件。然后使用派生类而不是TabPage。

#3


Controls can only be parented to one control, but it looks like you are trying to parent your tableLayoutPanel41 in every TabPage instance. You need to create new copies of the controls for each instance of the tab. There are various ways to fix this.

控件只能作为一个控件的父级,但看起来你试图在每个TabPage实例中为tableLayoutPanel41提供父级。您需要为选项卡的每个实例创建控件的新副本。有多种方法可以解决这个问题。

  1. Programmatically create your tab page and its contents multiple times.
  2. 以编程方式多次创建标签页及其内容。

  3. Have the contents of your TabPage implemented as a user control that you dock fill on a tab page. Then recreate one of those for each page duplicate your require.
  4. 将TabPage的内容实现为用户控件,并将其填充到选项卡页面上。然后为每个页面重新创建其中一个复制您的要求。

  5. Create a class derived from TabPage that implements your tab page and create new instances of that for each use.
  6. 创建一个派生自TabPage的类,该类实现您的标签页并为每次使用创建新的实例。