Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Finally, set the list view’s View property to lvwSmallIcon (= 1) and run the program, as shown in Figure 16.18. You can see the icons we’ve selected for each item displayed in the list view in that figure. Our code is a success.


Figure 16.18  Using small icons in a list view.

Selecting The View Type In List Views

List view controls support four different views :

  lvwIcon—0; can be manipulated with the mouse, allowing the user to drag and drop and rearrange objects.
  lvwSmallIcon—1; allows more ListItem objects to be viewed. Like the icon view, objects can be rearranged by the user.
  lvwList—2; presents a sorted view of the ListItem objects.
  lvwReport—3; presents a sorted view, with sub-items, allowing extra information to be displayed.

You set the view type in a list view with its View property, which you can set at design time or runtime.

Let’s see an example. Here, we’ll display the various view types in a combo box, Combo1 , and when the user selects one of them, we’ll make that the current view type in the list view, ListView1.

When the form first loads, we place the view types in the combo box:

Private Sub Form_Load()

    With Combo1
        .AddItem "Icon View"
        .AddItem "Small Icon View"
        .AddItem "List View"
        .AddItem "Report View"
    End With

End Sub

Then when the user makes a selection in the combo box, we install the corresponding view in the list view:

Private Sub Combo1_Change()
    ListView1.View = Combo1.ListIndex
End Sub

Private Sub Combo1_Click()
    ListView1.View = Combo1.ListIndex
End Sub

The result appears in Figure 16.19. Although we can now select all four view types in a list view, note that we haven’t implemented the last type, the report view, which displays a list of columns. We’ll take a look at that starting with the next topic in this chapter.


Figure 16.19  Selecting view types in a list view control.

Adding Column Headers To A List View

List views can display lists arranged in columns when you set their View property to lvwReport . We’ll take a look at using the report view in this and the next topic. Here, we’ll see how to add multiple columns to a list view control.

To add columns to a list view, you just need to add column headers, and you do that with the list view’s ColumnHeaders collection. For example, here’s how we add four columns to a list view, giving each column the caption “Field 1”, “Field 2”, and so on:

Private Sub Form_Load()
    Dim colHeader As ColumnHeader
    Dim intLoopIndex As Integer

    For intLoopIndex = 1 To 4
        Set colHeader = ListView1.ColumnHeaders.Add()
        colHeader.Text = "Field " & intLoopIndex
    Next intLoopIndex

End Sub

This code works fine, but each column appears in a default width, which might not be right for the size of your list view. To tailor the columns to your list view control, you can do something like this, where we set the columns’ Width property:

Private Sub Form_Load()
    Dim colHeader As ColumnHeader
    Dim intLoopIndex As Integer

    For intLoopIndex = 1 To 4
        Set colHeader = ListView1.ColumnHeaders.Add()
        colHeader.Text = "Field " & intLoopIndex
        colHeader.Width = ListView1.Width / 4
    Next intLoopIndex

End Sub

After you set the View property of the list view control to lvwReport, the result of this code appears in Figure 16.20 (where we’ve added a few items to the list view control itself, Items 1 through 3, as well).


Figure 16.20  Supporting column headers in a list view.

Now that we’re using columns in a list view, how do you add text for each column, item by item? We’ll look into that next.

Adding Column Fields To A List View

You’ve set up a list view and added the items you want to it. Now you want to set the list view up to use columns by setting its View property to lvwReport. You’ve added headers to each column (see the previous topic in this chapter)—but how do you add text for each item in each column?

You use the ListSubItems collection’s Add method to add column text to an item. Each ListItem object has a ListSubItems collection, and here’s how you use that collection’s Add method:

ListSubItems.Add [ index ] [, key ] [, text ] [, reporticon ] [, tooltiptext ]

For example, let’s say that we add three items to a list view that has four columns. We can add text in each of the columns for each of the three items.

Here’s how it works. The first column, or field , holds the item’s text (set with its Text property). To add text for the following three columns of the first item (we’ll display “Field 2” in field 2, “Field 3” in field 3, and so on), we use the ListSubItems collection’s Add method this way:

Private Sub Form_Load()

    Dim colHeader As ColumnHeader
    Dim intLoopIndex As Integer

    For intLoopIndex = 1 To 4        'Label headers
        Set colHeader = ListView1.ColumnHeaders.Add()
        colHeader.Text = "Field " & intLoopIndex
        colHeader.Width = ListView1.Width / 4
    Next intLoopIndex

    Dim ListItem1 As ListItem
    Set ListItem1 = ListView1.ListItems.Add()
    ListItem1.Text = "Item 1"
    ListItem1.Icon = 1
    ListItem1.SmallIcon = 1
    ListView1.ListItems(1).ListSubItems.Add , , "Field 2"
    ListView1.ListItems(1).ListSubItems.Add , , "Field 3"
    ListView1.ListItems(1).ListSubItems.Add , , "Field 4"
...


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.