* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
4.5 KiB
title, ms.date, description, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | description | dev_langs | helpviewer_keywords | ms.assetid | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Add and Remove Items from ComboBox, ListBox, or CheckedListBox Control | 03/30/2017 | Learn how to add and remove a Windows Forms ComboBox, ListBox, and CheckedListBox controls simply and with no data binding. |
|
|
7224c8d2-4118-443e-ae1e-d7c17d1e69ee |
How to: Add and Remove Items from a Windows Forms ComboBox, ListBox, or CheckedListBox Control
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. The text that is displayed in the control is the value returned by the object's ToString method.
To add items
-
Add the string or object to the list by using the
Addmethod of theObjectCollectionclass. The collection is referenced using theItemsproperty:ComboBox1.Items.Add("Tokyo")comboBox1.Items.Add("Tokyo");comboBox1->Items->Add("Tokyo");- or -
-
Insert the string or object at the desired point in the list with the
Insertmethod:CheckedListBox1.Items.Insert(0, "Copenhagen")checkedListBox1.Items.Insert(0, "Copenhagen");checkedListBox1->Items->Insert(0, "Copenhagen");- or -
-
Assign an entire array to the
Itemscollection: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
-
Call the
RemoveorRemoveAtmethod to delete items.Removehas one argument that specifies the item to remove.RemoveAtremoves 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
-
Call the
Clearmethod to remove all items from the collection:ListBox1.Items.Clear()listBox1.Items.Clear();listBox1->Items->Clear();
See also
- xref:System.Windows.Forms.ComboBox
- xref:System.Windows.Forms.ListBox
- xref:System.Windows.Forms.CheckedListBox
- How to: Sort the Contents of a Windows Forms ComboBox, ListBox, or CheckedListBox Control
- When to Use a Windows Forms ComboBox Instead of a ListBox
- Windows Forms Controls Used to List Options