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
Removing Items From A List Box
The Testing Department is calling againhow about letting the users customize your program? You ask, what do you mean? Well, they say, lets give the user some way of removing the 50 fine French cooking tips from the list box.
You can remove items from a list box at design time simply by deleting them in the List property. At runtime, you use the RemoveItem() method. Heres an example; in this case, we add four items, Items 0 through 3 to a list box:
Private Sub Form_Load()
List1.AddItem ("Item 0")
List1.AddItem ("Item 1")
List1.AddItem ("Item 2")
List1.AddItem ("Item 3")
End Sub
Item 0 has index 0 in the list box, Item 1 has index 1, and so on. To remove, say, Item 1 when the user clicks a command button, we can use RemoveItem and pass it the items index:
Private Sub Command1_Click()
List1.RemoveItem 1
End Sub
Running the program and clicking the button gives the result shown in Figure 8.3. Now were able to remove items from a list box.
Figure 8.3 Removing an item from a list box.
TIP: You should note that removing an item from a list box changes the indexes of the remaining items. After you remove Item 1 in the preceding example, Item 2 now gets index 1 and Item 3 gets index 2. If you want to change those indexes back to their original values, set the items Index properties.
Sorting A List Box
Youre very proud of your new programs list box, which lists all the classical music recordings available for the last 40 years. But the Testing Department isnt so happy. They ask, Cant you alphabetize that list?
You can alphabetize the items in a list box by setting its Sorted property to True (its False by default) at design time or runtime. Thats all it takes. (In fact, Ive known lazy programmers who sorted arrays of text by placing the text into a hidden list box and then read it back to save writing the code for the string comparisons!)
TIP: You should know, however, that sorting a list box can change the indexes of the items in that list box (unless they were already in alphabetical order). After the sorting is finished, the first item in the newly sorted list has index 0, the next index 1, and so on. If you want to change the indexes of the items back to their original values, you can set their Index properties.
Determining How Many Items Are In A List Box
You want to loop over the items in your list box to find out if a particular item is in the list, but you need to know how many items are in the list box in order to set up the loop. How can you set up the loop?
You can use the ListCount property to determine how many items are in a list box. When setting up loops over the items in a list box, you should note that ListCount is the total number of items in a list, whereas index values start at 0, not 1. This means that if youre looping over indices, you should loop to ListCount 1, not ListCount.
Lets see an example. Here, well search a list box to see if it has an item whose caption is Item 1. First, we set up the loop over the indexes of the items in the list box:
Private Sub Command1_Click()
Dim intLoopIndex As Integer
For intLoopIndex = 0 To List1.ListCount - 1
...
Next intLoopIndex
End Sub
Then we check the caption of each item, checking for the caption Item 1, and report if we find that item:
Private Sub Command1_Click()
Dim intLoopIndex As Integer
For intLoopIndex = 0 To List1.ListCount - 1
If List1.List(intLoopIndex) = "Item 1" Then
MsgBox "Found item 1!"
End If
Next intLoopIndex
End Sub
Determining If A List Box Item Is Selected
The big point of list boxes is to let the user make selections, of course, and there are a number of properties to handle that process. Heres an overview.
You get the index of the selected item in a list box with the ListIndex property. If no item is selected, ListIndex will be 1.
You can get the text of a lists selected item as List1.Text or List1.List(List1.ListIndex).
You can use a list boxs Selected array to determine if individual items in the list box are selected or not. Lets see an example to see how that works; in this case, well loop over the elements in the list box until we find the selected one.
We start by loading items into the list box 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
When the user clicks a command button, we can indicate which item is selected in the list box by displaying that items caption in a message box. We just loop over all the items in the list box:
Private Sub Command1_Click ()
Dim intLoopIndex
For intLoopIndex = 0 To List1.ListCount - 1
...
Next intLoopIndex
End Sub
And we check the Selected array for each item to find the selected item:
Private Sub Command1_Click ()
Dim intLoopIndex
For intLoopIndex = 0 To List1.ListCount - 1
If List1.Selected(intLoopIndex) Then
MsgBox "You selected " & List1.List(intLoopIndex)
End If
Next intLoopIndex
End Sub
Note that list boxes can support multiple selections if you set their MultiSelect property to True. See the next topic in this chapter to see how to handle selections in multiselect list boxes.
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:
257 260257 260257 260260 wichtigere deutsche Abkürzungen DE PL Deutsch als Fremdsprache260 261257 25926 (257)257 313 (2)260(1)254 257I CSK 257 12 1254 257więcej podobnych podstron