ListViewItem 类

定义

表示 ListView 控件中的一个项。Represents an item in a ListView control.

public ref class ListViewItem : ICloneable, System::Runtime::Serialization::ISerializable
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.ListViewItemConverter))]
[System.Serializable]
public class ListViewItem : ICloneable, System.Runtime.Serialization.ISerializable
type ListViewItem = class
    interface ICloneable
    interface ISerializable
Public Class ListViewItem
Implements ICloneable, ISerializable
继承
ListViewItem
属性
实现

示例

下面的代码示例创建一个 ListView 控件,其中指定了三个 ListViewItem 对象和为每个项指定了三个 ListViewItem.ListViewSubItem 对象。The following code example creates a ListView control with three ListViewItem objects specified and three ListViewItem.ListViewSubItem objects specified for each item. 该示例还创建 ColumnHeader 对象以在详细信息视图中显示子项。The example also creates ColumnHeader objects to display the subitems in details view. 在代码示例中还会创建两个 ImageList 对象,以便为 ListViewItem 对象提供图像。Two ImageList objects are also created in the code example to provide images for the ListViewItem objects. 这些 ImageList 对象将添加到 LargeImageListSmallImageList 属性。These ImageList objects are added to the LargeImageList and SmallImageList properties. 该示例使用以下属性创建 ListView 控件:The example uses the following properties in creating the ListView control:

需要将代码添加到 Form,并从构造函数或窗体上的其他方法调用在示例中创建的方法。You need to add the code to a Form and call the method created in the example from the constructor or another method on the form. 该示例要求名为 MySmallImage1MySmallImage2MyLargeImage1MyLargeImage2 的映像位于驱动器 C 的根目录中。The example requires that images named MySmallImage1, MySmallImage2, MyLargeImage1, and MyLargeImage2 are located in the root directory of drive C.

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.
      // Width of -2 indicates auto-size.
      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.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.
    // Width of -2 indicates auto-size.
    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 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.
    ' Width of -2 indicates auto-size.
    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

注解

ListView 控件类似于 ListBox 显示项列表。The ListView control is similar to a ListBox in that it displays a list of items. 主要区别在于 ListView 控件提供了许多不同的方式,用户可以查看这些项目。The main difference is that the ListView control provides a number of different ways items can be viewed by the user. ListViewItem 类定义与 ListView 控件中显示的项相关联的外观、行为和数据。The ListViewItem class defines the appearance, behavior, and data associated with an item that is displayed in the ListView control. ListViewItem 对象可以在 ListView 控件中显示为四种不同视图之一。ListViewItem objects can be displayed in the ListView control in one of four different views. 项目可显示为大图标或小图标,或显示为垂直列表中的小图标。Items can be displayed as large or small icons or as small icons in a vertical list. 项还可以具有包含与父项相关的信息的子项。Items can also have subitems that contain information that is related to the parent item. 使用第四个视图样式的 "详细信息" 视图,可以在具有列标题的网格中显示项及其子项,可用于标识子项中显示的信息。The fourth view style, details view, allows you to display the item and its subitems in a grid with column headers that can be used to identify the information being displayed in a subitem.

ListViewItem 类的大部分属性提供了更改与之关联的 ListView 控件中项的显示方式的方法。Most of the properties of the ListViewItem class provide ways to change the display of the item in the ListView control it is associated with. BackColorForeColorFont 属性允许您更改项文本在 ListView 控件中的显示方式。The BackColor, ForeColor, and Font properties allow you to change how the text of the item is displayed in the ListView control. ImageIndex 属性允许您指定要从分配给 ListView 控件的 ImageList 加载的图像(通过设置 SmallImageListLargeImageListListView属性)。The ImageIndex property allows you to specify the image to load from the ImageList that is assigned to the ListView control (by setting the LargeImageList or SmallImageList properties of the ListView). 项可以显示复选框,以便以类似于 CheckedListBox 控件的方式从用户处获取项选择。Items can display check boxes in order to obtain item choices from the user in a way similar to a CheckedListBox control. 您可以使用 Checked 属性来确定项是否已选中,或在运行时选中或清除复选框。You can use the Checked property to determine if an item is checked, or to select or clear the check box at run time. 当关联的 ListView 控件的 View 属性设置为 Details,并且在 ListView 控件的 ListView.ColumnHeaderCollection 中定义列时,项可以显示任意数量的子项。Items can display any number of subitems when the View property of the associated ListView control is set to Details and columns are defined in the ListView.ColumnHeaderCollection of the ListView control. 可以通过调用 ListViewItem.ListViewSubItemCollection 类的 Add 方法,将子项添加到项。You can add subitems to an item by calling the Add method of the ListViewItem.ListViewSubItemCollection class. SubItems 属性允许你获取对 ListViewItem.ListViewSubItemCollection 类及其成员的访问权限。The SubItems property allows you to gain access to the ListViewItem.ListViewSubItemCollection class and its members.

