为什么我的动态创建的TabPage无法显示?

时间:2019-05-23 14:51:17

标签: c# tabcontrol

我正在使用TabControl和TabPages。我有一个内置的TabPage,其中包括GroupBoxes。我做了一个函数,可以将TabPage的控件克隆到一个新的控件中。因此,我制作了新的TabPage,然后将所有第一个TabPage的控件克隆到新的控件中(包括新的GroupBoxes内的GroupBoxes控件),然后将新的TabPage添加到TabControl中并选择它。

Button BT_Delete = new Button {
    BackColor = System.Drawing.SystemColors.ControlLight,
    Location = new System.Drawing.Point(6, 377),
    Name = "BT_Delete",
    Size = new System.Drawing.Size(75, 23),
    TabIndex = 1002,
    Text = "Delete",
    UseVisualStyleBackColor = false
};
Button BT_Save = new Button {
    BackColor = System.Drawing.SystemColors.ControlLight,
    Location = new System.Drawing.Point(87, 377),
    Name = "BT_Save",
    Size = new System.Drawing.Size(75, 23),
    TabIndex = 1001,
    Text = "Save",
    UseVisualStyleBackColor = false
};
Label LBL_Status = new Label {
    AutoSize = true,
    Location = new System.Drawing.Point(168, 382),
    Name = "LBL_Status",
    Size = new System.Drawing.Size(41, 13),
    TabIndex = 1003,
    Text = "Ready."
};
Button BT_Close = new Button {
    BackColor = System.Drawing.SystemColors.ControlLight,
    Location = new System.Drawing.Point(1059, 377),
    Name = "BT_Close",
    Size = new System.Drawing.Size(75, 23),
    TabIndex = 1003,
    Text = "Close",
    UseVisualStyleBackColor = false
};

TabPage newPage = new TabPage("Text");
newPage.Name = "Name"

ClonePage(TCP_New.Controls, newPage.Controls);
newPage.Controls.Add(BT_Delete);
newPage.Controls.Add(BT_Save);
newPage.Controls.Add(LBL_Status);
newPage.Controls.Add(BT_Close);
TC_Main.TabPages.Add(newPage);
TC_Main.SelectTab(newPage);

现在工作正常,在TabControl中添加了新的TabPage,它显示的很好,我也可以操作数据。

现在的问题是,如果我使用完全相同的功能添加另一个,则GroupBox及其内容将不会显示。但是,按钮和标签工作正常。我可以看到他们,并与他们互动。所以我的想法是问题出在我对GroupBox及其内容的克隆中,但是我不知道在哪里或为什么。

这是我的(TabPage的)ControlCollection克隆功能:

private void ClonePage(Control.ControlCollection from, Control.ControlCollection to) {
    foreach (Control c in from) {
        if (c is GroupBox) {
            GroupBox gbx = (c as GroupBox).Clone();
            to.Add(gbx);

            foreach (Control child in (c as GroupBox).Controls) {
                if (child is Label) {
                    Label lbl = (child as Label).Clone();
                    gbx.Controls.Add(lbl);
                } else if (child is TextBox) {
                    TextBox tbx = (child as TextBox).Clone();
                    gbx.Controls.Add(tbx);
                } else if (child is CheckBox) {
                    CheckBox cbx = (child as CheckBox).Clone();
                    gbx.Controls.Add(cbx);
                } else if (child is NumericUpDown) {
                    NumericUpDown nup = (child as NumericUpDown).Clone();
                    gbx.Controls.Add(nup);
                } else if (child is Button) {
                    Button btn = (child as Button).Clone();
                    gbx.Controls.Add(btn);
                } else if (child is ComboBox) {
                    ComboBox cbb = (child as ComboBox).Clone();
                    gbx.Controls.Add(cbb);
                }
            }
        }
    }
}

这是我的Clone()函数:

public static T Clone<T>(this T controlToClone) where T : Control {
    PropertyInfo[] controlProperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

    T instance = Activator.CreateInstance<T>();

    if (controlToClone is ComboBox) {
        foreach (string i in (controlToClone as ComboBox).Items) {
            (instance as ComboBox).Items.Add(i);
        }
    }

    foreach (PropertyInfo propInfo in controlProperties) {
        if (propInfo.CanWrite) {
            if (propInfo.Name != "WindowTarget") {
                propInfo.SetValue(instance, propInfo.GetValue(controlToClone, null), null);
            }
        }
    }

    return instance;
}

感谢您的帮助,我被困在这里。

0 个答案:

没有答案