You are on page 1of 23

.

NET Controls: The List View


Overview of the List View
Introduction
A list box is used to display a list of strings and all items of that control are primarily strings. To
go a little further than a simple list of strings, the Microsoft Windows operating system provides
the list view control. A list view is used to display a list of items to the user but it can be
configured to change the type of display.

The Items of a List View


Introduction to Creating List View Items

To create the items of a list view, you can use the ListViewItem Collection Editor of Microsoft Visual
Studio .NET, or using coding. To get it, after adding the ListView to your application, you can click
the ellipsis button of its Items field in the Properties window:

At design time and in the ListViewItem Collection Editor, to create a new item, you can click the Add
button:

.NET Controls: The List View Page 1 of 23


Practical Learning 1: using design-wizard

The items of a list view are stored in a property called Items. The Items property is based on the
ListView.ListViewItemCollection class. To create a new list view item, the
ListViewItemCollection class is equipped with the Add() method which is overloaded with three
versions. One of the versions of this method uses the following syntax:

.NET Controls: The List View Page 2 of 23


Overloads Public Overridable Function Add(ByVal text As String) As ListViewItem

This method expects a string that will display as the new item. Here is an example:

Private Sub btnCreate_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles btnCreate.Click

lvwCountries.Items.Add("Egypt")

End Sub

As the Items property is in fact a list, each item of this collection is represented by the Item
property of the ListView.ListViewItemCollection class. This Item property is based on the
ListViewItem class. The ListViewItem class is equipped with various constructors, the default of
which allows you to instantiate an item without giving much details.

Instead of directly passing a string to the ListView.ListViewItemCollection.Add() method, you


can first create a ListViewItem object and pass it to the following version of the
ListView.ListViewItemCollection.Add() method:

Overloads Public Overridable Function Add(ByVal value As ListViewItem) As ListViewItem

This method expects a ListViewItem value. One way you can use it consists of providing the string
the item would display. To do this, you can use the following constructor of the ListViewItem class:

Private Sub btnCreate_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles btnCreate.Click

Dim lviPortugal As ListViewItem =lvwCountries.Items.Add("Portugal")

End Sub

.NET Controls: The List View Page 3 of 23


Practical Learning 2: using code

List View Items and Their Icons

One of the fundamental differences between a list box and a list view is that this one has a
built-in capability to display icons. Unlike the tree view, the list view uses two sets of pictures.
This means that two icons would be associated to each item. One of the icons should have a
32x32 pixels size and the other should have a 16x16 pixels size. The set of 32x32 pixels list is
referred to as large icons. The other set is referred to as small icons. Before using the pictures,
you should first store them in image lists. Each set must be stored in its own ImageList
object.

To support the various sets of icons, the ListView class is equipped with a property called
LargeImageList for the 32x32 icons and another property called SmallImageList for the
16x16 icons. After creating both ImageList objects, you can assign each to the appropriate
property.

When creating an item using the ListView.ListViewItemCollection.Add() method, if you


plan to display an icon next to it, you can use the following version of the method:

Overloads Public Overridable Function Add(ByVal text As String, _


ByVal imageIndex As Integer) As ListViewItem

.NET Controls: The List View Page 4 of 23


The first argument is the string that will display for the item. The second argument is the
index of the icon in the ImageList property. This method returns a reference to the
ListViewItem object that was added to the control.

We saw that you can also first create a ListViewItem object to add to the control. If you use
this approach and you want the item to display an icon, you can use the following constructor
of the TreeViewItem class:

Public Sub New(ByVal text As String, ByVal imageIndex As Integer)

This constructor uses the same arguments as the version of the


ListView.ListViewItemCollection.Add() method above. You can also create an array of
strings and assign them the same icon. To do this, you can create the item with the following
constructor of the TreeViewItem class:

Overloads Public Overridable Function Add(ByVal text As String, _


ByVal imageIndex As Integer) As ListViewItem

Practical Learning: Associating Icons With Nodes

1. Access the NewStoreItem form and change its design as follows:

Control Name Text Other Properties


