C#如何在switch中删除动态创建的控件

时间:2015-08-04 10:46:58

标签: c# switch-statement controls dynamic-controls

好的,所以在案例1 开关(comboBox1.SelectedIndex)中,它会动态创建一些标签和组合框并将它们添加到tabPage1,但我想要那些动态创建的控件在选择案例2 时删除

public void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox2.SelectedIndex)
            {
                case 0:
                    {
                        //do nothing.
                        break;
                    }
                case 1:
                    {
                        Label label16 = new Label();
                        tabPage1.Controls.Add(label16);
                        label16.Left = 465;
                        label16.Top = 111;
                        label16.Text = "Time:";
                        label16.Size = new Size(60, 13);

                        ComboBox comboBox13 = new ComboBox();
                        tabPage1.Controls.Add(comboBox13);
                        comboBox13.Left = 533;
                        comboBox13.Top = 108;
                        comboBox13.Size = new Size(104, 21);
                        comboBox13.DropDownStyle = ComboBoxStyle.DropDownList;

                        comboBox13.DisplayMember = "Text";
                        comboBox13.ValueMember = "Value";
                        var ComboBox13Items = new[] { 
                        new { Text = "1 Second", Value = "1" }, 
                        new { Text = "2.5 Seconds", Value = "2.5" }, 
                        new { Text = "5 Seconds", Value = "5" },
                        new { Text = "7.5 Seconds", Value = "7.5" },
                        new { Text = "10 Seconds", Value = "10" }
                        };
                        comboBox13.DataSource = ComboBox13Items;
                        break;
                    }
                case 2:
                    {
                        foreach (Control TimeLabel in tabPage1.Controls.OfType<Controls>())
                        {
                            if (TimeLabel.Name == "label16")
                                tabPage1.Controls.Remove(TimeLabel);
                        }

                        foreach (Control TimeComboBox in tabPage1.Controls.OfType<Controls>())
                        {
                            if (TimeComboBox.Name == "comboBox13")
                                tabPage1.Controls.Remove(TimeComboBox);
                        }

                        break;
                    }

我也尝试将OfType<Controls>更改为OfType<Label>OfType<ComboBox>,但仍然没有运气:/

1 个答案:

答案 0 :(得分:1)

SELECT
    id, first, last, email, notes
FROM
    My_Table
WHERE
    CASE ''''+@column_name_variable+''''
        WHEN ''''+column_1+''''=1 THEN column_1
        WHEN ''''+column_2+''''=2 THEN column_2
        ...
        ELSE 'not null'
    END IS NOT NULL

不创建名为&#34; label16&#34;的按钮。它会创建一个未命名的按钮。

你需要添加

Label label16 = new Label();
                        tabPage1.Controls.Add(label16);
                        label16.Left = 465;
                        label16.Top = 111;
                        label16.Text = "Time:";
                        label16.Size = new Size(60, 13);

名称应该是唯一的,如果有可能添加多个集合,请保留计数器或其他内容,并使用计数器创建唯一名称。