Export (0) Print
Expand All

ListViewItem.ListViewSubItem Class

Represents a subitem of a ListViewItem.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
<SerializableAttribute> _
Public Class ListViewSubItem
'Usage
Dim instance As ListViewItem.ListViewSubItem

A ListView control displays a list of items that are defined by the ListViewItem class. Each ListViewItem can store subitem objects that are defined by the ListViewItem.ListViewSubItem class. 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. 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. 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).

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. 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. 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.

The following code example creates a ListView control with three ListViewItem objects specified and three ListViewItem.ListViewSubItem objects specified for each item. The example also creates ColumnHeader objects to display the subitems in details view. Two ImageList objects are also created in the code example to provide images for the ListViewItem objects. These ImageList objects are added to the LargeImageList and SmallImageList properties. The example uses the following properties in creating the ListView control.

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. The example requires that images named MySmallImage1, MySmallImage2, MyLargeImage1, and MyLargeImage2 are located in the root directory of drive 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

System.Object
  System.Windows.Forms.ListViewItem.ListViewSubItem

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft