261 264




Visual Basic 6 Black Book:List Boxes And Combo Boxes
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive 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 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




Using Multiselect List Boxes
Everyone’s very pleased with your new program to sell classical music CDs—except 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 what’s 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:

•  0—Multiple selection isn’t allowed (this is the default).
•  1—Simple multiple selection. A mouse click or pressing the spacebar selects or deselects an item in the list. (Arrow keys move the focus.)
•  2—Extended multiple selection. Pressing the Shift key and clicking the mouse or pressing the Shift key and one of the arrow keys extends the selection from the previously selected item to the current item. Pressing the Ctrl key and clicking the mouse selects or deselects an item in the list.


TIP:  The DblClick event isn’t very useful with multiselect list boxes, because when you click the list box a second time, every item but the one you’ve clicked is deselected. In addition, a Click event is generated each time the user selects a new item, and you might want to wait until all selections are made before taking action. This is why you often use a command button to initiate action after a user selects items in a multiselect list box. Take a look at the following example to see how this works.

Let’s see an example of a multiselect list box at work. In this case, we’ll have two list boxes, List1 and List2, as well as a command button displaying an arrow (here, we’ll just give a button the caption “—>” to display the arrow). Set List1’s MultiSelect property to 1. When the user selects a number of items in List1 and clicks the button with an arrow, we’ll copy the selected items in List1 to List2, as in Figure 8.4.

Figure 8.4  Selecting multiple items in a multiselect list box.
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 we’re 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 not—is this necessary? Aren’t there SelStart and SelLength properties for the list box as there are for text boxes? Those properties don’t 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 it’s been selected.
Making List Boxes Scroll Horizontally
It’s a pity that there’s so little vertical space for the list box in your new program’s layout—the user can only view 4 of the more than 40 items in the list box at once. Can’t 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.
Let’s see an example—can multiselect list boxes also be multicolumn list boxes? They sure can; take a look at Figure 8.5.

Figure 8.5  A multiselect multicolumn list box.
In this example, we’ve just set List1’s 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 you’ve 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 Box
The Aesthetic Design Department has sent you a memo. People are so tired, they write, of standard list boxes. Can’t 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:

•  0—Standard list box (the default)
•  1—Checkmark list box

For example, the list box in Figure 8.6 has its Style property set to 1, making it a checkmark list box.

Figure 8.6  Using checkmark list boxes.

TIP:  By default, checkmark list boxes can support multiple selections; the MultiSelect property of these list boxes must be set to 0.





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.



Wyszukiwarka

Podobne podstrony:
261 264
Glee S01E17 Bad Reputation 720p WEB DL DD5 1 h 264 LP(1)
14 (261)
18 (264)
Rozdzial (264)
260 261
255 264
08 (261)
MAKIJAŻ 261 ALEXANDRE HERCHCOVITCH
264 (2)

więcej podobnych podstron