H o m e

 

M e e t    t h e    W i n    F o r m s    L i s t V i e w    C l a s s    

By Michael McIntyre

mikemc@getdotnetcode.com

 

This article introduces the Windows Forms ListView class and the related ListViewItem and ListViewSubItem classes. 

 

A ListView object is a Widows Forms control that displays ListViewItems and may display ListViewSubItems.

 

The first part of this article provides three code examples: 1) add ListViewItems to a ListView; 2) add ListViewSubItems to a ListViewItem; and 3) add columns to a ListView and set the ListView’s View property to show ListViewSubItems.

 

To try the example code in this article, prepare a Visual Studio.Net project as described below.

 

1.       Create a new VB.NET Windows Forms project.

2.       Add a ListView control named “DemoListView” to Form1.

3.       Add a Button named “AddListViewItemsButton” to Form1.

4.       Add a Button named ‘AddListViewSubItemsButton” to Form1.

5.       Add a Button named “ViewDetailsButton’ to Form1.

 

Example 1:  Add ListViewItems.

 

ListView items can be added using the Property Grid at design time or programmatically at run time. Below are examples of three different ways to add ListViewItems. Add the code to the “AddListViewItemsButton” button’s Click handler in Form1.

 

' Simple Add.

' Call DemoListView's Add method passing

' in a string to add one ListViewItem.

DemoListView.Items.Add("ListViewItem1")

 

' Declare, instantiate, and add a ListViewItem

Dim listViewItem2 As New ListViewItem("ListViewItem2")

DemoListView.Items.Add(listViewItem2)

 

' Declare, instantiate, and add three ListViewItems

' using the ListView AddRange method.

Dim listViewItem3 As New ListViewItem("ListViewItem3")

Dim listViewItem4 As New ListViewItem("ListViewItem4")

Dim listViewItem5 As New ListViewItem("ListViewItem5")

DemoListView.Items.AddRange(New ListViewItem() {listViewItem3, listViewItem4, listViewItem5})

 

Example 2: Add ListViewSubItems.

 

ListViewSubItems can be added using the Property Grid at design time or programmatically at run time. Below are examples of two different ways to add ListViewSubItems. Add the code to the “AddListViewSubItemsButton” button’s Click handler in Form1.

 

' Simple Add.

' Call DemoListView's SubItems.Add method passing

' in a parent ListViewItem and a string to add one ListViewSubItem.

' Declare and instantiate a parent ListViewItem.

Dim lvItem6 As New ListViewItem("lvItem6")

' Add a ListViewSubItem to lvItem6 (parent).

lvItem6.SubItems.Add(New ListViewItem.ListViewSubItem(lvItem6, "lvItem6sItem1"))

' Add lvItem6 and its one ListViewSubItem to DemoListView

Me.DemoListView.Items.Add(lvItem6)

 

' Declare, instantiate, and add three ListViewSubItems

' using DemoListView's SubItems.AddRange method.

' Declare and instantiate a parent ListViewItem.

Dim lvItem7 As New ListViewItem("lvItem7")

' Declare and instantiate three ListViewSubItems;

' parent for all three is lvItem7.

Dim lvItem7sItem1 As New ListViewItem.ListViewSubItem(lvItem7, "lvItem7sItem1")

Dim lvItem7sItem2 As New ListViewItem.ListViewSubItem(lvItem7, "lvItem7sItem2")

Dim lvItem7sItem3 As New ListViewItem.ListViewSubItem(lvItem7, "lvItem7sItem3")

' Call lvItem7's AddRange method to add the three ListViewItems to DemoListView.

lvItem7.SubItems.AddRange(New ListViewItem.ListViewSubItem() {lvItem7sItem1, lvItem7sItem2, lvItem7sItem3})

' Add lvItem7 and its three ListViewSubItems to DemoListView

Me.DemoListView.Items.Add(lvItem7)

 

Example 3: Add column to DemoListView and set its View property to show its ListViewSubItems.

 

To view ListViewSubItems it is necessary to add Columns to a ListView and set the ListView’s View property to Details.

 

ListView Columns can be added using the Property Grid at design time or programmatically at run time. Below is an example that shows how to add Columns and set the ListView View property to Details. Add the code to the “ViewDetailsButton” button’s Click handler in Form1.

 

' Add Columns to DemoListView.

' The first column added can show a ListViewItem.

' Add a column for ListViewItems.

DemoListView.Columns.Add("ListViewItem", 60, HorizontalAlignment.Left)

 

' Each additional column added can show a ListViewItem's ListViewSubItems.

' Add a column to show each ListViewItem's 1st ListViewSubItem.

DemoListView.Columns.Add("SubItem1", 100, HorizontalAlignment.Left)

' Add a column to show each ListViewItem's 2nd ListViewSubItem.

DemoListView.Columns.Add("SubItem2", 100, HorizontalAlignment.Left)

' Add a column to show each ListViewItem's 3rd ListViewSubItem.

DemoListView.Columns.Add("SubItem3", 100, HorizontalAlignment.Left)

 

' Set DemoListView's View property to Details.

Me.DemoListView.View = View.Details

 

Try The Code

 

Run your project now. Click the AddListViewItemsButton button, then the  AddListViewSubItemsButton button, and finally the ViewDetailsButton button.  DemoListView should show the ListViewItems and ListViewSubItems in the columns you created.

 

Moving On

 

The next part of the article provides four code examples: 1) get ListViewItem by Text; 2) get ListViewSubItem by Text; 3) change ListViewItem; and 4) change ListViewSubItem.

 

To try the example code in this part of the article, add two buttons to Form1 to your project.

 

