ListViewItem 类

表示 ListView 控件中的项。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<SerializableAttribute> _
Public Class ListViewItem
    Implements ICloneable, ISerializable
用法
Dim instance As ListViewItem
[SerializableAttribute] 
public class ListViewItem : ICloneable, ISerializable
[SerializableAttribute] 
public ref class ListViewItem : ICloneable, ISerializable
/** @attribute SerializableAttribute() */ 
public class ListViewItem implements ICloneable, ISerializable
SerializableAttribute 
public class ListViewItem implements ICloneable, ISerializable

备注

ListView 控件与 ListBox 相似,因为它显示了项的列表。主要区别是 ListView 控件为用户提供了一些查看项的不同方式。ListViewItem 类定义了与 ListView 控件中显示的项相关联的外观、行为和数据。ListViewItem 对象可用四种不同视图之一在 ListView 控件中显示。项可以显示为大图标或小图标,或在垂直列表中显示为小图标。项也可以具有子项(子项中包含了与父项相关的信息)。第四种视图样式(详细资料视图)使您可以在具有列标题(可用于识别在子项中显示的信息)的网格中显示项及其子项。

ListViewItem 类的多数属性均提供了更改项在与之关联的 ListView 控件中显示的方法。BackColorForeColorFont 属性使您可以更改项的文本在 ListView 控件中的显示方式。ImageIndex 属性使您可以指定要从分配给 ListView 控件的 ImageList 中加载的图像(通过设置 ListViewLargeImageListSmallImageList 属性)。项可以显示复选框,以便使用与 CheckedListBox 控件相似的方式从用户获取项选项。您可以使用 Checked 属性来确定是否选中了某项,或者在运行时选中或取消选中该复选框。当相关联的 ListView 控件的 View 属性设置为 Details 并且在 ListView 控件的 ListView.ColumnHeaderCollection 中定义了列时,项可以显示任意数量的子项。您可以通过调用 ListViewItem.ListViewSubItemCollection 类的 Add 方法来给项添加子项。SubItems 属性使您可以获得对 ListViewItem.ListViewSubItemCollection 类及其成员的访问权。

ListViewItem 类的一些属性和方法是 ListView 控件中属性和方法的项特定的版本。例如,EnsureVisible 方法与该方法的 ListView 版本相似,但 ListViewItem 版本只影响当前项。

ListViewItem 类还提供不是 ListView 方法的版本的方法。BeginEdit 方法将项的文本置于编辑模式中,因此用户可以更改项的文本(当 ListView 控件的 LabelEdit 属性设置为 true 时)。Clone 方法使您可以为现有 ListViewItem 对象创建一些副本,以在其他 ListView 控件中重新使用。

示例

下面的代码示例创建一个 ListView 控件,其中带有三个指定的 ListViewItem 对象,而这三个对象中的每一项又带有三个指定的 ListViewItem.ListViewSubItem 对象。该示例还创建 ColumnHeader 对象以便在详细信息视图中显示子项。在代码示例中还创建两个 ImageList 对象,以便为 ListViewItem 对象提供图像。这些 ImageList 对象被添加到 LargeImageListSmallImageList 属性中。在创建 ListView 控件的过程中,该示例使用下列属性:

您需要向 Form 添加代码,并从构造函数或窗体上的另一个方法来调用在该示例中创建的方法。该示例要求驱动器 C 的根目录下有名为 MySmallImage1MySmallImage2MyLargeImage1MyLargeImage2 的图像。

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.Windows.Forms.ListViewItem

线程安全

此类型的任何公共静态(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

请参见

参考

ListViewItem 成员
System.Windows.Forms 命名空间
ListView 类
ListViewItem.ListViewSubItem

其他资源

ListView 控件(Windows 窗体)