Search code examples
c#switch-statementcontrolsdynamic-controls

C# How to remove dynamically created controls in switch


ok so in my switch(comboBox1.SelectedIndex) in case 1 it creates some Labels and comboBoxes dynamically and adds them to tabPage1, but I want those dynamically created controls to be removed when case 2 is selected

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;
                    }

I also tried changing OfType<Controls> to OfType<Label> and OfType<ComboBox>, still no luck :/


Solution

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

    Does not create a button called "label16" it creates an unnamed button.

    you would need to add

    labal16.Name = "label16";
    

    Names should be unique, keep a counter or something if theres chance that more than one set would be added and use the counter to make a unique name.