다음을 통해 공유


ListView 클래스

Windows list view 컨트롤을 나타냅니다. 이 컨트롤은 네 개의 서로 다른 보기를 사용하여 표시할 수 있는 항목의 컬렉션을 나타냅니다.

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

구문

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

설명

ListView 컨트롤을 사용하면 항목 텍스트를 사용하여 항목 목록을 표시할 수 있으며 선택적으로 항목의 형식을 식별하는 아이콘을 표시할 수 있습니다. 예를 들어, Windows 탐색기의 파일 목록은 ListView 컨트롤의 모양과 비슷합니다. 탐색기는 트리에서 현재 선택한 파일과 폴더 목록을 표시합니다. 각 파일 및 폴더는 해당 항목과 연결된 아이콘을 표시하므로 파일 또는 폴더의 형식을 쉽게 확인할 수 있습니다. ListViewItem 클래스는 ListView 컨트롤 내에 있는 항목을 나타냅니다. 목록에 표시되는 항목은 다섯 가지 보기 중 하나에 표시할 수 있습니다. 큰 아이콘, 작은 아이콘 또는 세로 목록의 작은 아이콘으로 항목을 표시할 수 있습니다. 또한 항목에는 부모 항목과 관련된 정보가 들어 있는 하위 항목이 있을 수 있습니다. 자세히 보기를 사용하면 하위 항목에 표시되는 정보를 식별하는 열 머리글이 있는 표 형식으로 항목과 하위 항목을 표시할 수 있습니다. 아래에서 설명하는 것처럼 가용성이 제한되는 Tile 보기에서는 텍스트 정보 옆에 큰 아이콘이 포함된 바둑판으로 항목과 하위 항목을 표시할 수 있습니다. ListView는 단일 선택이나 여러 선택을 지원합니다. 다중 선택 기능을 사용하면 ListBox 컨트롤에서 사용하는 방법과 유사한 방법으로 항목 목록에서 선택할 수 있습니다. 또한, 선택한 항목을 활성화하여 작업을 수행할 수 있습니다. 예를 들어, ListView 컨트롤을 사용하면 응용 프로그램에서 열어 이용할 수 있도록 파일 목록을 표시할 수 있습니다. 열 파일을 선택하고 두 번 클릭하여 활성화하면 해당 파일을 응용 프로그램에서 열 수 있습니다. 또한 CheckBoxes 속성을 사용하면 ListView에서 작업할 항목을 선택할 수 있는 확인란을 표시할 수 있습니다. ListView 컨트롤을 다양한 방법으로 사용할 수 있습니다. 이 컨트롤을 사용하여 응용 프로그램, 데이터베이스 또는 텍스트 파일 정보를 표시할 수 있습니다. 또한 ListView를 사용하여 처리할 일련의 파일을 선택하는 것과 같은 사용자로부터의 정보를 얻을 수 있습니다.

ListView에는 모양과 동작에 융통성을 제공하는 많은 속성이 있습니다. View 속성을 사용하면 항목이 표시되는 방법을 변경할 수 있습니다. LargeImageList, SmallImageListStateImageList 속성을 사용하면 항목에 대해 표시되는 이미지가 들어 있는 ImageList 개체를 지정할 수 있습니다. StateImageList의 경우 CheckBoxes 속성이 true이면 확인란이 표시됩니다. CheckedItems 속성을 사용하여 ListView.CheckedListViewItemCollection 컬렉션에 액세스하면 어떤 항목이 선택되었는지 확인할 수 있습니다. Columns 속성을 사용하면 ListView.ColumnHeaderCollection에 액세스할 수 있습니다. 이 컬렉션에는 컨트롤의 View 속성이 Details로 설정된 경우 표시되는 열 머리글이 저장되어 있습니다. ListView 항목은 Items 속성을 사용하여 추가하거나 제거합니다. Items 속성을 사용하면 컨트롤에 있는 항목을 조작하는 메서드를 제공하는 컨트롤의 ListView.ListViewItemCollection에 액세스할 수 있습니다. LabelEdit 속성을 사용하면 항목 텍스트를 편집할 수 있도록 할 수 있습니다. 컨트롤에 많은 수의 항목이 들어 있는 경우 항목을 정렬된 목록으로 표시하는 것이 더 편리할 수 있습니다. Sorting 속성을 사용하여 항목을 사전순으로 정렬할 수 있습니다. ListView 컨트롤의 모양을 완전히 사용자 지정할 수도 있습니다. 이렇게 하려면 OwnerDraw 속성을 true로 설정하고 DrawItem, DrawSubItemDrawColumnHeader 이벤트 중 하나 이상을 처리합니다.

