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

C#在组合框中手动添加项目,c#,combobox,C#,Combobox,我在操作组合框时遇到问题。有三个组合框。如果我在第一个组合框中更改所选索引,则第二个和第三个组合框中的值应更新。一 发生IndexOutfrance异常。我知道,在开始时,我有3个数据项。。。当我改变第一个的索引时,第二个必须有8到9个值。这里出现了一个例外 第二个组合框有3个值。 现在,如果第一个发生了变化,则出现异常 private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) { if (c

我在操作组合框时遇到问题。有三个组合框。如果我在第一个组合框中更改所选索引,则第二个和第三个组合框中的值应更新。一 发生IndexOutfrance异常。我知道,在开始时,我有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";            
        }
}

我想你指的是ArgumentOutofRange例外。这可能是因为您直接分配给不存在的索引位置。您需要使用Add()或AddRange()来添加项目

comboBox1.Items.Add("Metric ton");
//...
comboBox1.Items.Add("Ounce");
Winforms组合框。项的类型为ObjectCollection。使用索引[0]表示法对已经存在的值有效,但对添加无效

将ComboBox.SelectedIndex设置为超出范围的值也会导致相同的异常。记住集合索引是基于零的,这意味着如果有3个项,它们使用索引[0..2]

您还可以使用items.clear()清除项目,或使用items.remove(“某物”)或items.RemoveAt(i)删除特定项目

或者

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

如果可以,通常最好避免通过集合中的索引访问和更改对象,例如,如果可以使用foreach,则在索引中使用foreach而不是for


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

请在发布之前花一些时间格式化您的问题(并明确您需要什么)。我们会觉得这值得我们花时间来回答。访问我共享的链接,实际上我想在第二个和第三个组合框中添加新项目,方法是从第1个框架中选择您使用的值?C#只是一种语言。\n我指的是哪个GUI框架。ComboBox不是标准的C#,它是GUI组件类型。另外,您的链接不显示任何代码。请直接在问题中添加代码,而不是作为可能消失的外部链接。combo1Box.Items.add(“我的项目”);添加了项,但在我希望删除值的位置添加了值,然后添加了newUse Items.RemoveAt(index)或Items.remove(“我的项”)查看我发布的链接,该链接提供了Items属性的所有方法<代码>MyObject MyObject=新建MyObject()-->
cb.Items.Add(myObject)
如果(cb.SelectedItem!=null)
-->
MyObject MyObject=(MyObject)cb.SelectedItem具有
公共字符串文本{get;set;}
公共int-SomeID{get;set;}
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);
        }
    }
}