|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Using Multiselect List BoxesEveryones very pleased with your new program to sell classical music CDsexcept for the Sales Department. Why, they want to know, can the user only buy one CD at a time? Well, you explain, the program uses a list box to display the list of CDs, and when the user makes a selection, the program orders that CD. They ask, How about using a multiselect list box? So whats that? A multiselect list box allows the user to select a number of items at one time. You make a list box into a multiselect list box with the MultiSelect property. The user can then select multiple items using the Shift and Ctrl keys. Here are the possible settings for MultiSelect:
Lets see an example of a multiselect list box at work. In this case, well have two list boxes, List1 and List2, as well as a command button displaying an arrow (here, well just give a button the caption > to display the arrow). Set List1s MultiSelect property to 1. When the user selects a number of items in List1 and clicks the button with an arrow, well copy the selected items in List1 to List2, as in Figure 8.4.
We start by loading items into List1 when the form loads:
Private Sub Form_Load ()
List1.AddItem ("Item 0")
List1.AddItem ("Item 1")
List1.AddItem ("Item 2")
List1.AddItem ("Item 3")
List1.AddItem ("Item 4")
List1.AddItem ("Item 5")
List1.AddItem ("Item 6")
List1.AddItem ("Item 7")
End Sub
Next, when the user clicks the command button to indicate he has made all the selections he wants, we loop over the list this way:
Private Sub Command1_Click ()
Dim intLoopIndex
For intLoopIndex = 0 To List1.ListCount - 1
...
Next intLoopIndex
End Sub
In the loop, we see which items were selected and move them to the other list box, List2:
Private Sub Command1_Click ()
Dim intLoopIndex
For intLoopIndex = 0 To List1.ListCount - 1
If List1.Selected(intLoopIndex) Then
List2.AddItem List1.List(intLoopIndex)
End If
Next intLoopIndex
End Sub
The result appears in Figure 8.4, where were letting the user make multiple selections using the mouse, Shift, and Ctrl keys. Note that we looped over every item in the list box to see if it was selected or notis this necessary? Arent there SelStart and SelLength properties for the list box as there are for text boxes? Those properties dont exist for list boxes, because the selected items in a multiselect list box may not be contiguous, which also means that we do indeed have to loop over all items in the list box, checking each one individually to see if its been selected. Making List Boxes Scroll HorizontallyIts a pity that theres so little vertical space for the list box in your new programs layoutthe user can only view 4 of the more than 40 items in the list box at once. Cant you make a list box work horizontally instead of vertically? Yes you can, if you break up the list into columns using the Columns property. When that property is set to 0, the default, the list box presents just a vertical list to the user. When you set the Columns property to another value, the list box displays its items in that number of columns instead. Lets see an examplecan multiselect list boxes also be multicolumn list boxes? They sure can; take a look at Figure 8.5.
In this example, weve just set List1s Columns property to 2 and used the same code we developed for our multiselect example, which transfers selected items from List1 to List2 when the user clicks the command button (if youve made List1 large, you might have to make it smaller before it will display the items in a number of columns rather than one large column):
Private Sub Command1_Click ()
Dim intLoopIndex
For intLoopIndex = 0 To List1.ListCount - 1
If List1.Selected(intLoopIndex) Then
List2.AddItem List1.List(intLoopIndex)
End If
Next intLoopIndex
End Sub
Now the user can select multiple items from the columns in List1 and transfer them to List2 at the click of a button. Using Checkmarks In A List BoxThe Aesthetic Design Department has sent you a memo. People are so tired, they write, of standard list boxes. Cant you punch them up a little in your program, SuperDuperTextPro? Suppressing your immediate response, which is to tell the Aesthetic Design Department just what you think of them in rather direct terms, you give the problem a little thought. Well, you decide, I could use those new checkmark list boxes. When you use checkmark list boxes, selected items appear with a checkmark in front of them. You can make a list box into a checkmark list box with its Style property, which can take these values:
For example, the list box in Figure 8.6 has its Style property set to 1, making it a checkmark list box.
|
|
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. |