次の方法で共有


方法 : Windows フォームの ComboBox、ListBox、または CheckedListBox コントロールに項目を追加または削除する

Windows フォームのコンボ ボックス、リスト ボックス、およびチェック ボックス付きリスト ボックスは、さまざまなデータ ソースにバインドできます。このため、これらのコントロールに項目を追加する方法はいくつかあります。 ここでは、データ バインディングの必要がない、最も簡単な方法を示します。 通常、表示される項目は文字列ですが、任意のオブジェクトを使用することもできます。 コントロールに表示されているテキストは、オブジェクトの 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 には、削除する項目を指定する引数が 1 つあります。 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 フォーム ComboBox、ListBox、または CheckedListBox コントロールを並べ替える

参照

ComboBox

ListBox

CheckedListBox

概念

ListBox の代わりに Windows フォーム ComboBox を使用する場合

その他の技術情報

オプションのリストを表示するための Windows フォーム コントロール