1.       Add a Button named “ChangeListViewItemsButton” to Form1.

2.       Add a Button named ‘ChangeListViewSubItemsButton” to Form1.

 

Example 1:  Get ListViewItem by Text

 

Add this function to Form1.

 

Private Function FindListViewItemByText(ByVal listViewItemText As String) As ListViewItem

   Dim i As Integer

   ' Loop through DemoListView’s ListViewItems.

   For i = 1 To DemoListView.Items.Count – 1

       ' If found, return a reference to the ListViewItem.

       If DemoListView.Items(i).Text = listViewItemText Then

           Return DemoListView.Items(i)

       End If

   Next

   ' If not found return Nothing.

   Return Nothing

End Function

 

Example 2: Get ListViewSubItem by Text.

 

Add this function to Form1.

 

Private Function FindListViewSubItemByText(ByVal listViewItemText As String, ByVal listViewSubItemText As String) As ListViewItem.ListViewSubItem

   Dim i As Integer

   ' Loop through DemoListView’s ListViewItems.

   For i = 1 To DemoListView.Items.Count - 1

       If DemoListView.Items(i).Text = listViewItemText Then

           Dim ii As Integer

           ' Loop through the ListViewItem’s ListViewSubItems.

           For ii = 1 To DemoListView.Items(i).SubItems.Count – 1

               ' If found, return a reference to the ListViewSubItem.

               If DemoListView.Items(i).SubItems(ii).Text = listViewSubItemText Then

                   Return DemoListView.Items(i).SubItems(ii)

               End If

           Next

       End If

   Next

   ' If not found return Nothing.

   Return Nothing

End Function

 

Example 3: Change ListViewItem

 

Add the code to the “ChangeListViewItemButton” button’s Click handler in Form1. This code calls the function shown in example one above.

 

' Find ListViewItem and change it using its Text property.

' See FindListViewItemByText function for code.

If Not FindListViewItemByText("lvItem6") Is Nothing Then

     FindListViewItemByText("lvItem6").Text = " lvItem6_edited.”"

Else

     MessageBox.Show("ListViewItem not found.")

End If

 

Example 4: Change ListViewSubItem

 

Add the code to the “ChangeListViewSubItemButton” button’s Click handler in Form1. This code calls the function shown in example two above.

 

' Find ListViewSubItem using its text and the text of its parent.

' See FindListViewSubItemByText function for code.

If Not FindListViewSubItemByText("lvItem7", "lvItem7sItem2") Is Nothing Then

     FindListViewSubItemByText("lvItem7", "lvItem7sItem2").Text = "Changed by Example 8."        Else

     MessageBox.Show("ListViewSubItem not found.")

End If

 

Try The Code

 

Run your project now. Click the AddListViewItemsButton button, then the AddListViewSubItemsButton button. Next click the ChangeListViewItemButton, then the ChangeListViewSubItem buttons to see the code in action.

 

This part of the article includes two code examples: 1) change the appearance of a ListViewItem; and 2) change the appearance of a ListViewSubItem.

 

To try the example code in this article, add two buttons to Form1 in your project.

 

1.       Add a Button named “ListViewItemAppearanceButton” to Form1.

2.       Add a Button named ‘ListViewSubItemAppearanceButton” to Form1.

 

Example 1:  Change the appearance of a ListViewItem

 

Add this code to the ListViewItemAppearanceButton.

 

' Change appearance of ListViewItems.

Dim listViewItemFont As New Font("Arial", 10, FontStyle.Italic)

' Change the appearance of all ListViewItems.

Dim i As Integer

For i = 0 To DemoListView.Items.Count - 1

     Dim listViewItem As ListViewItem = DemoListView.Items(i)

     listViewItem.ForeColor = Color.Red

     listViewItem.BackColor = Color.LightBlue

     listViewItem.Font = listViewItemFont

Next

 

Example 2: Change the appearance of a ListViewSubItem

 

Add this code to the ListViewSubItemAppearanceButton.

 

' Add a new ListViewItem with one ListViewSubItems, with

' different appearances for the ListViewItem and the ListViewSubItem.

 

' Declare and instantiate a new ListViewItem.

Dim lvItem8 As New ListViewItem("lvItem8")

' Set lvItem8's UseItemStyleForSubItems property to false

' so that the appearance of its ListViewSubItems can changed.

lvItem8.UseItemStyleForSubItems = False

' Set lvItem8's BackgroundColor to Red.

lvItem8.ForeColor = Color.Red

' Declare and instantiate a new ListViewSubItem with

' a ForeColor of Red, a BackColor of Blue, and a Font of Verdana size 7.

Dim lvItem8sItem1 As New ListViewItem.ListViewSubItem(lvItem8, "lvItem8sItem1", Color.Red, Color.Blue, New Font("Verdana", 7))

' Add the ListViewSubItem to the ListViewItem.

lvItem8.SubItems.Add(lvItem8sItem1)

 

' Add the ListViewItem to DemoListView.

Me.DemoListView.Items.Add(lvItem8)

 

Gotcha: If you later change the appearance of the ListViewItem it will over-write the appearance of the ListViewSubItem permanently. Plan ahead.

 

Try The Code

 

Run your project now. Click the AddListViewItemsButton button, then the AddListViewSubItemsButton button. Next click the ListViewItemAppearchButton, then the ListViewSubItemAppearanceButton to see the code in action.

 

 

Learn more about the ListView by visiting the links below.

 

ListView

ListViewItem

ListViewSubItem

 

Copyright © 2001-2003 aZ Software Developers. All rights reserved.