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


After the list view is in your program, it’s up to you to add items, images, and select what kind of view you want. There are four view types:

  Icon mode—Can be manipulated with the mouse, allowing the user to drag and drop and rearrange objects.
  SmallIcon mode—Allows more ListItem objects to be viewed. Like the icon view, objects can be rearranged by the user.
  List mode—Presents a sorted view of the ListItem objects.
  Report mode—Presents a sorted view, with sub-items allowing extra information to be displayed.

We’ll set up the list view in the following topics in this chapter, creating the program listview, which is located on this book’s accompanying CD-ROM. This program shows how to use a list view control and has the following controls in it: an image list control, ImageList1, that holds the images we’ll use for the items in the list view; a list view control, ListView1 , with its ImageList property set to ImageList1; a combo box, Combo1; and a text box, Text1.

Running the program yields the result you see in Figure 16.15; we’ve added four items to the list view in that program, and users can select what type of view they want in the list view with the combo box. When the user clicks an item in the list view, the program reports which item was clicked in a text box at the bottom on the form. The code for this example is located in the listview folder on this book’s accompanying CD-ROM.


Figure 16.15  Using a list view in a program.

Adding Items To A List View

You add items to a list view’s ListItems collection, using its Add method. Each item you add is a ListItem object.

Let’s see how this works in an example. In this case, we’ll add three items to a list view, ListView1 . We start by declaring the first item, ListItem1 , as a ListItem object:

Private Sub Form_Load()
    Dim ListItem1 As ListItem
...

Next we add that item to the list view control with the ListItems collection’s Add method:

Private Sub Form_Load()
    Dim ListItem1 As ListItem
    Set ListItem1 = ListView1.ListItems.Add()
...

We can also give the new item some text to display in the list view:

Private Sub Form_Load()
    Dim ListItem1 As ListItem
    Set ListItem1 = ListView1.ListItems.Add()
    ListItem1.Text = "Item 1"
...

And we add the other two items in the same way:

Private Sub Form_Load()
    Dim ListItem1 As ListItem
    Set ListItem1 = ListView1.ListItems.Add()
    ListItem1.Text = "Item 1"

    Dim ListItem2 As ListItem
    Set ListItem2 = ListView1.ListItems.Add()
    ListItem2.Text = "Item 2"

    Dim ListItem3 As ListItem
    Set ListItem3 = ListView1.ListItems.Add()
    ListItem3.Text = "Item 3"

End Sub

We set the ListView1 control’s View property to lvwList (= 2) and run the program, yielding the result you see in Figure 16.16.


Figure 16.16  Adding items to a list view control.

That’s fine as far as it goes—but what about adding icons to list view items? We’ll take a look at that in the next topic.

Adding Icons To List View Items

The Aesthetic Design Department is on the phone. Your new list view control is fine, but what about adding icons to the items in that list view? Hmm, you think, how do you do that?

Each item in a list view is a ListItem object, and each such object has an Icon property. You set this property to an image’s index or key in an image list control.

Let’s see an example. We add a list view control, ListView1 , to a form, as well as an image list, ImageList1 . We add one image to the image list, new.bmp, which is in the Visual Basic common\graphics\bitmaps\offctlbr\large\color directory.

To connect the image list with the list view, right-click the list view at design time, and select the Properties item in the menu that appears. Click the Image Lists tab in the property pages, and select ImageList1 in the box labeled Normal, then click on OK to close the property pages.

Now we can add the image in the image list to the items in a list view, using their Icon property like this:

Private Sub Form_Load()

    Dim ListItem1 As ListItem
    Set ListItem1 = ListView1.ListItems.Add()
    ListItem1.Text = "Item 1"    ListItem1.Icon = 1

    Dim ListItem2 As ListItem
    Set ListItem2 = ListView1.ListItems.Add()
    ListItem2.Text = "Item 2"
    ListItem2.Icon = 1

    Dim ListItem3 As ListItem
    Set ListItem3 = ListView1.ListItems.Add()
    ListItem3.Text = "Item 3"
    ListItem3.Icon = 1

End Sub

Finally, we set the list view’s View property to lvwIcon (= 0) and run the program. The result appears in Figure 16.17.


Figure 16.17  Displaying icons in a list view control.

On the other hand, only the lvwIcon view uses icons this way—the other three list view control views use small icons. We’ll see how to add small icons in the next topic.

Adding Small Icons To List View Items

You usually use two icons for each item in a list view, a normal icon and a small icon. Let’s see how to add small icons now.

Each set of icons is stored in its own image list control, so we add a new image list control, ImageList2 , to a program now to hold small icons (we’ll use ImageList1 to store the large icons and the actual list view control will be ListView1). In this example, we’ll just place one image in ImageList2 —leaf.bmp from the Visual Basic common\graphics\bitmap\outline directory.

To connect the image list with the list view, right-click the list view at design time, and select the Properties item in the menu that appears. Click the Image Lists tab in the property pages, and select ImageList2 in the box labeled Small, then click on OK to close the property pages.

Now we can add the image we’ve stored as the small icon of all the list items:

Private Sub Form_Load()

    Dim ListItem1 As ListItem
    Set ListItem1 = ListView1.ListItems.Add()
    ListItem1.Text = "Item 1"
    ListItem1.Icon = 1
    ListItem1.SmallIcon = 1

    Dim ListItem2 As ListItem
    Set ListItem2 = ListView1.ListItems.Add()
    ListItem2.Text = "Item 2"
    ListItem2.Icon = 1
    ListItem2.SmallIcon = 1

    Dim ListItem3 As ListItem
    Set ListItem3 = ListView1.ListItems.Add()
    ListItem3.Text = "Item 3"
    ListItem3.Icon = 1
    ListItem3.SmallIcon = 1

End Sub


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.