ListViewItem.ListViewSubItem Class

Definition

表示 ListViewItem 的子项。Represents a subitem of a ListViewItem.

public: ref class ListViewItem::ListViewSubItem
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.ListViewSubItemConverter))]
[System.Serializable]
public class ListViewItem.ListViewSubItem
type ListViewItem.ListViewSubItem = class
Public Class ListViewItem.ListViewSubItem
Inheritance
ListViewItem.ListViewSubItem
Attributes

Examples

下面的代码示例创建一个 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

Remarks

ListView 控件显示由 ListViewItem 类定义的项的列表。A ListView control displays a list of items that are defined by the ListViewItem class. 每个 ListViewItem 都可以存储由 ListViewItem.ListViewSubItem 类定义的子项对象。Each ListViewItem can store subitem objects that are defined by the ListViewItem.ListViewSubItem class. ListView 控件的 View 属性设置为 Details时,将显示子项。Subitems are displayed when the View property of the ListView control is set to Details. 通常,子项包含与它们的父项相关的信息。Typically, subitems contain information that is related to their parent item. 例如,如果 ListView 控件显示表示订单的项,则每个项都可以显示订单号。For example, if a ListView control displays items that represent orders, each item could display the order number. 可以将子项添加到每个项以显示信息,如订购的产品数量、订购的商品的数量以及订购的商品的总价。Subitems could be added to each item to display information such as the product ordered, the quantity of items ordered, and the total price of the items ordered. ListViewItem 对象不同,用户不能直接编辑 ListViewItem.ListViewSubItem 对象(如果 ListView 控件的 LabelEdit 属性设置为 true,则用户可以编辑 ListViewItem 对象)。Unlike ListViewItem objects, ListViewItem.ListViewSubItem objects cannot be edited directly by the user (the user can edit ListViewItem objects if the LabelEdit property of the ListView control is set to true).

由于子项不能由用户直接编辑,并且不显示图像,因此属性被限制为在 ListView 控件中显示时,影响子项文本样式的属性。Because subitems cannot be directly edited by the user and do not display images, properties are limited to those that affect the style of the subitem text when it is displayed in the ListView control. 如果包含子项的 ListViewUseItemStyleForSubItems 属性设置为 false,则可以使用 FontBackColorForeColor 属性更改应用于文本显示的样式。If the UseItemStyleForSubItems property of the ListView that contains the subitems is set to false, you can use the Font, BackColor, and ForeColor properties to change the styles applied to the text display. 通常,项和子项的样式在 ListView 控件中是相同的,但是如果要更改特定 ListViewItem.ListViewSubItem 的样式以突出显示它,则可以在要以不同方式显示的项上使用这些属性。Typically, the styles of the item and the subitems are the same in a ListView control, but if you want to change the style of a specific ListViewItem.ListViewSubItem to highlight it, you can use these properties on the items you want to display differently.

Constructors

ListViewItem.ListViewSubItem()

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

ListViewItem.ListViewSubItem(ListViewItem, String)

用指定的所有者和文本初始化 ListViewItem.ListViewSubItem 类的新实例。Initializes a new instance of the ListViewItem.ListViewSubItem class with the specified owner and text.

ListViewItem.ListViewSubItem(ListViewItem, String, Color, Color, Font)

用指定的所有者、文本、前景色、背景色和字体值初始化 ListViewItem.ListViewSubItem 类的新实例。Initializes a new instance of the ListViewItem.ListViewSubItem class with the specified owner, text, foreground color, background color, and font values.

Properties

BackColor

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

Bounds

获取 ListViewItem.ListViewSubItem 的边框。Gets the bounding rectangle of the ListViewItem.ListViewSubItem.

Font

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

ForeColor

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

Name

获取或设置 ListViewItem.ListViewSubItem 的名称。Gets or sets the name of the ListViewItem.ListViewSubItem.

Tag

获取或设置包含有关 ListViewItem.ListViewSubItem 的数据的对象。Gets or sets an object that contains data about the ListViewItem.ListViewSubItem.

Text

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

Methods

Equals(Object)

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

(Inherited from Object)
GetHashCode()

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

(Inherited from Object)
GetType()

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

(Inherited from Object)
MemberwiseClone()

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

(Inherited from Object)
ResetStyle()

将应用于子项的样式重置为默认字体和颜色。Resets the styles applied to the subitem to the default font and colors.

ToString()

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

Applies to

See also