2
votes

I have 2 comboboxes in a form.

I'd like the selected value in combobox1 to change when the list in combobox2 gets updated.

For Example: ComboBox1 has names of mobile companies and ComboBox2 containing the list of all mobile phones of that company.

3

3 Answers

3
votes

Assume you have a dictionary that associates phone models to their manufacturers:

Dictionary<string, string[]> brandsAndModels = new Dictionary<string, string[]>();

public void Form_Load(object sender, EventArgs e)
{
    brandsAndModels["Samsung"] = new string[] { "Galaxy S", "Galaxy SII", "Galaxy SIII" };
    brandsAndModels["HTC"] = new string[] { "Hero", "Desire HD" };
}

You can get the items to be displayed in the left combo box as:

foreach (string brand in brandsAndModels.Keys)
    comboBox1.Items.Add(brand);

You do this only once, for example in the form's Load event. Note: The brandsAndModels dictionary must be an instance variable, not a local variable as we need to access it later on.

Then, you'd have to assign an event handler for the SelectedIndexChanged event, in which you replace the items in the second combo box with the items in the array for the selected brand:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    comboBox2.Items.Clear();

    if (comboBox1.SelectedIndex > -1)
    {
        string brand = brandsAndModels.Keys.ElementAt(comboBox1.SelectedIndex);
        comboBox2.Items.AddRange(brandsAndModels[brand]);
    }
}

If all of this came from a database, things would be much nicer using data bindings as described in the answers to the question I've linked in my comment to your question.

2
votes

You must handle the SelectedIndexChanged event of combobox to achieve that

2
votes

As you look new I will explain you step-by-step.

  1. Right-Click on ComboBox1 and select properties.
  2. This will open the properties panel.
  3. From the top of properties panel select event button.
  4. List of all events related to combobox will be displayed.
  5. From this list double-click on SelectedIndexChanged.
  6. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {} will be created

You can use the following code after this.

Dictionary<string, string[]> models = new Dictionary<string, string[]>();
public Form1()
{
    InitializeComponent();

    //initializing combobox1
    comboBox1.Items.Add("Select Company");
    comboBox1.Items.Add("HTC");
    comboBox1.Items.Add("Nokia");
    comboBox1.Items.Add("Sony");

    //select the selected index of combobox1
    comboBox1.SelectedIndex = 0;

    //initializing model list for each brand
    models["Sony"] = new string[] { "Xperia S", "Xperia U", "Xperia P" };
    models["HTC"] = new string[] { "WildFire", "Desire HD" };
    models["Nokia"] = new string[] { "N97", "N97 Mini" };
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    comboBox2.Items.Clear();

    if (comboBox1.SelectedIndex > -1)
    {
        string brand = comboBox1.SelectedItem.ToString();
        if(brand != "" && comboBox1.SelectedIndex > 0)
            foreach (string model in models[brand])
                comboBox2.Items.Add(model);
    }
}