通过


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 控件,该控件在列中显示多个项,并且可以在控件的列表中选择多个项。 该示例的代码使用类的方法ListBox.ObjectCollection添加 50 个项ListBoxAdd,然后使用该方法从列表中选择SetSelected三个项目。 然后,代码显示集合(通过SelectedItems属性)和ListBox.SelectedIndexCollection(通过属性)SelectedIndices的值ListBox.SelectedObjectCollection。 此示例要求代码位于 中并从中 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该属性ListBoxtrue,则项按字母顺序插入列表中。 否则,该项将插入列表末尾。 若要在特定位置将项插入列表框中,请使用 Insert 该方法。 若要在单个操作中向列表框添加一组项,请使用 AddRange 该方法。 如果要使用 Add 该方法向列表中添加大量项目,请使用 BeginUpdateEndUpdate 方法防止 ListBox 每次向列表中添加项时重新绘制项,直到所有项都添加到列表中。 将项添加到 a ListBox时,首先对项进行排序,然后添加新项会更高效。

将对象添加到集合时,ListBox第一次检查该类的属性ListControl是否DisplayMember具有从指定对象中引用的成员的名称,以在获取项文本时引用。 DisplayMember如果该属性未指定成员,则ListBox调用ToString对象的方法来获取要显示在列表中的文本。

适用于