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

컬렉션에 추가할 항목을 나타내는 개체입니다.

반환

Int32

컬렉션에서 항목의 인덱스(0부터 시작)이며 BeginUpdate()가 호출되면 -1입니다.

예외

공간이 부족하여 목록에 새 항목을 추가할 수 없는 경우

item이(가) null인 경우

예제

다음 코드 예제에서는 열에 여러 항목을 표시 하 고 컨트롤의 목록에서 둘 이상의 항목을 선택할 수 있는 컨트롤을 만드는 ListBox 방법을 보여 줍니다. 예제의 코드는 클래스의 ListBox.ObjectCollection 메서드를 사용하여 Add 50개의 항목을 ListBox 추가한 다음, 메서드를 사용하여 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

설명

속성 ListBoxSorted 설정true되면 항목이 목록에 사전순으로 삽입됩니다. 그렇지 않으면 항목이 목록의 끝에 삽입됩니다. 특정 위치의 목록 상자에 항목을 삽입하려면 메서드를 Insert 사용합니다. 단일 작업에서 목록 상자에 항목 집합을 추가하려면 이 메서드를 AddRange 사용합니다. 메서드를 Add 사용하여 목록에 많은 수의 항목을 추가하려면 모든 항목이 목록에 추가될 때까지 항목이 목록에 추가될 때마다 해당 항목이 다시 표시되지 않도록 ListBox 하기 위해 해당 및 메서드를 사용합니다 BeginUpdate EndUpdate. 항목을 추가할 ListBox때 먼저 항목을 정렬한 다음 새 항목을 추가하는 것이 더 효율적입니다.

컬렉션에 개체가 ListBox 추가되면 먼저 클래스의 ListControl 속성에 항목 텍스트를 가져올 때 참조하도록 지정된 개체의 멤버 이름이 있는지 DisplayMember 확인합니다. 속성에 DisplayMember 지정된 ListBox 멤버가 없는 경우 개체의 메서드를 호출 ToString 하여 목록에 표시할 텍스트를 가져옵니다.

적용 대상