簡體   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