在重新创建tabpage时,在窗体页面中的控件以一个增量递增

时间:2015-04-20 19:18:00

标签: c# winforms

在我的Windows窗体中,我创建了一个标签页并为它们添加了控件。但是,当我关闭某个特定的标签页并重新打开它时,其中的控件会增加一个。

我的意思是说,当我关闭它并重新打开时,我只想在tabpage中显示一个panelbox。标签页包含两个面板。

以下是代码:

//onButtonClickEvent
TabPage tab1 = new TabPage();
tab1.Name = "CriminalRecord";
tab1.Text = "Criminal Record".ToString();
tab1.Visible = true;
tab1.BackColor = Color.Brown;

if (tabControl1.TabPages.Count != 0)
{
    for (int i = 0; i <= tabControl1.TabCount; i++)
    {
        if (tabControl1.TabPages.Contains(tab1))
        {
            tabControl1.SelectTab(tab1);
        }
        else
        {
            tabControl1.Controls.Add(tab1);
            tab1.Controls.Add(loadPanelBottom());
            tab1.Controls.Add(loadPanel());
            tab1.Controls.Add(loadPanelTop());
        }
    }
}
else
{
    tabControl1.Controls.Add(tab1);
    tab1.Controls.Add(loadPanelBottom());
    tab1.Controls.Add(loadPanel());
    tab1.Controls.Add(loadPanelTop());
}

Splitter loadPanel()
{
    Splitter splitter1 = new Splitter();
    splitter1.BackColor = SystemColors.ControlDarkDark;
    splitter1.Cursor = Cursors.HSplit;
    splitter1.Dock = DockStyle.Top;
    splitter1.Location = new System.Drawing.Point(0, 0);
    splitter1.Name = "splitter1";
    splitter1.Size = new System.Drawing.Size(100, 5);
    splitter1.TabIndex = 1;

    return splitter1;
}

1 个答案:

答案 0 :(得分:1)

您应该在表单类中声明新的tabpage 示例

public partial class main:form
{
    TabPage tab1;
}

然后

//onButtonClickEvent
TabPage tab1 = new TabPage();
tab1.Name = "CriminalRecord";
tab1.Text = "Criminal Record".ToString();
tab1.Visible = true;
tab1.BackColor = Color.Brown;