다음을 통해 공유


방법: Windows Forms ComboBox, ListBox 또는 CheckedListBox 컨트롤에서 항목 추가 및 제거

Windows Forms 콤보 상자, 목록 상자 또는 확인 목록 상자는 다양한 데이터 소스에 바인딩할 수 있기 때문에 이러한 컨트롤에 항목을 추가할 수 있는 방법은 여러 가지가 있습니다. 그러나 이 항목에서는 가장 간단한 방법을 설명하므로 데이터 바인딩이 필요 없습니다. 표시되는 항목은 일반적으로 문자열이지만 모든 개체를 사용할 수 있습니다. 컨트롤에 표시되는 텍스트는 개체의 ToString 메서드가 반환하는 값입니다.

항목을 추가하려면

  1. ObjectCollection 클래스의 Add 메서드를 사용하여 목록에 문자열이나 개체를 추가합니다. 컬렉션은 Items 속성을 사용하여 참조합니다.

    ComboBox1.Items.Add("Tokyo")
    
    comboBox1.Items.Add("Tokyo");
    
    comboBox1.get_Items().Add("Tokyo");
    
    comboBox1->Items->Add("Tokyo");
    

    -또는-

  2. Insert 메서드를 사용하여 목록에서 원하는 지점에 문자열이나 개체를 삽입합니다.

    CheckedListBox1.Items.Insert(0, "Copenhagen")
    
    checkedListBox1.Items.Insert(0, "Copenhagen");
    
    checkedListBox1.get_Items().Insert(0, "Copenhagen");
    
    checkedListBox1->Items->Insert(0, "Copenhagen");
    

    -또는-

  3. 전체 배열을 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);
    
    System.Object[] ItemObject = new System.Object[10];
    for(int i=0;i <= 9;i ++)
    {
       ItemObject .set_Item( i , "Item" + i );
    }
    listBox1.get_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.get_Items().RemoveAt(0);
    // To remove currently selected item:
    comboBox1.get_Items().Remove(comboBox1.get_SelectedItem());
    // To remove "Tokyo" item:
    comboBox1.get_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.get_Items().Clear();
    
    listBox1->Items->Clear();
    

참고 항목

작업

방법: Windows Forms ComboBox, ListBox 또는 CheckedListBox 컨트롤의 내용 정렬

참조

ComboBox

ListBox

CheckedListBox

개념

ListBox 대신 Windows Forms ComboBox를 사용해야 하는 경우

기타 리소스

옵션 목록 표시에 사용하는 Windows Forms 컨트롤