Professional Documents
Culture Documents
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:
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:
This method expects a string that will display as the new item. Here is an example:
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.
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:
End Sub
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.
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:
frmItem.txtItemNumber.Text = itemNumber
If frmItem.ShowDialog() = DialogResult.OK Then
strCatSelected = frmItem.cboCategories.Text
End If
End Sub
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:
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
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:
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
38. Double-click each of the new buttons from left to right and implement their Click events as
follows:
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
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:
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:
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:
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:
2. Display the first form and double-click its New Item button
3. Change its Click event as follows:
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
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
Font: This allows you to apply a font of your choice for a particular sub-item. This would be
done as follows:
. . .
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:
. . .
BackColor: This property allows an individual sub-item to display on top of a specify color.
This would be done as follows:
When called, this method resets the font, the text color, and the background color.
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:
To delete an item based on its index, you can call the ListViewItemCollection.RemoveAt()
method whose syntax is:
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:
When called, this method removes all items of the list view.
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.
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
7. Save all
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,
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