Listview

This sample populates a Listview and shows the 4 different ways the data can be displayed.

The Listview control contains the ListItem and ColumnHeader objects. The ListItem object defines the various characteristics of items in the Listview control, such as:

ColumnHeaders can be added at both design and run time. At design time, you can use the Column Headers tab of the Listview Control Custom Properties dialog box. At run time, use the Add method to add a ColumnHeader object to the ColumnHeaders collection.
Syntax for the ColumnHeaders add method:
object. Add (index, key, text, width, alignment)
Example: MyLstView.ColumnHeaders.Add 1, , "Weather", 1500

***Found in the Help under Add Method (ColumnHeaders Collection)

You can only add items to a list view at Run-time. To Add items to the Listview you use the add method of the ListItem collection.
Syntax for the add method:
object. Add (index,key, text, icon, smallIcon)
Example: ListView.Listitems.Add 1, , "Cloudy", 1,1

***Found in the Help Under Add Method (ListItems Collection)

You can add subitems that will be displayed if the view is changed to ReportView
Syntax for adding subitems is:
object.Item(index).SubItems(index) [= string]
Example: ListView1.ListItems.Item(1).SubItems(1) = "Gloomy"

In Report view, you can hide the column headers by setting the HideColumnHeaders property to True. You can also use the ColumnClick event and the Sorted, SortOrder, and SortKey properties to sort the ListItem objects or subitems when a user clicks a column header. The user can change the size of the column by grabbing the right border of a column header and dragging it to the desired size.

To see the subitems you add to the Listview. It has to be in ReportView with at least 2 ColumnHeaders.


New For Visual Basic 6

Visual Basic 6 adds the ability to insert icons into the headings of columns and, in Report View, to highlight a corresponding item in another column within the list when an item in the primary column is selected.

The event you would use to add or change an icon in the column heading (Only in Report View) is the ListView1_ColumnClick event.

The actual line of code would be: ColumnHeader.Icon = 1

Where 1 is the index number of an icon in an image control. The code sample below shows an example of sorting the list box and adding an icon to the column heading

Private Sub myLstView_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
If (MyLstView.SortKey <> ColumnHeader.Index - 1) Or(MyLstView.Sorted = False) Then
' new sort key
' remove previous ColumnHeader icon
MyLstView.ColumnHeaders(MyLstView.SortKey + 1).Icon = 0
MyLstView.SortKey = ColumnHeader.Index - 1
MyLstView.SortOrder = lvwAscending
ColumnHeader.Icon = 1
Else
' toggle the sort order
If MyLstView.SortOrder = lvwAscending Then
MyLstView.SortOrder = lvwDescending
ColumnHeader.Icon = 2
Else
MyLstView.SortOrder = lvwAscending
ColumnHeader.Icon = 1
End If
End If
MyLstView.Sorted = True

End Sub

The sample code below shows one way of bolding the corresponding item in a secondary column when an item is selected in the primary column. (This only applies when the ListView control is in Report View)

Private Sub myLstView_ItemClick(ByVal Item AsMSComctlLib.ListItem)
' This is one way to accomplish this. There are others.
Static LastItem As ListItem
Item.ListSubItems(1).Bold = True
If Not LastItem Is Nothing Then
LastItem.ListSubItems(1).Bold = False
End If
Set LastItem = Item
End Sub
Sample Project

1. Start a new project.

2. Go to Project Components and add Microsoft Windows Common Controls 6.0 to your project.

3. Add a Listview, an ImageList, a frame and 4 option buttons to your form.

4. In form load add 2 column headers using the add method.

5. 5 items to the list view and 5 subitems.

6. Uses the 4 option buttons to change the view of the Listview.

Bonus

Add code to the sample project to, while in ReportView, sort the contents of the ListView in ascending order when the heading is clicked putting an up arrow in the heading and sorting in descending order when the heading is clicked again changing the icon in the heading to a down arrow.

Add code to bold the value in the second column when the user selects an item in the first column while in Report View


The above information was compiled from portions of the Vb5 and Vb6 Help files. The "New In VB6" and the "Bonus" sections were compiled by Buddy West. Copyright Microsoft.


[Home]   [Back to the top]


Hosted by www.Geocities.ws

1