|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Finally, set the list views View property to lvwSmallIcon (= 1) and run the program, as shown in Figure 16.18. You can see the icons weve selected for each item displayed in the list view in that figure. Our code is a success.
Selecting The View Type In List ViewsList view controls support four different views :
You set the view type in a list view with its View property, which you can set at design time or runtime. Lets see an example. Here, well display the various view types in a combo box, Combo1 , and when the user selects one of them, well 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 havent implemented the last type, the report view, which displays a list of columns. Well take a look at that starting with the next topic in this chapter.
Adding Column Headers To A List ViewList views can display lists arranged in columns when you set their View property to lvwReport . Well take a look at using the report view in this and the next topic. Here, well 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 views ColumnHeaders collection. For example, heres 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 weve added a few items to the list view control itself, Items 1 through 3, as well).
Now that were using columns in a list view, how do you add text for each column, item by item? Well look into that next. Adding Column Fields To A List ViewYouve 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. Youve 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 collections Add method to add column text to an item. Each ListItem object has a ListSubItems collection, and heres how you use that collections Add method: ListSubItems.Add [ index ] [, key ] [, text ] [, reporticon ] [, tooltiptext ] For example, lets 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. Heres how it works. The first column, or field , holds the items text (set with its Text property). To add text for the following three columns of the first item (well display Field 2 in field 2, Field 3 in field 3, and so on), we use the ListSubItems collections 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"
...
|
|
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. |