ListViewItem.ListViewSubItem ListViewItem.ListViewSubItem ListViewItem.ListViewSubItem ListViewItem.ListViewSubItem Class

定义

表示 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
继承
ListViewItem.ListViewSubItemListViewItem.ListViewSubItemListViewItem.ListViewSubItemListViewItem.ListViewSubItem
属性

示例

下面的代码示例创建一个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对象将添加LargeImageList到和SmallImageList属性。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. 该示例要求MySmallImage1名为MyLargeImage1MySmallImage2、和MyLargeImage2的映像位于驱动器 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控件显示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. View ListView控件的属性设置为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控件显示表示 orders 的项, 则每个项都可以显示订单号。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属性ListViewItem设置为true, 则用户可以编辑对象)。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. Font false BackColor ForeColor如果包含子项的的ListView属性设置为, 则可以使用、和属性更改应用于文本显示的样式。 UseItemStyleForSubItemsIf 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.

构造函数

ListViewItem.ListViewSubItem() ListViewItem.ListViewSubItem() ListViewItem.ListViewSubItem() ListViewItem.ListViewSubItem()

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

ListViewItem.ListViewSubItem(ListViewItem, String) ListViewItem.ListViewSubItem(ListViewItem, String) ListViewItem.ListViewSubItem(ListViewItem, String) 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(ListViewItem, String, Color, Color, Font) ListViewItem.ListViewSubItem(ListViewItem, String, Color, Color, Font) 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.

属性

BackColor BackColor BackColor BackColor

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

Bounds Bounds Bounds Bounds

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

Font Font Font Font

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

ForeColor ForeColor ForeColor ForeColor

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

Name Name Name Name

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

Tag Tag Tag Tag

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

Text Text Text Text

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

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
ResetStyle() ResetStyle() ResetStyle() ResetStyle()

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

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

适用于

另请参阅