방법: Windows Forms ComboBox, ListBox 또는 CheckedListBox 컨트롤에서 항목 추가 및 제거
이러한 컨트롤은 다양한 데이터 원본에 바인딩할 수 있으므로 다양한 방법으로 Windows Forms 콤보 상자, 목록 상자 또는 선택 목록 상자에 항목을 추가할 수 있습니다. 그러나 이 항목에서는 가장 간단한 방법을 보여 줍니다. 데이터 바인딩이 필요하지 않습니다. 표시되는 항목은 일반적으로 문자열입니다. 그러나 모든 개체를 사용할 수 있습니다. 컨트롤에 표시되는 텍스트는 개체의 ToString
메서드에서 반환되는 값입니다.
항목을 추가하려면
ObjectCollection
클래스의Add
메서드를 사용하여 문자열 또는 개체를 목록에 추가합니다. 이 컬렉션은Items
속성을 사용하여 참조됩니다.ComboBox1.Items.Add("Tokyo")
comboBox1.Items.Add("Tokyo");
comboBox1->Items->Add("Tokyo");
- 또는
Insert
메서드를 사용하여 목록의 원하는 지점에 문자열 또는 개체를 삽입합니다.CheckedListBox1.Items.Insert(0, "Copenhagen")
checkedListBox1.Items.Insert(0, "Copenhagen");
checkedListBox1->Items->Insert(0, "Copenhagen");
- 또는
Items
컬렉션에 전체 배열을 할당합니다.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);
항목을 제거하려면
Remove
또는RemoveAt
메서드를 호출하여 항목을 삭제합니다.Remove
에는 제거할 항목을 지정하는 인수가 하나 있습니다.RemoveAt
은(는) 지정된 값이 포함된 항목을 제거합니다.' 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");
모든 항목을 제거하려면
Clear
메서드를 호출하여 컬렉션에서 모든 항목을 제거합니다.ListBox1.Items.Clear()
listBox1.Items.Clear();
listBox1->Items->Clear();
참고 항목
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET Desktop feedback