C#手动添加组合框中的项目

时间:2014-07-31 13:32:55

标签: c# combobox

我在操作组合框时遇到问题。有三个组合框。如果我更改第一个组合框上的选定索引,则应更新第二个和第三个中的值。一个 发生IndexOutOfRange异常。我知道,在开始时,我有3个数据项......当我更改第1个索引时,第2个必须有8到9个值。这里发生异常 第二个组合框有3个值。 现在,如果第一个被改变,那么这里发生异常

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";            
        }
}

3 个答案:

答案 0 :(得分:1)

如果可以,通常更好的做法是避免通过集合中的索引访问和更改对象,例如当你可以使用foreach时,使用它而不是for索引。

例如,在这种情况下,您可以从数组创建List(在对象的代码中定义),并将.Items集合设置为此。这避免了使用任何数字。您还可以存储对comboBox1项的引用,并使用.SelectedItem而不是.SelectedIndex,例如,如果有可能将更多项添加到该组合框中。

答案 1 :(得分:0)

我假设您的意思是ArgumentOutOfRangeException。这可能是因为您直接分配给不存在的索引位置。您需要使用Add()或AddRange()来添加项目。

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

Winforms ComboBox.Items的类型为ObjectCollection。使用index [0]表示法对于已存在的值有效,但不适用于添加。

将ComboBox.SelectedIndex设置为超出范围的值也可能导致同样的异常。请记住,集合索引是从零开始的,这意味着如果您有3个项目,则它们使用索引[0..2]

您也可以使用Items.Clear()清除项目,或使用Items.Remove(“something”)或Items.RemoveAt(i)

删除特定项目。

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

答案 2 :(得分:0)

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
    }
}

可选地

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);
        }
    }
}    
相关问题