ListViewItem 类的某些属性和方法是 ListView 控件中特定于项的属性和方法版本。Some of the properties and methods of the ListViewItem class are item-specific versions of properties and methods in the ListView control. 例如,EnsureVisible 方法类似于方法的 ListView 版本,但 ListViewItem 版本只影响当前项。For example, the EnsureVisible method is similar to the ListView version of the method, but the ListViewItem version affects only the current item.

ListViewItem 类还提供了不是 ListView 方法版本的方法。The ListViewItem class also provides methods that are not versions of ListView methods. BeginEdit 方法将项的文本置于编辑模式,以便用户可以更改项的文本(ListView 控件的 LabelEdit 属性设置为 true)时。The BeginEdit method places the item's text into edit mode so the user can change the item's text (when the LabelEdit property of the ListView control is set to true). 利用 Clone 方法,您可以创建现有 ListViewItem 对象的副本,以便在其他 ListView 控件中重复使用。The Clone method allows you to create copies of existing ListViewItem objects to reuse in other ListView controls.

构造函数

ListViewItem()

使用默认值初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with default values.

ListViewItem(ListViewGroup)

初始化 ListViewItem 类的新实例,并将它分配到指定的组。Initializes a new instance of the ListViewItem class and assigns it to the specified group.

ListViewItem(ListViewItem+ListViewSubItem[], Int32)

用项图标的图像索引位置和 ListViewItem 对象的数组初始化 ListViewItem.ListViewSubItem 类的新实例。Initializes a new instance of the ListViewItem class with the image index position of the item's icon and an array of ListViewItem.ListViewSubItem objects.

ListViewItem(ListViewItem+ListViewSubItem[], Int32, ListViewGroup)

用项图标的图像索引位置和 ListViewItem 对象的数组初始化 ListViewItem.ListViewSubItem 类的新实例,并将该项分配到指定的组。Initializes a new instance of the ListViewItem class with the image index position of the item's icon and an array of ListViewItem.ListViewSubItem objects, and assigns the item to the specified group.

ListViewItem(ListViewItem+ListViewSubItem[], String)

用指定的子项和图像初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with the specified subitems and image.

ListViewItem(ListViewItem+ListViewSubItem[], String, ListViewGroup)

用指定的子项、图像和组初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with the specified subitems, image, and group.

ListViewItem(SerializationInfo, StreamingContext)

使用指定的序列化信息和流上下文初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with the specified serialization information and streaming context.

ListViewItem(String)

用指定的项文本初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with the specified item text.

ListViewItem(String, Int32)

用指定的项文本和项图标的图像索引位置初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with the specified item text and the image index position of the item's icon.

ListViewItem(String, Int32, ListViewGroup)

用指定的项文本和项图标的图像索引位置初始化 ListViewItem 类的新实例,并将该项分配到指定的组。Initializes a new instance of the ListViewItem class with the specified item text and the image index position of the item's icon, and assigns the item to the specified group.

ListViewItem(String, ListViewGroup)

用指定的项文本初始化 ListViewItem 类的新实例,并将它分配到指定的组。Initializes a new instance of the ListViewItem class with the specified item text and assigns it to the specified group.

ListViewItem(String, String)

用指定的文本和图像初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with the specified text and image.

ListViewItem(String, String, ListViewGroup)

用指定的文本、图像和组初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with the specified text, image, and group.

ListViewItem(String[])

用表示子项的字符串数组初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with an array of strings representing subitems.

ListViewItem(String[], Int32)

用项图标的图像索引位置和表示子项的字符串数组初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with the image index position of the item's icon and an array of strings representing subitems.

ListViewItem(String[], Int32, Color, Color, Font)

用项图标的图像索引位置、前景色、背景色、项的字体和表示子项的字符串数组初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with the image index position of the item's icon; the foreground color, background color, and font of the item; and an array of strings representing subitems.

ListViewItem(String[], Int32, Color, Color, Font, ListViewGroup)

用项图标的图像索引位置、前景色、背景色、项的字体和表示子项的字符串数组初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with the image index position of the item's icon; the foreground color, background color, and font of the item; and an array of strings representing subitems. 将该项分配到指定的组。Assigns the item to the specified group.

ListViewItem(String[], Int32, ListViewGroup)

用项图标的图像索引位置和表示子项的字符串数组初始化 ListViewItem 类的新实例,并将该项分配到指定的组。Initializes a new instance of the ListViewItem class with the image index position of the item's icon and an array of strings representing subitems, and assigns the item to the specified group.

ListViewItem(String[], ListViewGroup)

