다음을 통해 공유


ListBox 클래스

항목의 목록을 표시하는 Windows 컨트롤을 나타냅니다.

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

구문

‘선언
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class ListBox
    Inherits ListControl
‘사용 방법
Dim instance As ListBox
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class ListBox : ListControl
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
public ref class ListBox : public ListControl
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class ListBox extends ListControl
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class ListBox extends ListControl

설명

ListBox 컨트롤을 사용하면 사용자가 클릭하여 선택할 수 있는 항목 목록을 표시할 수 있습니다. ListBox 컨트롤에서는 SelectionMode 속성을 사용하여 단일 또는 다중 선택 옵션을 제공할 수 있습니다. ListBox에는 항목을 세로가 아닌 열 형식으로 표시할 수 있는 MultiColumn 속성도 있습니다. 따라서 컨트롤에서 더 많은 항목을 표시할 수 있으며 항목을 보기 위해 스크롤할 필요가 없습니다.

일반적으로 Windows에서는 ListBox에 표시할 항목의 그리기 작업을 처리합니다. DrawMode 속성을 사용하고 MeasureItemDrawItem 이벤트를 처리하여 Windows에서 제공하는 자동 그리기를 재정의하고 해당 항목을 직접 그릴 수 있습니다. 소유자가 그린 ListBox 컨트롤을 사용하여 목록에서 가변 길이 항목, 이미지, 각 항목 텍스트의 색 또는 글꼴 등을 표시할 수 있습니다. HorizontalExtent 속성, GetItemHeightGetItemRectangle을 사용해도 항목을 직접 그릴 수 있습니다.

ListBox에서는 표시 및 선택 기능뿐 아니라 ListBox에 항목을 효율적으로 추가하거나 목록의 항목 내에서 텍스트를 검색하는 기능도 제공합니다. BeginUpdateEndUpdate 메서드를 사용하면 항목을 목록에 추가할 때마다 컨트롤을 다시 그리지 않고도 ListBox에 많은 수의 항목을 추가할 수 있습니다. FindStringFindStringExact 메서드를 사용하면 특정 검색 문자열을 포함하는 항목을 목록에서 검색할 수 있습니다.

Items, SelectedItemsSelectedIndices 속성을 사용하면 ListBox에 사용되는 세 가지 컬렉션에 액세스할 수 있습니다. 다음 표에서는 ListBox에 사용되는 세 가지 컬렉션과 이 컬렉션들이 컨트롤 내에서 어떻게 사용되는지를 대략적으로 보여 줍니다.

컬렉션 클래스

ListBox 내에서 사용합니다.

ListBox.ObjectCollection

ListBox 컨트롤에 들어 있는 모든 항목을 포함합니다.

ListBox.SelectedObjectCollection

ListBox 컨트롤에 들어 있는 항목의 하위 집합인 선택된 항목의 컬렉션을 포함합니다.

ListBox.SelectedIndexCollection

ListBox.ObjectCollection 인덱스의 하위 집합인 선택된 인덱스의 컬렉션을 포함합니다. 이 인덱스는 선택된 항목을 지정합니다.

다음 세 가지 예제에서는 ListBox 클래스에서 지원하는 인덱싱된 세 가지 컬렉션을 보여 줍니다.

다음 표에서는 ListBox.ObjectCollection에서 ListBox 항목을 저장하는 방법의 예제와 ListBox 예제에서의 선택 상태를 보여 줍니다.

인덱스

항목

ListBox 내의 선택 상태

0

object1

선택하지 않음

1

object2

선택함

2

object3

선택하지 않음

3

object4

선택함

4

object5

선택함

다음 표에서는 위의 표에 표시된 ListBox.ObjectCollection에 따라 ListBox.SelectedObjectCollection이 표시되는 방식을 보여 줍니다.

인덱스

항목

0

object2

1

object4

2

object5

다음 표에서는 위의 표에 표시된 ListBox.ObjectCollection에 따라 ListBox.SelectedIndexCollection이 표시되는 방식을 보여 줍니다.

인덱스

항목 인덱스

0

1

1

3

2

4

ListBox.ObjectCollection 클래스의 Add 메서드를 사용하면 ListBox에 항목을 추가할 수 있습니다. Add 메서드는 ListBox에 멤버를 추가할 때 개체를 수용할 수 있습니다. 개체가 ListBox에 추가될 때 개체 내의 멤버 이름이 DisplayMember 속성에 지정되어 있지 않을 경우 해당 컨트롤에서는 개체의 ToString 메서드에 정의된 텍스트를 사용합니다. ListBox.ObjectCollection 클래스의 Add 메서드를 사용하여 항목을 추가할 수 있을 뿐만 아니라 ListControl 클래스의 DataSource 속성을 사용하여 항목을 추가할 수도 있습니다.

참고

기본 Windows Form에 ListBox, ComboBox 또는 CheckedListBox가 있고 파생된 Windows Form에 있는 컨트롤의 문자열 컬렉션을 수정하려면 기본 Windows Form에 있는 해당 컨트롤의 문자열 컬렉션이 비어 있어야 합니다. 문자열 컬렉션이 비어 있지 않으면 다른 Windows Form을 파생시킬 때 해당 문자열 컬렉션이 읽기 전용이 됩니다.

예제

다음 코드 예제에서는 열에 여러 항목을 표시하고 컨트롤 목록에서 둘 이상의 항목을 선택할 수 있는 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());             
}

상속 계층 구조

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
         System.Windows.Forms.ListControl
          System.Windows.Forms.ListBox
             System.Windows.Forms.CheckedListBox

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

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 멤버
System.Windows.Forms 네임스페이스