ListView 컨트롤의 많은 속성은 컨트롤의 View 속성이 Details로 설정된 경우 사용됩니다. AllowColumnReorder 속성을 사용하면 런타임에 ListView 컨트롤의 열 순서를 다시 구성할 수 있습니다. FullRowSelect 속성을 사용하면 항목만이 아니라 항목과 해당 항목의 하위 항목을 모두 선택할 수 있습니다. GridLines 속성을 사용하면 자세히 보기에 모눈선을 표시하여 ListView에서 항목과 하위 항목의 경계를 구분할 수 있습니다. HeaderStyle 속성을 사용하면 표시할 열 머리글의 형식을 지정할 수 있습니다.

ListView 컨트롤에 사용할 수 있는 많은 속성 이외에도 응용 프로그램에서 ListView에 추가 기능을 제공하는데 사용할 수 있는 메서드와 이벤트가 있습니다. BeginUpdateEndUpdate 메서드를 사용하면 항목을 추가할 때마다 컨트롤을 다시 그리지 않아도 되므로 ListView에 많은 항목을 추가할 때 성능이 향상될 수 있습니다. ListView 컨트롤에 항목과 하위 항목이 표시되면 하위 항목을 마우스 오른쪽 단추로 클릭할 때 기능을 제공해야 할 경우가 있습니다. GetItemAt 메서드를 사용하면 클릭한 하위 항목이 속한 항목을 확인할 수 있습니다. 항목이 편집된 후 항목의 유효성 검사를 수행할 경우 변경할 특정 항목을 표시할 수 있습니다. 특정 항목이 컨트롤의 보이는 영역에 있도록 하려면 EnsureVisible 메서드를 호출합니다.

LabelEdit 속성이 true로 설정되어 있으면 BeforeLabelEditAfterLabelEdit 이벤트에 대한 이벤트 처리기를 만들어 텍스트가 변경되기 전과 변경된 후에 텍스트의 유효성을 검사하는 등의 작업을 수행할 수 있습니다. ListView에 표시된 항목을 편집할 수 있도록 파일을 열거나 대화 상자를 표시하려면 ItemActivate 이벤트에 대한 이벤트 처리기를 만듭니다. ListView에서 열 머리글을 클릭하면 항목이 정렬되게 하려면 ColumnClick에 대한 이벤트 처리기를 만들어 정렬을 수행합니다. CheckBoxes 속성이 true로 설정되어 있는 경우 ItemCheck 이벤트를 사용하여 항목의 확인란 상태가 변경되는 시기를 결정할 수 있습니다.

BackgroundImage 속성을 사용하여 ListView의 배경 이미지도 설정할 수 있습니다. ListView 컨트롤의 배경 이미지를 올바르게 표시하려면 응용 프로그램에 Main 메서드에 대한 STAThreadAttribute가 있어야 합니다. 또한 배경 이미지가 있는 ListView 컨트롤이 Internet Explorer에 호스팅되어 있으면 응용 프로그램 매니페스트 파일의 종속 어셈블리로 comctl32.dll 버전 6.0을 지정하여 배경 이미지가 제대로 표시되도록 합니다.

참고

.NET Framework 버전 1.1과 이전 버전에서는 ListView 컨트롤의 Cursor 속성을 설정해도 커서의 모양에 영향을 주지 않습니다.

Windows XP 및 Windows Server 2003에서는 응용 프로그램에서 Application.EnableVisualStyles 메서드를 호출할 때 ListView 컨트롤을 향상하는 세 가지 기능인 Tile 보기, 그룹화 및 삽입 표시를 제공합니다.

Tile 보기를 사용하면 큰 아이콘 옆에 항목 및 하위 항목 텍스트를 표시하여 그래픽 정보와 텍스트 정보의 균형을 맞출 수 있습니다. 이 기능을 사용하려면 View 속성을 View.Tile로 설정합니다.

그룹화 기능을 사용하면 그룹 항목을 관련 범주로 시각적으로 그룹화할 수 있습니다. 이 기능을 사용하려면 Groups 속성을 사용하여 ListViewGroup 개체를 ListView 컨트롤에 추가합니다. 이 기능을 일시적으로 사용하지 않으려면 ShowGroups 속성을 false로 설정합니다.

