-1

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!

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 1
    Please post your exacly exception, code you store object reference, code read – HungPV Jul 17 '15 at 01:05
  • 1
    You only show the code that creates the control, which is the part that you said already works. You failed to show how you attempt to read the value of the control, which is the part that you said wasn't working. – Steven Doggart Jul 17 '15 at 01:08

2 Answers2

0

You need to write an event handler exactly as you normally would, e.g.

private void ComboBoxes_SelectedIndexChanged(object sender, EventArgs e)
{
    // ...
}

When you create the ComboBox control, you attach that method to the event, e.g.

var cbx = new ComboBox();

cbx.SelectedIndexChanged += ComboBoxes_SelectedIndexChanged;

Inside the event handler, the sender parameter refers to the object that raised the event, i.e. the ComboBox whose SelectedIndex just changed, e.g.

private void ComboBoxes_SelectedIndexChanged(object sender, EventArgs e)
{
    var cbx = (ComboBox) sender;
    var selection = cbx.Text;

    MessageBox.Show(selection, "You chose...");
}
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
0

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());
Nam Bình
  • 412
  • 2
  • 13