ListBox.ObjectCollection.Add(Object) Metodo

Definizione

Aggiunge un elemento all'elenco di elementi per un controllo 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

Parametri

item
Object

Oggetto che rappresenta l'elemento da aggiungere all'insieme.

Restituisce

Indice in base zero dell'elemento nell'insieme o -1 se è stato chiamato il metodo BeginUpdate().

Eccezioni

Lo spazio disponibile non è sufficiente per l'aggiunta di un nuovo elemento all'elenco.

item è null.

Esempio

Nell'esempio di codice seguente viene illustrato come creare un ListBox controllo che visualizza più elementi in colonne e può avere più di un elemento selezionato nell'elenco del controllo. Il codice per l'esempio aggiunge 50 elementi all'oggetto ListBox utilizzando il Add metodo della ListBox.ObjectCollection classe e quindi seleziona tre elementi dall'elenco usando il SetSelected metodo . Il codice visualizza quindi i valori della ListBox.SelectedObjectCollection raccolta (tramite la SelectedItems proprietà) e ( ListBox.SelectedIndexCollection tramite la SelectedIndices proprietà ). In questo esempio è necessario che il codice si trovi in e venga chiamato da un oggetto 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

Commenti

Se la Sorted proprietà di ListBox è impostata su true, l'elemento viene inserito nell'elenco in ordine alfabetico. In caso contrario, l'elemento viene inserito alla fine dell'elenco. Per inserire un elemento nella casella di riepilogo in una posizione specifica, utilizzare il Insert metodo . Per aggiungere un set di elementi alla casella di riepilogo in una singola operazione, utilizzare il AddRange metodo . Se si desidera utilizzare il Add metodo per aggiungere un numero elevato di elementi all'elenco, utilizzare i BeginUpdate metodi e EndUpdate per impedire l'aggiornamento ListBox di ogni volta che un elemento viene aggiunto all'elenco finché non vengono aggiunti tutti gli elementi all'elenco. Quando si aggiungono elementi a , ListBoxè più efficiente ordinare prima gli elementi e quindi aggiungere nuovi elementi.

Quando un oggetto viene aggiunto all'insieme, il ListBox primo verifica se la DisplayMember proprietà della ListControl classe ha il nome di un membro dell'oggetto specificato per fare riferimento quando si ottiene il testo dell'elemento. Se la DisplayMember proprietà non dispone di un membro specificato, chiama ListBox il ToString metodo dell'oggetto per ottenere il testo da visualizzare nell'elenco.

Si applica a