Search code examples
c#winformscomboboxdynamic-controls

How do I read the selections from Dynamically created combo boxes? in c#


I am trying to create a forms application in C# that will create several comboboxes, populate them with selectable items and then be able to detect what items were selected. I have the first two parts working: I can create the combo boxes, add the items to them, but when I try to write the code to read what items have been selected, I get errors because the controls don’t exist at build time..

public System.Windows.Forms.ComboBox AddNewSEPComboBox()
{


System.Windows.Forms.ComboBox SEPcbox = new     System.Windows.Forms.ComboBox();
this.Controls.Add(SEPcbox);
SEPcbox.Top = A * 28;
        SEPcbox.Left = 250;
        string[] SEPSTAT = new string[]{"current (12.1.4013.4013), no changes made.", "not installed, no changes made.", "not installed, latest version (12.1.4013.4013) installed.", "outdated, updated to the latest version (12.1.4013.4013).", "outdated, no changes made."};
        SEPcbox.Items.AddRange(SEPSTAT);
        SEPcbox.Name = "SEPcbox" + A;

        A++;

        return SEPcbox;

Thanks in advance!


Solution

  • Manage your added controls by a list:

        List<System.Windows.Forms.ComboBox> lstComboBoxAdded = new List<System.Windows.Forms.ComboBox>();
        public System.Windows.Forms.ComboBox AddNewSEPComboBox()
        {
            System.Windows.Forms.ComboBox SEPcbox = new System.Windows.Forms.ComboBox();
            SEPcbox.Top = A * 28;
            SEPcbox.Left = 250;
            SEPcbox.Location = new Point(20, A * 30);
            string[] SEPSTAT = new string[] { "current (12.1.4013.4013), no changes made.", "not installed, no changes made.", "not installed, latest version (12.1.4013.4013) installed.", "outdated, updated to the latest version (12.1.4013.4013).", "outdated, no changes made." };
            SEPcbox.Items.AddRange(SEPSTAT);
            SEPcbox.Name = "SEPcbox" + A;
            A++;
            this.Controls.Add(SEPcbox);
            lstComboBoxAdded.Add(SEPcbox);
            return SEPcbox;
        }
    

    Then get your value like:

    MessageBox.Show(lstComboBoxAdded.Where(m => m.Name == "SEPcbox" + A).First().Text);
    

    Or manage Index/Value/Text by:

        Dictionary<string, int> dicIndexSelected = new Dictionary<string, int>();
        public System.Windows.Forms.ComboBox AddNewSEPComboBox()
        {
            System.Windows.Forms.ComboBox SEPcbox = new System.Windows.Forms.ComboBox();
            SEPcbox.Top = A * 28;
            SEPcbox.Left = 250;
            SEPcbox.Location = new Point(20, A * 30);
            string[] SEPSTAT = new string[] { "current (12.1.4013.4013), no changes made.", "not installed, no changes made.", "not installed, latest version (12.1.4013.4013) installed.", "outdated, updated to the latest version (12.1.4013.4013).", "outdated, no changes made." };
            SEPcbox.Items.AddRange(SEPSTAT);
            SEPcbox.Name = "SEPcbox" + A;
            A++;
            this.Controls.Add(SEPcbox);
            dicIndexSelected.Add(SEPcbox.Name, -1);
            SEPcbox.SelectedIndexChanged += new EventHandler(SEPcbox_SelectedIndexChanged);
            return SEPcbox;
        }
    
        void SEPcbox_SelectedIndexChanged(object sender, EventArgs e)
        {
            dicIndexSelected[((System.Windows.Forms.ComboBox)sender).Name] = ((System.Windows.Forms.ComboBox)sender).SelectedIndex;
        }
    

    Then get your value like:

    MessageBox.Show(dicIndexSelected["SEPcbox" + A].ToString());