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
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
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.
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) ThenMyLstView.Sorted = True
' new sort keyElse
' remove previous ColumnHeader icon
MyLstView.ColumnHeaders(MyLstView.SortKey + 1).Icon = 0
MyLstView.SortKey = ColumnHeader.Index - 1
MyLstView.SortOrder = lvwAscending
ColumnHeader.Icon = 1
' toggle the sort orderEnd If
If MyLstView.SortOrder = lvwAscending Then
MyLstView.SortOrder = lvwDescendingElse
ColumnHeader.Icon = 2
MyLstView.SortOrder = lvwAscendingEnd If
ColumnHeader.Icon = 1
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.End Sub
Static LastItem As ListItem
Item.ListSubItems(1).Bold = True
If Not LastItem Is Nothing ThenSet LastItem = Item
LastItem.ListSubItems(1).Bold = FalseEnd If
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.
BonusAdd 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.