' Visual Basic ComboBox1.Items.Add("Tokyo") // C# comboBox1.Items.Add("Tokyo"); // C++ comboBox1->Items->Add(S"Tokyo");
- or -- Insert the string or object at the desired point in the list with the Insert method:
' Visual Basic CheckedListBox1.Items.Insert(0, "Copenhagen") // C# checkedListBox1.Items.Insert(0, "Copenhagen"); // C++ checkedListBox1->Items->Insert(0, S"Copenhagen");
- or - - Assign an entire array to the Items collection:
' Visual Basic 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) // C# System.Object[] ItemObject = new System.Object[10]; for (int i = 0; i <= 9; i++) { ItemObject[i] = "Item" + i; } listBox1.Items.AddRange(ItemObject); // C++ System::Object* ItemObject[] = new System::Object*[10]; for (int i = 0; i <= 9; i++) { ItemObject[i] = String::Concat(S"Item", i.ToString()); } listBox1->Items->AddRange(ItemObject);
To remove an item
- Call the Remove or RemoveAt method to delete items.Remove has one argument that specifies the item to remove. RemoveAt removes the item with the specified index number.
' Visual Basic ' 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") // C# // 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"); // C++ // 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(S"Tokyo");
To remove all items
- Call the Clear method to remove all items from the collection:
' Visual Basic ListBox1.Items.Clear() // C# listBox1.Items.Clear(); // C++ listBox1->Items->Clear();