Label Category:
Modifiers: Public
ComboBox cboCategories Miscellaneous Items: Babies, Teens,
Women, Men, Miscellaneous

2. Display the first form and click the list view on it


3. In the Properties window, change the following properties:
LargeImageList: imgLarge
SmallImageList: imgSmall
4. Double-click the New Item button and change its Click event as follows:

Private Sub btnNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Dim frmItem As NewStoreItem = New NewStoreItem
Dim icoSelected As Integer = 4
Dim strCatSelected As String = "Miscellaneous"

Dim rndNumber As Random = New Random(DateTime.Now.Millisecond)


Dim number1 As Integer = rndNumber.Next(100, 999)

.NET Controls: The List View Page 5 of 23


Dim number2 As Integer = rndNumber.Next(100, 999)
Dim itemNumber As String = CStr(number1) & "-" & CStr(number2)

frmItem.txtItemNumber.Text = itemNumber
If frmItem.ShowDialog() = DialogResult.OK Then
strCatSelected = frmItem.cboCategories.Text
End If

If strCatSelected = "Babies" Then


icoSelected = 0
ElseIf strCatSelected = "Teens" Then
icoSelected = 1
ElseIf strCatSelected = "Women" Then
icoSelected = 2
ElseIf strCatSelected = "Men" Then
icoSelected = 3
End If