삽입 표시 기능을 사용하면 놓는 위치를 나타내는 시각적 피드백과 함께 끌어서 놓기 항목 위치 변경 기능을 제공할 수 있습니다. 삽입 표시를 나타내려면 InsertionMark 속성을 통해 검색된 ListViewInsertionMark 개체를 사용합니다.

이러한 기능은 Windows XP와 Windows Server 2003에서만 사용할 수 있습니다. 이전 버전의 플랫폼에서는 이러한 기능과 관련된 코드가 적용되지 않으며 Tile 보기가 큰 아이콘 보기로 나타나고 삽입 표시와 그룹이 표시되지 않습니다. 이러한 기능을 사용할 수 있는지 확인하는 코드를 포함시키고 사용할 수 없는 경우 대체 기능을 제공할 수도 있습니다. 이러한 기능은 운영 체제 테마 기능을 제공하는 라이브러리에서 제공됩니다. 이 라이브러리를 사용할 수 있는지 확인하려면 FeatureSupport.IsPresent(Object) 메서드 오버로드를 호출하고 OSFeature.Themes 값을 전달합니다.

다음 표에서는 일부 ListView 멤버와 해당 멤버에 유효한 보기를 보여 줍니다.

ListView 멤버

보기

Alignment 속성

SmallIcon 또는 LargeIcon

AutoArrange 속성

SmallIcon 또는 LargeIcon

AutoResizeColumn 메서드

Details

Columns 속성

Details 또는 Tile

DrawSubItem 이벤트

Details

FindItemWithText 메서드

Details, List 또는 Tile

FindNearestItem 메서드

SmallIcon 또는 LargeIcon

GetItemAt 메서드

Details 또는 Tile

Groups 속성

List를 제외한 모든 보기

HeaderStyle 속성

Details

InsertionMark 속성

LargeIcon, SmallIcon 또는 Tile

예제

다음 코드 예제에서는 각 항목에 대해 세 개의 ListViewItem 개체 및 세 개의 ListViewItem.ListViewSubItem 개체가 지정되어 있는 ListView 컨트롤을 만듭니다. 이 예제에서는 자세히 보기에 하위 항목도 표시하는 ColumnHeader 개체도 만듭니다. ImageList 개체도 코드 예제에 만들어져 ListViewItem 개체에 이미지를 제공합니다. 이런 ImageList 개체는 LargeImageListSmallImageList 속성에 추가됩니다. 이 예제에서는 다음과 같은 속성을 사용하여 ListView 컨트롤을 만듭니다.

  • View

  • LabelEdit

  • AllowColumnReorder

  • CheckBoxes

  • FullRowSelect

  • GridLines

  • Sorting

이 예제를 실행하려면 코드를 Form에 추가하고 폼의 생성자나 다른 메서드로부터 예제에서 만든 메서드를 호출한 상태여야 합니다. 또한 MySmallImage1, MySmallImage2, MyLargeImage1MyLargeImage2라는 이미지가 C 드라이브의 루트 디렉터리에 있어야 합니다.

Private Sub CreateMyListView()
    ' Create a new ListView control.
    Dim listView1 As New ListView()
    listView1.Bounds = New Rectangle(New Point(10, 10), New Size(300, 200))

    ' Set the view to show details.
    listView1.View = View.Details
    ' Allow the user to edit item text.
    listView1.LabelEdit = True
    ' Allow the user to rearrange columns.
    listView1.AllowColumnReorder = True
    ' Display check boxes.
    listView1.CheckBoxes = True
    ' Select the item and subitems when selection is made.
    listView1.FullRowSelect = True
    ' Display grid lines.
    listView1.GridLines = True
    ' Sort the items in the list in ascending order.
    listView1.Sorting = SortOrder.Ascending

    ' Create three items and three sets of subitems for each item.
    Dim item1 As New ListViewItem("item1", 0)
    ' Place a check mark next to the item.
    item1.Checked = True
    item1.SubItems.Add("1")
    item1.SubItems.Add("2")
    item1.SubItems.Add("3")
    Dim item2 As New ListViewItem("item2", 1)
    item2.SubItems.Add("4")
    item2.SubItems.Add("5")
    item2.SubItems.Add("6")
    Dim item3 As New ListViewItem("item3", 0)
    ' Place a check mark next to the item.
    item3.Checked = True
    item3.SubItems.Add("7")
    item3.SubItems.Add("8")
    item3.SubItems.Add("9")

    ' Create columns for the items and subitems.
    listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center)

    'Add the items to the ListView.
    listView1.Items.AddRange(New ListViewItem() {item1, item2, item3})

    ' Create two ImageList objects.
    Dim imageListSmall As New ImageList()
    Dim imageListLarge As New ImageList()

    ' Initialize the ImageList objects with bitmaps.
    imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage1.bmp"))
    imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage2.bmp"))
    imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage1.bmp"))
    imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage2.bmp"))

    'Assign the ImageList objects to the ListView.
    listView1.LargeImageList = imageListLarge
    listView1.SmallImageList = imageListSmall

    ' Add the ListView to the control collection.
    Me.Controls.Add(listView1)
