繁体   English   中英

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

[英]C# add items in combobox manually

我在操作comboBoxes时遇到问题。 有三个组合框。 如果我更改了第一个组合框上的选定索引,则应该更新第二个和第三个组合中的值。 发生IndexOutOfRange异常。 我知道,一开始我有3个数据项...当我更改第一个的索引时,第二个必须具有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";            
        }
}

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

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

我假设您的意思是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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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