|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
After the list view is in your program, its up to you to add items, images, and select what kind of view you want. There are four view types:
Well set up the list view in the following topics in this chapter, creating the program listview, which is located on this books 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 well 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; weve 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 books accompanying CD-ROM.
Adding Items To A List ViewYou add items to a list views ListItems collection, using its Add method. Each item you add is a ListItem object. Lets see how this works in an example. In this case, well 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 collections 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 controls View property to lvwList (= 2) and run the program, yielding the result you see in Figure 16.16.
Thats fine as far as it goesbut what about adding icons to list view items? Well take a look at that in the next topic. Adding Icons To List View ItemsThe 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 images index or key in an image list control. Lets 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 views View property to lvwIcon (= 0) and run the program. The result appears in Figure 16.17.
On the other hand, only the lvwIcon view uses icons this waythe other three list view control views use small icons. Well see how to add small icons in the next topic. Adding Small Icons To List View ItemsYou usually use two icons for each item in a list view, a normal icon and a small icon. Lets 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 (well use ImageList1 to store the large icons and the actual list view control will be ListView1). In this example, well 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 weve 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
|
|
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. |