简体   繁体   中英

C# add items in combobox manually

I have a problem in manipulating comboBoxes. There are three comboboxes. If I change the selected index on the 1st combobox then values in the 2nd and 3rd should be updated. An IndexOutOfRange exception occurs. I know, in start, I have 3 data items... when I change the index of the 1st then the 2nd must have 8 to 9 values. Here occur exception 2nd combobox have 3 values. now if 1st is changed then here occur exception

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{ 
        if (comboBox3.SelectedIndex == 1)
        {                         
            comboBox1.Items[0]="Kilometer";
            comboBox1.Items[1]="Meter";
            comboBox1.Items[2]="Centimeter";
            comboBox1.Items[3]="Millimeter";
            comboBox1.Items[4]="Mile";
            comboBox1.Items[5]="Yard";
            comboBox1.Items[6]="Foot";
            comboBox1.Items[7]="Inch";
            comboBox1.Items[8] = "Nautical Mile";            

            comboBox2.Items[0] = "Meter";
            comboBox2.Items[1] = "Centimeter";
            comboBox2.Items[2] = "Millimeter";
            comboBox2.Items[3] = "Mile";
            comboBox2.Items[4] = "Yard";
            comboBox2.Items[5] = "Foot";
            comboBox2.Items[6] = "Inch";
            comboBox2.Items[7] = "Nautical Mile";
            comboBox2.Items[8] = "Kilometer";
        }
        else if (comboBox3.SelectedIndex == 2) 
        {
            comboBox1.Items[0] = "Metric ton";
            comboBox1.Items[1] = "Kilogram";
            comboBox1.Items[2] = "Gram";
            comboBox1.Items[3] = "Milligram";
            comboBox1.Items[4] = "Mcg";
            comboBox1.Items[5] = "Long ton";
            comboBox1.Items[6] = "Short ton";
            comboBox1.Items[7] = "Stone";
            comboBox1.Items[8] = "Pound";
            comboBox1.Items[9] = "Ounce";            
        }
}

When you can it's often better practice to avoid accessing and changing objects by their index in a collection, eg when you can use foreach, use that rather than a for with the index.

For instance, in this case you can create a List from an array (defined in the code of the objects), and set the .Items collection to this. This avoids the usage of any numbers. You can also store references to the comboBox1 items and use .SelectedItem rather than .SelectedIndex, for instance if there was ever any chance that more items would be added to that combo box.

I assume you mean ArgumentOutOfRangeException. It is likely because you are directly assigning to non-existent index locations. You need to use Add() or AddRange() to add the items.

comboBox1.Items.Add("Metric ton");
//...
comboBox1.Items.Add("Ounce");

Winforms ComboBox.Items is of type ObjectCollection. Use of index[0] notation is valid for values that already exist, but not for adding.

The same exception can also be caused by setting ComboBox.SelectedIndex to a value that is out of range. Remember the collection index is zero-based, which means if you have 3 items, they use indexes [0..2]

You can also clear the items with Items.Clear() or remove specific Items with Items.Remove("something") or Items.RemoveAt(i)

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection.add(v=vs.110).aspx

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{ 
    if (comboBox3.SelectedIndex == 1)
    {    
        comboBox1.Items.Clear();       
        comboBox1.Items.Add("Kilometer");
        // and so on
        comboBox2.Items.Clear();
        comboBox2.Items.Add("Meter");
        // and so on
    }
    else if(comboBox3.SelectedIndex == 2)
    {
        comboBox1.Items.Clear();       
        comboBox1.Items.Add("Metric ton");
        // and so on
    }
}

Alternatively

private String[] comboBoxOneItemsArraySetOne = { "SetOneItem1", "SetOneItems2" };
private String[] comboBoxOneItemsArraySetTwo = { "SetTwoItem1", "SetTwoItems2" };
private String[] comboBoxTwoItemsArray = { "Item1", "Items2" };

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{ 
    if (comboBox3.SelectedIndex == 1)
    {    
        comboBox1.Items.Clear();

        foreach(String s in comboBoxOneItemsArraySetOne)
        {
            comboBox1.Items.Add(s);
        }

        comboBox2.Items.Clear();

        foreach(String s in comboBoxTwoItemsArray)
        {
            comboBox2.Items.Add(s);
        }
    }
    else if(comboBox3.SelectedIndex == 2)
    {
        comboBox1.Items.Clear(); 

        foreach(String s in comboBoxOneItemsArraySetTwo)
        {
            comboBox1.Items.Add(s);
        }
    }
}    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM