views:

104

answers:

2

I have two combo boxes. I insert one value in first combo box and now i want that my second combo box updates its value according to first one. How should i do that?

+1  A: 

Subscribe to first combobox's value changed event and populate the second:

combobox1.SelectedIndexChanged+= new EventHandler(ListBox1_SelectedIndexChanged);

private combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // do stuff with combobox2
}

or

combobox1.SelectedValueChanged += new EventHandler(ListBox1_SelectedValueChanged);

private combobox1_SelectedValueChanged(object sender, EventArgs e)
{
    // do stuff with combobox2
}

Population:

combobox2.Items.Add(new object());
combobox2.Items.Add(new ListItem("caption", "value"));
// etc

Find an existing item:

var index = combobox2.FindStringExact(combobox1.SelectedText);
if (index != -1)
    comobox2.SelectedItem = combobox2.Items[index];
abatishchev
what is syntax to populate the second?
ghd
@sas: See my updated post
abatishchev
I need to do that when first combo box gets a value 'a' for instance then second combo box should automatically show the last value in the drop down of combo box. I am not able to get the syntax to update the value of second combo box
ghd
There are already items in my second combo box, i just want the last item should be shown in it when my firs one gets a certain value. i do not need to add anything in second combo box.
ghd
@sas: See my updated post
abatishchev
abatishchev: thanks!
ghd
@ghd: You're welcome! Don't forget to accept one of the answers)
abatishchev
+3  A: 

Handle the SelectedIndexChanged event for the first ComboBox, then update the second combo box based on the SelectedItem value for the first ComboBox.

A quick example (sans error handling when retrieving the SelectedItem):

public partial class Form1 : Form
{
    private string[] comboBox1Range = new[] { "A", "B", "C", "D" };
    private string[] comboBox2RangeA = new[] { "A1", "A2", "A3", "A4" };
    private string[] comboBox2RangeB = new[] { "B1", "B2", "B3", "B4" };
    private string[] comboBox2RangeC = new[] { "C1", "C2", "C3", "C4" };
    private string[] comboBox2RangeD = new[] { "D1", "D2", "D3", "D4" };

    public Form1()
    {
        InitializeComponent();
        comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
        comboBox1.Items.AddRange(comboBox1Range);
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedValue = comboBox1.SelectedItem as string;

        switch (selectedValue)
        {
            case "A":
                comboBox2.Items.Clear();
                comboBox2.Items.AddRange(comboBox2RangeA);
                break;
            case "B":
                comboBox2.Items.Clear();
                comboBox2.Items.AddRange(comboBox2RangeB);
                break;
            case "C":
                comboBox2.Items.Clear();
                comboBox2.Items.AddRange(comboBox2RangeC);
                break;
            case "D":
                comboBox2.Items.Clear();
                comboBox2.Items.AddRange(comboBox2RangeD);
                break;
        }
    }
}
fletcher