ListBox.ObjectCollection.Add(Object) Metoda

Definicja

Dodaje element do listy elementów dla elementu 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

Parametry

item
Object

Obiekt reprezentujący element, który ma zostać dodany do kolekcji.

Zwraca

Int32

Zerowy indeks elementu w kolekcji lub -1, jeśli BeginUpdate() został wywołany.

Wyjątki

Za mało miejsca, aby dodać nowy element do listy.

item to null.

Przykłady

Poniższy przykład kodu pokazuje, jak utworzyć kontrolkę ListBox , która wyświetla wiele elementów w kolumnach i może mieć więcej niż jeden element wybrany na liście kontrolki. Kod przykładu dodaje 50 elementów do ListBox metody using Add klasy ListBox.ObjectCollection , a następnie wybiera trzy elementy z listy przy użyciu SetSelected metody . Następnie kod wyświetla wartości z ListBox.SelectedObjectCollection kolekcji (za pośrednictwem SelectedItems właściwości) i ListBox.SelectedIndexCollection (za pośrednictwem SelectedIndices właściwości). Ten przykład wymaga, aby kod znajdował się w obiekcie i wywoływany z elementu 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

Uwagi

Sorted Jeśli właściwość ListBox jest ustawiona na true, element zostanie wstawiony do listy alfabetycznie. W przeciwnym razie element zostanie wstawiony na końcu listy. Aby wstawić element do pola listy w określonej pozycji, użyj Insert metody . Aby dodać zestaw elementów do pola listy w jednej operacji, użyj AddRange metody . Jeśli chcesz użyć Add metody , aby dodać dużą liczbę elementów do listy, użyj BeginUpdate metod i EndUpdate , aby zapobiec ListBox ponownemu malowaniu za każdym razem, gdy element zostanie dodany do listy, dopóki wszystkie elementy nie zostaną dodane do listy. Podczas dodawania elementów do elementu ListBoxjest wydajniejszy, aby najpierw sortować elementy, a następnie dodawać nowe elementy.

Po dodaniu obiektu do kolekcji pierwszy sprawdza, ListBox czy DisplayMember właściwość ListControl klasy ma nazwę elementu członkowskiego z obiektu określonego do odwołania podczas uzyskiwania tekstu elementu. DisplayMember Jeśli właściwość nie ma określonego elementu członkowskiego, ListBox metoda obiektu wywołuje ToString metodę obiektu w celu uzyskania tekstu do wyświetlenia na liście.

Dotyczy