Multiple Selection Listbox for Visual Basic Version 0.2 1/4/91 by Mark Gamber *-----------------------------------------------------------------------------* One control I have needed in Visual Basic is a multiple selection listbox. I am assuming I am not alone by sending this into the wild blue yonder. To use the control in your programs, select "Add File..." from the "File" menu of Visual Basic and select "MULTSEL.VBX". If the control bitmap is added to the toolbox, you're ready to begin using the listbox. *-----------------------------------------------------------------------------* Improvements have been made on the original, Version 0.1, which make this version far easier to use and understand. First, a quick rundown of the various things the control offers and then, onto an example program in Visual Basic. *-----------------------------------------------------------------------------* The control is addressed as MultipleSel. Thus, the first listbox on a form is addresses as MultipleSel1 and so on. Adding to and removing from the list- -box is the same as with the standard Visual Basic listbox. For example, to add an item the listbox, you could use this line: MultipleSel1.AddItem "We add this line" To remove line 0 (the one on the top), you might use this line: MultipleSel1.RemoveItem 0 Easy, huh? Nothing new there. Along with those familiar functions comes ListCount which returns the number of items in the listbox. For example: num = MultipleSel1.ListCount ListIndex is the first fork in the road from the standard listbox. Since there may be several items selected, you cannot select any particular item. Instead, this returns whether or not an item has been selected. For example, if we want to see if item four has been selected, we could use this line: if MultipleSel1.ListIndex(3) then ... Remember, it starts at zero so item four is indexed as three. Anyway, if the item was selected, the return value is TRUE (-1). If unselected, the return value is FALSE (0). To see if items one through ten have been selected, we could use this loop: for i% = 0 to 9 if MultipleSel1.ListIndex(i%) then ... next Finally, to retrieve the text of a particular item, we use the List function. For example, to get the text for item zero (the top of the listbox): t$ = MultipleSel1.List(0) In a loop, we could use something like this: for i% = 0 to 10 if MultipleSel1.ListIndex(i%) then Picture1.Print MultipleSel1.List(i%) endif next The above loop will print all selected items in the listbox. Nice, huh? *-----------------------------------------------------------------------------* So there's a basic outline of the listbox. The example program is called MSELTEST.EXE and is available with this file. I suggest you look at the code for Command1 and Command2 buttons which is what controls the adding and testing of the listbox items. *-----------------------------------------------------------------------------* As always, this program is free as long as you agree that I did nothing to ruin your life in any way. After all, it's just a simple control. I may be reached for questions, comments, suggestions and praise via America Online, mail address MarkG85. I do NOT answer phone calls as that's reserved for my real job. I hope you find this control as useful as I did.