Dim lviStoreItem As ListViewItem = New ListViewItem(frmItem.txtItemNumber.Text,


Me.lvwStoreItems.Items.Add(lviStoreItem)

End Sub

5. Execute the application to test it


6. Try creating an item for each category (use the random numbers generated by the
application)

7. Close the form and return to your programming environment

View Styles

To set it apart from the list box, a list view provides various options of displaying its items. To
support this, the ListView class is equipped with the View property that is based on the View
enumerator. Three of its members are:

 LargeIcon: In this view, the control displays a list of items using icons with a 32x32
pixels size of icons. The string of the item displays under its corresponding icon:

.NET Controls: The List View Page 6 of 23


 List: Each item appears with a small icon to its left. The first item appears to the left side
of the view. The next item (usually in alphabetical order) appears under it, and so on. If
there are more items to fit in one column, the list continues with a new column to the right
of the previous one. This continues until the list is complete:

 SmallIcon: Like the List option, this view uses small icons to display its items. The icon
of an item appears to the left of its string. The first item appears in the top-left section of
the view. The next item is positioned to the right of the previous item. The list continues to
the right. If there are more items, the subsequent ones display on the next line, until the
list is complete

.NET Controls: The List View Page 7 of 23


As seen so far, you can use one of four different displays on a list view. Furthermore, you can
give the user the ability to change views as needed. The different displays of the list view are
controlled by the View property of the ListView class. To specify the type of view to use,
assign the desired member of the View enumerator to the ListView.View property.

Practical Learning: Using View Styles

1. To create an icon, on the main menu, click Project -> Add New Item...
2. In the Add New Item dialog box, click Icon File
3. Set the Name to LrgIcon.ico and press Enter
4. On the main menu, click Image -> New Item Type...
5. In the New Icon Image Type dialog box, make sure 16x16, 16 colors is selected and click OK
6. Design the icon as follows:

7. Right-click the white area -> Current Icon Image Types -> 32x32, 16 Colors
8. Right-click the white area and click Delete Image Type
9. To save the file, on the main menu, click File -> Save LrgIcon.ico
10. To create another icon, on the main menu, click Project -> Add New Item...
11. In the Add New Item dialog box, click Icon File
12. Set the Name to SmIcon.ico and press Enter
13. On the main menu, click Image -> New Item Type...
14. In the New Icon Image Type dialog box, make sure 16x16, 16 colors is selected and click OK
15. Design the icon as follows:

.NET Controls: The List View Page 8 of 23


16. Right-click the white area -> Current Icon Image Types -> 32x32, 16 Colors
17. Right-click the white area and click Delete Image Type
18. To save the file, on the main menu, click File -> Save LrgIcon.ico
19. To create another icon, on the main menu, click Project -> Add New Item...
20. In the Add New Item dialog box, click Icon File
21. Set the Name to List.ico and press Enter
22. On the main menu, click Image -> New Item Type...
23. In the New Icon Image Type dialog box, make sure 16x16, 16 colors is selected and click OK
24. Design the icon as follows:

25. Right-click the white area -> Current Icon Image Types -> 32x32, 16 Colors
26. Right-click the white area and click Delete Image Type
27. To save the file, on the main menu, click File -> Save LrgIcon.ico
28. To create another icon, on the main menu, click Project -> Add New Item...
29. In the Add New Item dialog box, click Icon File
30. Set the Name to Details.ico and press Enter
31. On the main menu, click Image -> New Item Type...
32. In the New Icon Image Type dialog box, make sure 16x16, 16 colors is selected and click OK
33. Design the icon as follows:

34. Right-click the white area . Current Icon Image Types . 32x32, 16 Colors
35. Right-click the white area and click Delete Image Type
36. Save all

.NET Controls: The List View Page 9 of 23


37. Change the design of the first form as follows:

Control Name Appearance Checked Image


RadioButton btnLargeIcons Button LrgIcons.ico
RadioButton btnSmallIcons Button SmIcons.ico
RadioButton btnList Button List.ico
RadioButton btnDetails Button True Details.ico

38. Double-click each of the new buttons from left to right and implement their Click events as
follows:

Private Sub btnLargeIcons_CheckedChanged(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles btnLargeIcons.CheckedChanged
lvwStoreItems.View = View.LargeIcon
End Sub

Private Sub btnSmallIcons_CheckedChanged(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles btnSmallIcons.CheckedChanged
lvwStoreItems.View = View.SmallIcon
End Sub

Private Sub btnList_CheckedChanged(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles btnList.CheckedChanged
lvwStoreItems.View = View.List
End Sub

Private Sub btnDetails_CheckedChanged(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles btnDetails.CheckedChanged
lvwStoreItems.View = View.Details
End Sub

39. Return to the first form. Under the form, click contextMenu1
40. On the form, click Context Menu and click Type Here under the other menu items

.NET Controls: The List View Page 10 of 23


41. Type View and click Type Here on its right
42. Create the sub-menu items and select the events in the Events section of the Properties
window as follows:

Menu Text (Name) Event . Click


Large Icons mnuLargeIcons btnLargeIcons_CheckedChanged
Small Icons mnuSmallIcons btnSmallIcons_CheckedChanged
List mnuList btnList_CheckedChanged
Details mnuDetails btnDetails_CheckedChanged

43. Save all

The Columns of a List View


Introduction

Another characteristic that sets the list view apart from the list box is that the former can
provide more information about each item of its list. Based on this, each type of item we have
created so far can be equipped with its own list of sub-items. The view would appear as
follows:

.NET Controls: The List View Page 11 of 23


Creating Columns
Before creating the sub-items of a list view, you may need to plan it first to identify the types
of information you want to provide. To guide the user with the type of information that each
item would display, you can create a column for each type. To support columns, the ListView
class is equipped with the Columns property. The Columns property is an object of type
ListView.ColumnHeaderCollection. As its name indicates, the Columns property
represents a list of columns. Each column is based on the ColumnHeader class.

To create a column, you can suing Columns property of Listview

Practical Learning: Creating Columns

The Sub Items of an Item


Introduction
The idea of having columns is to provide more information about each item of a list view
instead of a simple string for each. Consider the following example:

Private Sub btnCreate_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles btnCreate.Click

.NET Controls: The List View Page 12 of 23


lvwCountries.Items.Add("Egypt")

Dim lviPortugal As ListViewItem = New ListViewItem("Portugal")


lvwCountries.Items.Add(lviPortugal)

Dim lviCountry As ListViewItem = New ListViewItem("Australia")


lvwCountries.Items.Add(lviCountry)
lviCountry = New ListViewItem("Mali")
lvwCountries.Items.Add(lviCountry)
lviCountry = New ListViewItem("Sweden")
lvwCountries.Items.Add(lviCountry)
End Sub

To support sub-items, the ListViewItem class is equipped with a property called SubItems.
This property is of type ListViewItem.ListViewSubItemCollection. To create a sub-item,
you can directly specify its text by passing a string to the ListViewSubItemCollection.Add()
method. The ListViewSubItemCollection.Add() method is overloaded with three versions.
The version referred to in this case uses the following syntax:

Overloads Public Function Add(ByVal text As String) As ListViewSubItem

To identify each piece of information concerning a sub-item, the ListViewSubItemCollection


class is equipped with a property called Item, which in turn is based on the
ListViewSubItem class. As you can see, this method returns a ListViewSubItem value.

Here are two examples of calling the above Add() method:

Private Sub btnCreate_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles btnCreate.Click

Dim lviEgypt As ListViewItem = lvwCountries.Items.Add("Egypt")


lviEgypt.SubItems.Add("1,001,450")
lviEgypt.SubItems.Add("74,718,797")
lviEgypt.SubItems.Add("Cairo")
lviEgypt.SubItems.Add("eg")

Dim lviPortugal As ListViewItem = New ListViewItem("Portugal")


lviPortugal.SubItems.Add("92,391")
lviPortugal.SubItems.Add("10,102,022")
lviPortugal.SubItems.Add("Lisbon")
lviPortugal.SubItems.Add("pt")
lvwCountries.Items.Add(lviPortugal)

Dim lviCountry As ListViewItem = New ListViewItem("Australia")

.NET Controls: The List View Page 13 of 23


lvwCountries.Items.Add(lviCountry)

lviCountry = New ListViewItem("Mali")


lvwCountries.Items.Add(lviCountry)

lviCountry = New ListViewItem("Sweden")


lvwCountries.Items.Add(lviCountry)
End Sub

As mentioned above, each sub-item is of type ListViewSubItem. The ListViewSubItem


class is equipped with three constructors. The default allows you to create an empty sub-item.
After declaring a sub-item, you can specify its text by assigning the desired string to the
ListViewSubItem.Text property. Instead of directly passing the text of a sub-item to the
ListViewSubItemCollection.Add() method as done above, you can first define a
ListViewSubItem object using the following constructor of the ListViewSubItem class:

Public Sub New(ByVal owner As ListViewItem, ByVal text As String)

The first argument of this constructor specifies the ListViewItem object that this sub-item
will belong to. The second argument is simply the string that this sub-item will display. After
defining a ListViewSubItem object, you can pass it to the following version of the
ListViewSubItemCollection.Add() method:

Overloads Public Function Add(ByVal item As ListViewItem.ListViewSubItem) As ListViewSubItem

Here are three examples of using it:


Private Sub btnCreate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCreate.Click

Dim lviEgypt As ListViewItem = lvwCountries.Items.Add("Egypt")


lviEgypt.SubItems.Add("1,001,450")
lviEgypt.SubItems.Add("74,718,797")
lviEgypt.SubItems.Add("Cairo")
lviEgypt.SubItems.Add("eg")

Dim lviPortugal As ListViewItem = New ListViewItem("Portugal")


lviPortugal.SubItems.Add("92,391")
lviPortugal.SubItems.Add("10,102,022")
lviPortugal.SubItems.Add("Lisbon")
lviPortugal.SubItems.Add("pt")
lvwCountries.Items.Add(lviPortugal)

Dim lviAustralia As ListViewItem = New ListViewItem("Australia")


Dim subAustralia As ListViewItem.ListViewSubItem = New ListViewItem.ListVie
lviAustralia.SubItems.Add(subAustralia)

.NET Controls: The List View Page 14 of 23


subAustralia = New ListViewItem.ListViewSubItem(lviAustralia, "19,731,984")
lviAustralia.SubItems.Add(subAustralia)
subAustralia = New ListViewItem.ListViewSubItem(lviAustralia, "Canberra")
lviAustralia.SubItems.Add(subAustralia)
subAustralia = New ListViewItem.ListViewSubItem(lviAustralia, "au")
lviAustralia.SubItems.Add(subAustralia)
lvwCountries.Items.Add(lviAustralia)

Dim lviMali As ListViewItem = New ListViewItem("Mali")


Dim subMali As ListViewItem.ListViewSubItem = New ListViewItem.ListViewSubItem(lviM
lviMali.SubItems.Add(subMali)
subMali = New ListViewItem.ListViewSubItem(lviMali, "11,626219")
lviMali.SubItems.Add(subMali)
subMali = New ListViewItem.ListViewSubItem(lviMali, "Bamako")
lviMali.SubItems.Add(subMali)
subMali = New ListViewItem.ListViewSubItem(lviMali, "ml")
lviMali.SubItems.Add(subMali)
lvwCountries.Items.Add(lviMali)

Dim lviSweden As ListViewItem = New ListViewItem("Sweden")


Dim subSweden As ListViewItem.ListViewSubItem = New ListViewItem.ListViewSubItem(lv
lviSweden.SubItems.Add(subSweden)
subSweden = New ListViewItem.ListViewSubItem(lviSweden, "8,878,085")
lviSweden.SubItems.Add(subSweden)
subSweden = New ListViewItem.ListViewSubItem(lviSweden, "Stockholm")
lviSweden.SubItems.Add(subSweden)
subSweden = New ListViewItem.ListViewSubItem(lviSweden, "se")
lviSweden.SubItems.Add(subSweden)
lvwCountries.Items.Add(lviSweden)
End Sub

If you call the ListViewItem.ListViewSubItemCollection.Add() method to create a sub-item,


the new one would be added to end of the list. If you want, you can insert the new sub-
somewhere inside the collection. To do this, you would call the
ListViewItem.ListViewSubItemCollection.Insert() method. Its syntax is:

Public Sub Insert(ByVal index As Integer, ByVal item As ListViewItem.ListViewSubItem)

The first argument is the index that the new sub-item will occupy after being inserted. The second
argument is the sub-item to create.
Practical Learning: Creating Sub-Items

1. Display the New Store Item form and change its design as follows:

.NET Controls: The List View Page 15 of 23


Control Name Text Other Properties
Label Category:
Modifiers: Public
ComboBox cboCategories Miscellaneous Items: Babies, Teens,
Women, Men, Miscellaneous
Label Item Name:
TextBox txtItemName
Label Item Size:
TextBox txtItemSize
Label Quantity:
TextBox txtQuantity
Label Unit Price:
TextBox txtUnitPrice TextAlign: Right
Label Item #:

2. Display the first form and double-click its New Item button
3. Change its Click event as follows:

Private Sub btnNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Dim frmItem As NewStoreItem = New NewStoreItem
Dim icoSelected As Integer = 4

Dim rndNumber As Random = New Random(DateTime.Now.Millisecond)


Dim number1 As Integer = rndNumber.Next(100, 999)
Dim number2 As Integer = rndNumber.Next(100, 999)
Dim itemNumber As String = CStr(number1) & "-" & CStr(number2)

frmItem.txtItemNumber.Text = itemNumber
If frmItem.ShowDialog() = DialogResult.OK Then
Dim strCatSelected As String = frmItem.cboCategories.Text
Dim strItemName As String = frmItem.txtItemName.Text
Dim strItemSize As String = frmItem.txtItemSize.Text
Dim strQuantity As String = frmItem.txtQuantity.Text
Dim strUnitPrice As String = frmItem.txtUnitPrice.Text
Dim strItemNumber As String = frmItem.txtItemNumber.Text

' If the user didn't provide a name for the item,


' don't do anything

.NET Controls: The List View Page 16 of 23


If strItemName = "" Then Exit Sub

' Make sure the user entered a unit price


If strItemNumber = "" Then Exit Sub

If strCatSelected = "Babies" Then


icoSelected = 0
ElseIf strCatSelected = "Teens" Then
icoSelected = 1
ElseIf strCatSelected = "Women" Then
icoSelected = 2
ElseIf strCatSelected = "Men" Then
icoSelected = 3
End If

Dim lviStoreItem As ListViewItem = New ListViewItem(strItemNumber, icoSelect


lviStoreItem.SubItems.Add(strCatSelected)
lviStoreItem.SubItems.Add(strItemName)
lviStoreItem.SubItems.Add(strItemSize)
lviStoreItem.SubItems.Add(strUnitPrice)
lviStoreItem.SubItems.Add(strQuantity)
lvwStoreItems.Items.Add(lviStoreItem)
End If
End Sub

4. Execute the application and create a few items as follows (let the computer generate store
numbers):

Unit
Category Item Name Size Qty
Price
Women Cashmere Lined Glove 8 115.95 12
Miscellaneous Chocolate Gift Box Medium 45.00 5
Men Trendy Jacket Medium 45.85 8
Women Stretch Flare Jeans Petite 27.75 6
Women Belted Sweater L 15.95 10
Teens Girls Classy Handbag One Size 95.95 4
Women Casual Dress Shoes 9.5M 45.95 16
Babies Infant Girls Ballerina Dress 12M 22.85 14
Teens Girls Velour Dress 10 12.55 8
Women Lace Desire Panty M 7.15 22
Teens Boys Hooded Sweatshirt M (7/8) 15.95 6
Men Classic Pinstripe Suit 38 145.95 8

.NET Controls: The List View Page 17 of 23


5. Close the form and return to your programming environment

Managing Sub Items


When you create a new sub-item, it uses a default font and a black color on a white
background. If you want, you can change the way a sub-item aesthetically displays. To allow
these changes, the ListViewItem class is equipped with the UseItemStyleForSubItems
Boolean property, whose default value is true. When this property is set to true, the compiler
refers to the item that "owns" the current sub-item to paint the sub-item, as we will see in the
next section. If you plan to change these aspects, you must first set this property to false.

After setting the ListViewItem.UseItemStyleForSubItems property to false, you can set


the following properties of the ListViewSubItem class as you wish:

 Font: This allows you to apply a font of your choice for a particular sub-item. This would be
done as follows:

Private Sub btnCreate_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles btnCreate.Click
Dim lvwCountries As ListView = New ListView

. . .

Dim lviSweden As ListViewItem = New ListViewItem("Sweden")


lviSweden.UseItemStyleForSubItems = False
Dim subSweden As ListViewItem.ListViewSubItem = _
New ListViewItem.ListViewSubItem(lviSweden, "449,964")
lviSweden.SubItems.Add(subSweden)
subSweden = New ListViewItem.ListViewSubItem(lviSweden, "8,878,085")
lviSweden.SubItems.Add(subSweden)
subSweden = New ListViewItem.ListViewSubItem(lviSweden, "Stockholm")
subSweden.Font = New Drawing.Font("Times New Roman", 14, FontStyle.Italic)
lviSweden.SubItems.Add(subSweden)
subSweden = New ListViewItem.ListViewSubItem(lviSweden, "se")
lviSweden.SubItems.Add(subSweden)
lvwCountries.Items.Add(lviSweden)

.NET Controls: The List View Page 18 of 23


End Sub

 ForeColor: Instead writing a sub-item's text in black, you can assign the desired color to
this property to use a different color. This would be done as follows:

Private Sub btnCreate_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles btnCreate.Click

. . .

Dim lviSweden As ListViewItem = New ListViewItem("Sweden")


lviSweden.UseItemStyleForSubItems = False
Dim subSweden As ListViewItem.ListViewSubItem = _
New ListViewItem.ListViewSubItem(lviSweden, "449,964")
subSweden.ForeColor = Color.Red
lviSweden.SubItems.Add(subSweden)
subSweden = New ListViewItem.ListViewSubItem(lviSweden, "8,878,085")
lviSweden.SubItems.Add(subSweden)
subSweden = New ListViewItem.ListViewSubItem(lviSweden, "Stockholm")
subSweden.Font = New Drawing.Font("Times New Roman", 14, FontStyle.Italic)
lviSweden.SubItems.Add(subSweden)
subSweden = New ListViewItem.ListViewSubItem(lviSweden, "se")
lviSweden.SubItems.Add(subSweden)
lvwCountries.Items.Add(lviSweden)
End Sub

 BackColor: This property allows an individual sub-item to display on top of a specify color.
This would be done as follows:

Private Sub btnCreate_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles btnCreate.Click

.NET Controls: The List View Page 19 of 23


. . .

Dim lviSweden As ListViewItem = New ListViewItem("Sweden")


lviSweden.UseItemStyleForSubItems = False
Dim subSweden As ListViewItem.ListViewSubItem = _
New ListViewItem.ListViewSubItem(lviSweden, "449,964")
subSweden.ForeColor = Color.Red
lviSweden.SubItems.Add(subSweden)
subSweden = New ListViewItem.ListViewSubItem(lviSweden, "8,878,085")
subSweden.BackColor = Color.Blue
lviSweden.SubItems.Add(subSweden)
subSweden = New ListViewItem.ListViewSubItem(lviSweden, "Stockholm")
subSweden.Font = New Drawing.Font("Times New Roman", 14, FontStyle.Italic)
lviSweden.SubItems.Add(subSweden)
subSweden = New ListViewItem.ListViewSubItem(lviSweden, "se")
lviSweden.SubItems.Add(subSweden)
lvwCountries.Items.Add(lviSweden)
End Sub

To restore these settings on the sub-item, you can call the


ListViewItem.ListViewSubItem.ResetStyle() method. Its syntax is:

Public Sub ResetStyle()

When called, this method resets the font, the text color, and the background color.

Managing Items of a List View


Deleting Items
To delete an item from a list view, you can call the ListView.ListViewItemCollection.Remove
() method. Its syntax is:

Public Overridable Sub Remove(ByVal item As ListViewItem)

This method takes as argument the ListViewItem object to be deleted. If you are already
positioned at that item, you can call its own ListViewItem.Remove() method. Its syntax is:

Public Overridable Sub Remove()

To delete an item based on its index, you can call the ListViewItemCollection.RemoveAt()
method whose syntax is:

.NET Controls: The List View Page 20 of 23


Public Overridable Sub RemoveAt(ByVal index As Integer) Implements IList.RemoveAt

When calling this method, you must pass the index of the undesired item. If the item is found, it
would be deleted. If you provide a negative index or one that is higher than the
ListViewItemCollection.Count property, the compiler would throw an
ArgumentOutOfRangeException exception.

To delete all items from a list view, you can call the ListViewItemCollection.Clear() method.
Its syntax is:

Public Overridable Sub Clear() Implements IList.Clear

When called, this method removes all items of the list view.

Selecting Items in a List View


To select an item in the list, the user can click it. The selected item indicates this by being
highlighted. To select another item, the user can click it and this automatically dismisses the
previous selection. If you want, you can give the user the ability to select more than one item
or you can prevent the user from selecting more than one item. This characteristic is
controlled by the MultiSelect property of the ListView class. Its default value is true, which
allows the user to select one or more items. If you set it to false, the user can select only one
item at a time.

You can also allow the user to select an item by positioning the mouse over it. This
characteristic is controlled by the HoverSelection property of the ListView class.

When an item has been selected or more than one item are selected, the selected items are
stored in a list represented by the SelectedItems property of the ListView class. The
ListView.SelectedItems property is an object based on the
ListView.SelectedListViewItemCollection class. If the ListView.MultiSelect property is
set to false, this collection would contain only one item.

The number of items selected in the control is known as the Count property of the
SelectedListViewItemCollection class. Each item selected can be identified through the
Item indexed property of the SelectedListViewItemCollection class.

The SelectedListViewItemCollection class holds a list of the objects that are selected and
each is identified as a ListViewItem. If you are more interested in the positions of the items
selected and not necessarily their references, you can use the SelectedIndices property of
the ListView class. Each item selected has its index stored in this list. The
ListView.SelectedIndices property is based on the ListView.SelectedIndexCollection
class.

After selecting an item, if the user clicks another control, the item that was selected would not
be highlighted anymore. If you want the control to continue showing the current selection
even when the list view loses focus, set the value of the HideSelection Boolean property of
the ListView class accordingly.

Practical Learning: Managing List View Items

1. In the Class Name, select lvwStoreItems


2. In the Method Name, select DoubleClick and implement the event as follows:

.NET Controls: The List View Page 21 of 23


Private Sub lvwStoreItems_DoubleClick(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles lvwStoreItems.DoubleClick
If lvwStoreItems.SelectedItems.Count = 0 Or _
lvwStoreItems.SelectedItems.Count > 1 Then Exit Sub

Dim lviCurrent As ListViewItem = lvwStoreItems.SelectedItems.Item(0)

Dim frmItem As NewStoreItem = New NewStoreItem

frmItem.txtItemNumber.Text = lviCurrent.Text
frmItem.cboCategories.Text = lviCurrent.SubItems.Item(1).Text
frmItem.txtItemName.Text = lviCurrent.SubItems.Item(2).Text
frmItem.txtItemSize.Text = lviCurrent.SubItems.Item(3).Text
frmItem.txtUnitPrice.Text = lviCurrent.SubItems.Item(4).Text
frmItem.txtQuantity.Text = lviCurrent.SubItems.Item(5).Text

If frmItem.ShowDialog() = DialogResult.OK Then


lvwStoreItems.SelectedItems.Item(0).Text = frmItem.txtItemNumber.Text
lvwStoreItems.SelectedItems.Item(0).SubItems.Item(1).Text = frmItem.cboCateg
lvwStoreItems.SelectedItems.Item(0).SubItems.Item(2).Text = frmItem.txtItemN
lvwStoreItems.SelectedItems.Item(0).SubItems.Item(3).Text = frmItem.txtItemS
lvwStoreItems.SelectedItems.Item(0).SubItems.Item(4).Text = frmItem.txtUnitP
lvwStoreItems.SelectedItems.Item(0).SubItems.Item(5).Text = frmItem.txtQuant
End If
End Sub

3. In the Class name combo box, select mnuDelItem


4. In the Method Name combo box, select Click and implement its event as follows:

Private Sub mnuDelItem_Click(ByVal sender As Object, _


ByVal e As System.EventArgs) Handles mnuDelItem.Click
If lvwStoreItems.SelectedItems.Count = 0 Then Exit Sub

Dim answer As System.Windows.Forms.DialogResult


answer = MsgBox("Are you sure you want to remove this item from the inventory?",
MsgBoxStyle.YesNo, "Deletion Warning")
If answer = DialogResult.Yes Then
lvwStoreItems.SelectedItems.Item(0).Remove()
End If
End Sub

5. In the Class name combo box, select mnuDeleteAll


6. In the Method Name combo box, select Click and implement its event as follows:

Private Sub mnuDeleteAll_Click(ByVal sender As Object, _


ByVal e As System.EventArgs) Handles mnuDeleteAll.Click
lvwStoreItems.Items.Clear()
End Sub

7. Save all

Full Row Selection

By default, to select an item, the user must click the item itself and not one of its sub-items. If
you want an item and its sub-items to be selected when the user clicks anything on their line,
you can change the value of the ListView.FullRowSelect Boolean property. Its default value
is set to false, which obliges the user to click the item itself. If you set this property to true,

.NET Controls: The List View Page 22 of 23


the whole row would be highlighted when either you or the user selects it.

Practical Learning: Allowing Full Row Selection

1. To allow the user to select a whole when clicking an item, set the list view's FullRowSelect
property to True
2. Save all

Grid Lines
When using the detail view, to make a list view more indicative, you can underline each row.
This characteristic is controlled by the GridLines Boolean property of the ListView class. The
default value of this property is false. If you set it to true, horizontal grid lines would appear
among items throughout the list view, including empty rows:
Private Sub btnCreate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCreate.Click
Dim lvwCountries As ListView = New ListView
lvwCountries.Location = New Point(10, 40)
lvwCountries.Width = 420
lvwCountries.Height = 100
lvwCountries.View = View.Details
lvwCountries.GridLines = true

Controls.Add(lvwCountries)

. . .

End Sub

.NET Controls: The List View Page 23 of 23

You might also like