End Sub 'CreateMyListView
private void CreateMyListView()
{
    // Create a new ListView control.
    ListView listView1 = new ListView();
    listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));

    // Set the view to show details.
    listView1.View = View.Details;
    // Allow the user to edit item text.
    listView1.LabelEdit = true;
    // Allow the user to rearrange columns.
    listView1.AllowColumnReorder = true;
    // Display check boxes.
    listView1.CheckBoxes = true;
    // Select the item and subitems when selection is made.
    listView1.FullRowSelect = true;
    // Display grid lines.
    listView1.GridLines = true;
    // Sort the items in the list in ascending order.
    listView1.Sorting = SortOrder.Ascending;
                
    // Create three items and three sets of subitems for each item.
    ListViewItem item1 = new ListViewItem("item1",0);
    // Place a check mark next to the item.
    item1.Checked = true;
    item1.SubItems.Add("1");
    item1.SubItems.Add("2");
    item1.SubItems.Add("3");
    ListViewItem item2 = new ListViewItem("item2",1);
    item2.SubItems.Add("4");
    item2.SubItems.Add("5");
    item2.SubItems.Add("6");
    ListViewItem item3 = new ListViewItem("item3",0);
    // Place a check mark next to the item.
    item3.Checked = true;
    item3.SubItems.Add("7");
    item3.SubItems.Add("8");
    item3.SubItems.Add("9");

    // Create columns for the items and subitems.
    listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);

    //Add the items to the ListView.
            listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});

    // Create two ImageList objects.
    ImageList imageListSmall = new ImageList();
    ImageList imageListLarge = new ImageList();

    // Initialize the ImageList objects with bitmaps.
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp"));
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp"));

    //Assign the ImageList objects to the ListView.
    listView1.LargeImageList = imageListLarge;
    listView1.SmallImageList = imageListSmall;

    // Add the ListView to the control collection.
    this.Controls.Add(listView1);
}
private:
   void CreateMyListView()
   {
      // Create a new ListView control.
      ListView^ listView1 = gcnew ListView;
      listView1->Bounds = Rectangle(Point(10,10),System::Drawing::Size( 300, 200 ));

      // Set the view to show details.
      listView1->View = View::Details;

      // Allow the user to edit item text.
      listView1->LabelEdit = true;

      // Allow the user to rearrange columns.
      listView1->AllowColumnReorder = true;

      // Display check boxes.
      listView1->CheckBoxes = true;

      // Select the item and subitems when selection is made.
      listView1->FullRowSelect = true;

      // Display grid lines.
      listView1->GridLines = true;

      // Sort the items in the list in ascending order.
      listView1->Sorting = SortOrder::Ascending;

      // Create three items and three sets of subitems for each item.
      ListViewItem^ item1 = gcnew ListViewItem( "item1",0 );

      // Place a check mark next to the item.
      item1->Checked = true;
      item1->SubItems->Add( "1" );
      item1->SubItems->Add( "2" );
      item1->SubItems->Add( "3" );
      ListViewItem^ item2 = gcnew ListViewItem( "item2",1 );
      item2->SubItems->Add( "4" );
      item2->SubItems->Add( "5" );
      item2->SubItems->Add( "6" );
      ListViewItem^ item3 = gcnew ListViewItem( "item3",0 );

      // Place a check mark next to the item.
      item3->Checked = true;
      item3->SubItems->Add( "7" );
      item3->SubItems->Add( "8" );
      item3->SubItems->Add( "9" );

      // Create columns for the items and subitems.
      listView1->Columns->Add( "Item Column", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 2", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 3", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 4", -2, HorizontalAlignment::Center );

      //Add the items to the ListView.
      array<ListViewItem^>^temp1 = {item1,item2,item3};
      listView1->Items->AddRange( temp1 );

      // Create two ImageList objects.
      ImageList^ imageListSmall = gcnew ImageList;
      ImageList^ imageListLarge = gcnew ImageList;

      // Initialize the ImageList objects with bitmaps.
      imageListSmall->Images->Add( Bitmap::FromFile( "C:\\MySmallImage1.bmp" ) );
      imageListSmall->Images->Add( Bitmap::FromFile( "C:\\MySmallImage2.bmp" ) );
      imageListLarge->Images->Add( Bitmap::FromFile( "C:\\MyLargeImage1.bmp" ) );
      imageListLarge->Images->Add( Bitmap::FromFile( "C:\\MyLargeImage2.bmp" ) );

      //Assign the ImageList objects to the ListView.
      listView1->LargeImageList = imageListLarge;
      listView1->SmallImageList = imageListSmall;
      
      // Add the ListView to the control collection.
      this->Controls->Add( listView1 );
   }
private void CreateMyListView()
{
    // Create a new ListView control.
    ListView listView1 = new ListView();
    listView1.set_Bounds(new Rectangle(new Point(10, 10), 
        new Size(300, 200)));

    // Set the view to show details.
    listView1.set_View(View.Details);

    // Allow the user to edit item text.
    listView1.set_LabelEdit(true);

    // Allow the user to rearrange columns.
    listView1.set_AllowColumnReorder(true);

    // Display check boxes.
    listView1.set_CheckBoxes(true);

    // Select the item and subitems when selection is made.
    listView1.set_FullRowSelect(true);

    // Display grid lines.
    listView1.set_GridLines(true);

    // Sort the items in the list in ascending order.
    listView1.set_Sorting(SortOrder.Ascending);

    // Create three items and three sets of subitems for each item.
    ListViewItem item1 = new ListViewItem("item1", 0);

    // Place a check mark next to the item.
    item1.set_Checked(true);

    item1.get_SubItems().Add("1");
    item1.get_SubItems().Add("2");
    item1.get_SubItems().Add("3");

    ListViewItem item2 = new ListViewItem("item2", 1);
    item2.get_SubItems().Add("4");
    item2.get_SubItems().Add("5");
    item2.get_SubItems().Add("6");

    ListViewItem item3 = new ListViewItem("item3", 0);

    // Place a check mark next to the item.
    item3.set_Checked(true);

    item3.get_SubItems().Add("7");
    item3.get_SubItems().Add("8");
    item3.get_SubItems().Add("9");

    // Create columns for the items and subitems.
    listView1.get_Columns().Add("Item Column", -2, 
        HorizontalAlignment.Left);
    listView1.get_Columns().Add("Column 2", -2, HorizontalAlignment.Left);
    listView1.get_Columns().Add("Column 3", -2, HorizontalAlignment.Left);
    listView1.get_Columns().Add("Column 4", -2, HorizontalAlignment.Center);

    //Add the items to the ListView.
    listView1.get_Items().AddRange(new ListViewItem[] { item1, item2, 
        item3 });

    // Create two ImageList objects.
    ImageList imageListSmall = new ImageList();
    ImageList imageListLarge = new ImageList();

    // Initialize the ImageList objects with bitmaps.
    imageListSmall.get_Images().Add(Bitmap.FromFile(
        "C:\\MySmallImage1.bmp"));
    imageListSmall.get_Images().Add(Bitmap.FromFile(
        "C:\\MySmallImage2.bmp"));
    imageListLarge.get_Images().Add(Bitmap.FromFile(
        "C:\\MyLargeImage1.bmp"));
    imageListLarge.get_Images().Add(Bitmap.FromFile(
        "C:\\MyLargeImage2.bmp"));

    //Assign the ImageList objects to the ListView.
    listView1.set_LargeImageList(imageListLarge);
    listView1.set_SmallImageList(imageListSmall);

    // Add the ListView to the control collection.
    this.get_Controls().Add(listView1);
} //CreateMyListView

상속 계층 구조

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
        System.Windows.Forms.ListView

스레드로부터의 안전성

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

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, 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에서 지원

참고 항목

참조

ListView 멤버
System.Windows.Forms 네임스페이스
ListViewItem
ImageList 클래스
Application.EnableVisualStyles