다음을 통해 공유


ListBox.ObjectCollection.Add 메서드

ListBox의 항목 목록에 항목을 추가합니다.

네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)

구문

‘선언
Public Function Add ( _
    item As Object _
) As Integer
‘사용 방법
Dim instance As ObjectCollection
Dim item As Object
Dim returnValue As Integer

returnValue = instance.Add(item)
public int Add (
    Object item
)
public:
int Add (
    Object^ item
)
public int Add (
    Object item
)
public function Add (
    item : Object
) : int

매개 변수

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

반환 값

컬렉션 항목의 0부터 시작하는 인덱스입니다.

예외

예외 형식 조건

SystemException

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

설명

ListBoxSorted 속성이 true로 설정되어 있으면 항목이 사전순으로 목록에 삽입됩니다. 그렇지 않으면, 항목은 목록의 끝에 삽입됩니다. 항목을 목록 상자의 특정 위치에 삽입하려면, Insert 메서드를 사용하십시오. 단일 작업에서 항목 세트를 목록 상자에 추가하려면, AddRange 메서드를 사용하십시오. Add 메서드를 사용하여 다수의 항목을 목록에 추가하려면, 모든 항목을 목록에 추가할 때까지 하나의 항목을 목록에 추가할 때마다 ListBox가 다시 칠하지 않도록 BeginUpdateEndUpdate 메서드를 사용하십시오. ListBox에 항목을 추가할 때는 우선 기존 항목을 정렬한 다음 새 항목을 추가하는 것이 더욱 효과적입니다.

컬렉션에 개체를 추가할 경우, ListBox는 우선 ListControl 클래스의 DisplayMember 속성이 항목 텍스트를 가져올 때 참조에 지정된 개체에 구성원의 이름이 포함되는지 여부를 확인합니다. DisplayMember 속성이 구성원을 지정하지 않을 경우, ListBox는 개체의 ToString 메서드를 호출하여 텍스트를 목록에 표시합니다.

예제

다음 코드 예제에서는 열에 여러 항목을 표시하고 컨트롤 목록에서 둘 이상의 항목을 선택할 수 있는 ListBox 컨트롤을 만드는 방법을 보여 줍니다. 이 예제의 코드에서는 ListBox.ObjectCollection 클래스의 Add 메서드를 사용하여 ListBox에 50개의 항목을 추가한 다음 SetSelected 메서드를 사용하여 목록에서 세 개의 항목을 선택합니다. 그런 다음 SelectedItems 속성을 통해 ListBox.SelectedObjectCollection 컬렉션의 값을 표시하고 SelectedIndices 속성을 통해 ListBox.SelectedIndexCollection의 값을 표시합니다. 이 예제에서는 해당 코드가 Form에 있으며 여기에서 호출되어야 합니다.

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
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());             
}
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 );
   
   // 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 ] );
}
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.set_Size(new System.Drawing.Size(200,100));
    listBox1.set_Location(new System.Drawing.Point(10,10));

    // Add the ListBox to the form.
    this.get_Controls().Add(listBox1);

    // Set the ListBox to display items in multiple columns.
    listBox1.set_MultiColumn(true);

    // Set the selection mode to multiple and extended.
    listBox1.set_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.get_Items().Add(("Item" + (new Integer(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.get_SelectedItems().get_Item(1).ToString());

    // Display the index of the first selected item in the ListBox.
    System.Diagnostics.Debug.WriteLine((new Integer
        (listBox1.get_SelectedIndices().get_Item(0))).ToString());
} //button1_Click
private function button1_Click(sender : Object, e : System.EventArgs)
{
   // Create an instance of the ListBox.
   var listBox1 : ListBox = 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 (var x : int = 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());             
}

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

ListBox.ObjectCollection 클래스
ListBox.ObjectCollection 멤버
System.Windows.Forms 네임스페이스