共用方式為


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

一個代表要加入收藏的物品的物件。

傳回

集合中項目的零基索引,或 -1(如果 BeginUpdate() 已被呼叫)。

例外狀況

空間不足以將新項目加入清單。

itemnull

範例

以下程式碼範例示範如何建立 ListBox 一個控制項,能在欄位中顯示多個項目,且控制項列表中可選擇多個項目。 範例中的程式碼會用Add類別的方法ListBox.ObjectCollection加入 50 個項目ListBox,然後用該SetSelected方法從列表中選擇三個項目。 程式碼接著顯示集合ListBox.SelectedObjectCollection(透過屬性)和集合ListBox.SelectedIndexCollection(透過SelectedItemsSelectedIndices屬性)的值。 此範例要求程式碼位於 ,並從 中呼叫 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

備註

Sorted 的屬性 ListBox 設為 true,則該項目會依字母順序插入於清單中。 否則,該項目會入清單的末尾。 要將項目插入清單框中特定位置,請使用此 Insert 方法。 要在單一操作中將一組項目加入清單框,請使用該 AddRange 方法。 如果你想用 Add 這個方法把大量項目加入清單,請用 BeginUpdate and EndUpdate 方法防止 ListBox 每次新增項目時重新上色,直到所有項目都加入清單為止。 在將項目加入 ListBox時,先排序項目再新增項目會更有效率。

當物件加入集合時,ListBox首先檢查該類別的屬性ListControl是否DisplayMember包含該物件中某個成員的名稱,以便在取得項目文本時參考。 若 DisplayMember 屬性未指定成員,則 ListBox 呼叫 ToString 物件的方法取得顯示於清單中的文字。

適用於