-2

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 :/

MiRaN
  • 641
  • 1
  • 6
  • 11
  • Is creating the controls once and setting them visible or hidden based on the selected index an option? – cdsln Aug 04 '15 at 10:49
  • Your creation code is not creating them with a name. The code name doesnt actually get populated into the "name" of the control, so it wont be matching them because all the names are blank – BugFinder Aug 04 '15 at 10:51
  • @cdsln well...that's a good idea, thank you – MiRaN Aug 04 '15 at 10:52
  • @BugFinder oh thank you so much, I don't know how I forgot that, it's fixed now – MiRaN Aug 04 '15 at 10:56

1 Answers1

1
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.

BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • yes worked perfectly, but now the problem is when I want to use `comboBox13.SelectedValue` it is saying `The name 'comboBox13' does not exist in the current context` so I can't get values from the comboBoxes that are created dynamically.. ? – MiRaN Aug 04 '15 at 15:16
  • Of course you can, but, comboBox is a temporary name, like women give their bumps names if they dont want to be told genders of their growing baby. Did you name your combobox.. Will there only be 1 of these boxes at a time, or,could there be more than one lot? – BugFinder Aug 04 '15 at 16:33