如何:在 Windows 窗体 ComboBox 控件、ListBox 控件或 CheckedListBox 控件中添加或移除项How to: Add and Remove Items from a Windows Forms ComboBox, ListBox, or CheckedListBox Control

项可以添加到 Windows 窗体组合框中,列表框中,或检查列表框中有许多种情况下,因为这些控件可以绑定到各种数据源。Items can be added to a Windows Forms combo box, list box, or checked list box in a variety of ways, because these controls can be bound to a variety of data sources. 但是,本主题演示了最简单方法,并且不需要数据绑定。However, this topic demonstrates the simplest method and requires no data binding. 显示的项通常是字符串;但是,可以使用任何对象。The items displayed are usually strings; however, any object can be used. 在控件中显示的文本是由该对象返回的值ToString方法。The text that is displayed in the control is the value returned by the object's ToString method.

若要添加项目To add items

  1. 通过使用添加到列表的字符串或对象Add方法的ObjectCollection类。Add the string or object to the list by using the Add method of the ObjectCollection class. 使用引用集合Items属性:The collection is referenced using the Items property:

    ComboBox1.Items.Add("Tokyo")  
    
    comboBox1.Items.Add("Tokyo");  
    
    comboBox1->Items->Add("Tokyo");  
    
    • 或 -or -
  2. 在使用列表中的所需点处插入字符串或对象Insert方法:Insert the string or object at the desired point in the list with the Insert method:

    CheckedListBox1.Items.Insert(0, "Copenhagen")  
    
    checkedListBox1.Items.Insert(0, "Copenhagen");  
    
    checkedListBox1->Items->Insert(0, "Copenhagen");  
    
    • 或 -or -
  3. 分配到整个数组Items集合:Assign an entire array to the Items collection:

    Dim ItemObject(9) As System.Object  
    Dim i As Integer  
       For i = 0 To 9  
       ItemObject(i) = "Item" & i  
    Next i  
    ListBox1.Items.AddRange(ItemObject)  
    
    System.Object[] ItemObject = new System.Object[10];  
    for (int i = 0; i <= 9; i++)  
    {  
       ItemObject[i] = "Item" + i;  
    }  
    listBox1.Items.AddRange(ItemObject);  
    
    Array<System::Object^>^ ItemObject = gcnew Array<System::Object^>(10);  
    for (int i = 0; i <= 9; i++)  
    {  
       ItemObject[i] = String::Concat("Item", i.ToString());  
    }  
    listBox1->Items->AddRange(ItemObject);  
    

若要删除项To remove an item

  1. 调用RemoveRemoveAt方法来删除项。Call the Remove or RemoveAt method to delete items.

    Remove 有一个参数,指定要移除的项。RemoveAtRemove has one argument that specifies the item to remove.RemoveAt 移除具有指定的索引号的项。removes the item with the specified index number.

    ' To remove item with index 0:  
    ComboBox1.Items.RemoveAt(0)  
    ' To remove currently selected item:  
    ComboBox1.Items.Remove(ComboBox1.SelectedItem)  
    ' To remove "Tokyo" item:  
    ComboBox1.Items.Remove("Tokyo")  
    
    // To remove item with index 0:  
    comboBox1.Items.RemoveAt(0);  
    // To remove currently selected item:  
    comboBox1.Items.Remove(comboBox1.SelectedItem);  
    // To remove "Tokyo" item:  
    comboBox1.Items.Remove("Tokyo");  
    
    // To remove item with index 0:  
    comboBox1->Items->RemoveAt(0);  
    // To remove currently selected item:  
    comboBox1->Items->Remove(comboBox1->SelectedItem);  
    // To remove "Tokyo" item:  
    comboBox1->Items->Remove("Tokyo");  
    

若要删除的所有项To remove all items

  1. 调用Clear方法从集合中移除所有项:Call the Clear method to remove all items from the collection:

    ListBox1.Items.Clear()  
    
    listBox1.Items.Clear();  
    
    listBox1->Items->Clear();  
    

请参阅See also