用表示子项的字符串数组初始化 ListViewItem 类的新实例,并将该项分配到指定的组。Initializes a new instance of the ListViewItem class with an array of strings representing subitems, and assigns the item to the specified group.

ListViewItem(String[], String)

用指定的项和子项文本及图像初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with the specified item and subitem text and image.

ListViewItem(String[], String, Color, Color, Font)

用包含指定的文本、图像、颜色和字体的子项初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with the subitems containing the specified text, image, colors, and font.

ListViewItem(String[], String, Color, Color, Font, ListViewGroup)

用包含指定的文本、图像、颜色、字体和组的子项初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with the subitems containing the specified text, image, colors, font, and group.

ListViewItem(String[], String, ListViewGroup)

用包含指定文本、图像和组的子项初始化 ListViewItem 类的新实例。Initializes a new instance of the ListViewItem class with subitems containing the specified text, image, and group.

属性

BackColor

获取或设置该项文本的背景色。Gets or sets the background color of the item's text.

Bounds

获取该项(包括子项)的边框。Gets the bounding rectangle of the item, including subitems.

Checked

获取或设置一个值,该值指示是否选中此项。Gets or sets a value indicating whether the item is checked.

Focused

获取或设置一个值,该值指示该项在 ListView 控件中是否具有焦点。Gets or sets a value indicating whether the item has focus within the ListView control.

Font

获取或设置由该项显示的文本的字体。Gets or sets the font of the text displayed by the item.

ForeColor

获取或设置该项文本的前景色。Gets or sets the foreground color of the item's text.

Group

获取或设置该项所分配到的组。Gets or sets the group to which the item is assigned.

ImageIndex

获取或设置为该项显示的图像的索引。Gets or sets the index of the image that is displayed for the item.

ImageKey

获取或设置为该项显示的图像的键。Gets or sets the key for the image that is displayed for the item.

ImageList

获取 ImageList,它包含与该项一起显示的图像。Gets the ImageList that contains the image displayed with the item.

IndentCount

获取或设置 ListViewItem 要缩进的小图像宽度的数量。Gets or sets the number of small image widths by which to indent the ListViewItem.

Index

获取 ListView 控件中该项的从零开始的索引。Gets the zero-based index of the item within the ListView control.

ListView

获取包含该项的 ListView 控件。Gets the ListView control that contains the item.

Name

获取或设置与此 ListViewItem 关联的名称。Gets or sets the name associated with this ListViewItem.

Position

获取或设置 ListViewItem 的左上角的位置。Gets or sets the position of the upper-left corner of the ListViewItem.

Selected

获取或设置指示是否选定了项的值。Gets or sets a value indicating whether the item is selected.

StateImageIndex

获取或设置为该项显示的状态图像(指示项的状态的图像,例如,选中的复选框或已清除的复选框)的索引。Gets or sets the index of the state image (an image such as a selected or cleared check box that indicates the state of the item) that is displayed for the item.

SubItems

获取包含该项的所有子项的集合。Gets a collection containing all subitems of the item.

Tag

获取或设置包含数据的对象以与该项关联。Gets or sets an object that contains data to associate with the item.

Text

获取或设置该项的文本。Gets or sets the text of the item.

ToolTipText

获取或设置当鼠标指针停留在 ListViewItem 上时显示的文本。Gets or sets the text shown when the mouse pointer rests on the ListViewItem.

UseItemStyleForSubItems

获取或设置一个值,该值指示项的 FontForeColorBackColor 属性是否用于其所有子项。Gets or sets a value indicating whether the Font, ForeColor, and BackColor properties for the item are used for all its subitems.

方法

BeginEdit()

将项文本置于编辑模式中。Places the item text into edit mode.

Clone()

创建该项的完全相同的副本。Creates an identical copy of the item.

Deserialize(SerializationInfo, StreamingContext)

反序列化该项。Deserializes the item.

EnsureVisible()

确保该项在控件中是可见的,必要时滚动控件的内容。Ensures that the item is visible within the control, scrolling the contents of the control, if necessary.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
FindNearestItem(SearchDirectionHint)

ListViewItem 查找下一个项,按指定的方向搜索。Finds the next item from the ListViewItem, searching in the specified direction.

GetBounds(ItemBoundsPortion)

为该项检索边框的指定部分。Retrieves the specified portion of the bounding rectangle for the item.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetSubItemAt(Int32, Int32)

返回位于指定坐标位置的 ListViewItem 的子项。Returns the subitem of the ListViewItem at the specified coordinates.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Remove()

从关联的 ListView 控件中移除该项。Removes the item from its associated ListView control.

Serialize(SerializationInfo, StreamingContext)

序列化该项。Serializes the item.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

显式接口实现

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

序列化该项。Serializes the item.

适用于

另请参阅