Bagikan melalui


ListBox.ObjectCollection.Add(Object) Metode

Definisi

Menambahkan item ke daftar item untuk 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

Parameter

item
Object

Objek yang mewakili item untuk ditambahkan ke koleksi.

Mengembalikan

Indeks berbasis nol item dalam koleksi, atau -1 jika BeginUpdate() telah dipanggil.

Pengecualian

Tidak tersedia cukup ruang untuk menambahkan item baru ke daftar.

item adalah null.

Contoh

Contoh kode berikut menunjukkan cara membuat ListBox kontrol yang menampilkan beberapa item dalam kolom dan dapat memilih lebih dari satu item dalam daftar kontrol. Kode untuk contoh menambahkan 50 item ke ListBox menggunakan Add metode ListBox.ObjectCollection kelas lalu memilih tiga item dari daftar menggunakan SetSelected metode . Kode kemudian menampilkan nilai dari ListBox.SelectedObjectCollection koleksi (melalui SelectedItems properti) dan ListBox.SelectedIndexCollection (melalui SelectedIndices properti ). Contoh ini mengharuskan kode berada di dan dipanggil dari 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

Keterangan

Sorted Jika properti diatur ListBox ke true, item disisipkan ke dalam daftar menurut abjad. Jika tidak, item disisipkan di akhir daftar. Untuk menyisipkan item ke dalam kotak daftar pada posisi tertentu, gunakan metode .Insert Untuk menambahkan sekumpulan item ke kotak daftar dalam satu operasi, gunakan metode .AddRange Jika Anda ingin menggunakan Add metode untuk menambahkan sejumlah besar item ke daftar, gunakan BeginUpdate metode dan EndUpdate untuk mencegah ListBox pengecatan ulang setiap kali item ditambahkan ke daftar hingga semua item ditambahkan ke daftar. Saat menambahkan item ke ListBox, lebih efisien untuk mengurutkan item terlebih dahulu lalu menambahkan item baru.

Saat objek ditambahkan ke koleksi, yang ListBox pertama memeriksa untuk melihat apakah DisplayMember properti ListControl kelas memiliki nama anggota dari objek yang ditentukan untuk dirujuk saat mendapatkan teks item. DisplayMember Jika properti tidak memiliki anggota yang ditentukan, ListBox maka memanggil ToString metode objek untuk mendapatkan teks untuk ditampilkan dalam daftar.

Berlaku untuk