ListBox.ObjectCollection.Add(Object) メソッド

定義

ListBox の項目のリストに項目を追加します。

public:
 int Add(System::Object ^ item);
public int Add (object item);
member this.Add : obj -> int
Public Function Add (item As Object) As Integer

パラメーター

item
Object

コレクションに追加する項目を表すオブジェクト。

戻り値

コレクション内の項目の 0 から始まるインデックス。BeginUpdate() が呼び出された場合は -1。

例外

新しい項目をリストに追加するための空き領域が不足しています。

itemnullです。

次のコード例では、複数の項目を列に表示し、コントロールの一覧で複数の項目を選択できるコントロールを作成 ListBox する方法を示します。 この例のコードでは、 クラスの メソッドを使用して Add 50 個の項目を ListBox.ObjectCollectionListBox追加し、 メソッドを使用してリストから 3 つの項目をSetSelected選択します。 次に、(プロパティをListBox.SelectedObjectCollection介して) コレクションの値と、 (プロパティをListBox.SelectedIndexCollectionSelectedItemsじて) の値をSelectedIndices表示します。 この例では、 にコードを配置し、 から Form呼び出す必要があります。

void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   
   // Create an instance of the ListBox.
   ListBox^ listBox1 = gcnew ListBox;
   
   // Set the size and location of the ListBox.
   listBox1->Size = System::Drawing::Size( 200, 100 );
   listBox1->Location = System::Drawing::Point( 10, 10 );
   
   // Add the ListBox to the form.
   this->Controls->Add( listBox1 );
   
   // Set the ListBox to display items in multiple columns.
   listBox1->MultiColumn = true;
   
   // Set the selection mode to multiple and extended.
   listBox1->SelectionMode = SelectionMode::MultiExtended;
   
   // Shutdown the painting of the ListBox as items are added.
   listBox1->BeginUpdate();
   
   // Loop through and add 50 items to the ListBox.
   for ( int x = 1; x <= 50; x++ )
   {
      listBox1->Items->Add( String::Format( "Item {0}", x ) );

   }
   listBox1->EndUpdate();
   
   // Select three items from the ListBox.
   listBox1->SetSelected( 1, true );
   listBox1->SetSelected( 3, true );
   listBox1->SetSelected( 5, true );
   
   #if defined(DEBUG)
   // Display the second selected item in the ListBox to the console.
   System::Diagnostics::Debug::WriteLine( listBox1->SelectedItems[ 1 ] );
   
   // Display the index of the first selected item in the ListBox.
   System::Diagnostics::Debug::WriteLine( listBox1->SelectedIndices[ 0 ] );
   #endif
}
private void button1_Click(object sender, System.EventArgs e)
{
   // Create an instance of the ListBox.
   ListBox listBox1 = new ListBox();
   // Set the size and location of the ListBox.
   listBox1.Size = new System.Drawing.Size(200, 100);
   listBox1.Location = new System.Drawing.Point(10,10);
   // Add the ListBox to the form.
   this.Controls.Add(listBox1);
   // Set the ListBox to display items in multiple columns.
   listBox1.MultiColumn = true;
   // Set the selection mode to multiple and extended.
   listBox1.SelectionMode = SelectionMode.MultiExtended;
 
   // Shutdown the painting of the ListBox as items are added.
   listBox1.BeginUpdate();
   // Loop through and add 50 items to the ListBox.
   for (int x = 1; x <= 50; x++)
   {
      listBox1.Items.Add("Item " + x.ToString());
   }
   // Allow the ListBox to repaint and display the new items.
   listBox1.EndUpdate();
      
   // Select three items from the ListBox.
   listBox1.SetSelected(1, true);
   listBox1.SetSelected(3, true);
   listBox1.SetSelected(5, true);

   // Display the second selected item in the ListBox to the console.
   System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems[1].ToString());
   // Display the index of the first selected item in the ListBox.
   System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices[0].ToString());             
}
Private Sub button1_Click(sender As Object, e As System.EventArgs)
    ' Create an instance of the ListBox.
    Dim listBox1 As New ListBox()
    ' Set the size and location of the ListBox.
    listBox1.Size = New System.Drawing.Size(200, 100)
    listBox1.Location = New System.Drawing.Point(10, 10)
    ' Add the ListBox to the form.
    Me.Controls.Add(listBox1)
    ' Set the ListBox to display items in multiple columns.
    listBox1.MultiColumn = True
    ' Set the selection mode to multiple and extended.
    listBox1.SelectionMode = SelectionMode.MultiExtended
    
    ' Shutdown the painting of the ListBox as items are added.
    listBox1.BeginUpdate()
    ' Loop through and add 50 items to the ListBox.
    Dim x As Integer
    For x = 1 To 50
        listBox1.Items.Add("Item " & x.ToString())
    Next x
    ' Allow the ListBox to repaint and display the new items.
    listBox1.EndUpdate()
    
    ' Select three items from the ListBox.
    listBox1.SetSelected(1, True)
    listBox1.SetSelected(3, True)
    listBox1.SetSelected(5, True)
       
    ' Display the second selected item in the ListBox to the console.
    System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems(1).ToString())
    ' Display the index of the first selected item in the ListBox.
    System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices(0).ToString())
End Sub

注釈

ListBox プロパティが Sortedtrue設定されている場合、項目はアルファベット順にリストに挿入されます。 それ以外の場合、項目はリストの末尾に挿入されます。 リスト ボックス内の特定の位置に項目を挿入するには、 メソッドを Insert 使用します。 1 回の操作でリスト ボックスに項目のセットを追加するには、 メソッドを AddRange 使用します。 メソッドをAdd使用して多数の項目をリストに追加する場合は、 メソッドと EndUpdate メソッドを使用してBeginUpdate、 のすべての項目がリストに追加されるまで、リストに項目が追加されるたびに を再描画しないようにListBoxします。 に項目を ListBox追加する場合は、最初に項目を並べ替えてから新しい項目を追加する方が効率的です。

オブジェクトがコレクションに追加されると、最初にListBox、クラスの ListControl プロパティに、項目のテキストを取得するときに参照するように指定されたオブジェクトのメンバーの名前があるかどうかをDisplayMember確認します。 プロパティに DisplayMember メンバーが指定されていない場合、 ListBox は オブジェクトの メソッドを ToString 呼び出して、リストに表示するテキストを取得します。

適用対象