VB6
Bluebox Global Solutions Stocks

Visual Basic 6 (VB6) and Visual Basic.net (VB.NET)
Software Developer: Mark Sheldon Wong

 
 


Visual Basic
 

VB6 Information
 

Visual Basic • Visual Basic.net • VB6 Tutorials • VB6 download
• Proigramming Visual Basic • VB.NET Download • Visual Basic Tutorials • Download VB.NET  • How to Program in VB6 •
Visual Basic.Net
Mark Sheldon Wong

 

 

 

Google

Using VB Listbox
Learn everything about the Listbox control. From simple to advanced to very advanced things you can do.


ListBoxes provide two property arrays into which you can place data: List and ItemData. Whenever you reference these properties of the ListBox control, you must do so with an index reference, i.e.:



List1.List(X) or List1.ItemData(X)



The List array is a zero-based array of STRING items and is what the user sees in the ListBox. The ItemData array is an array of LONG INTEGER items in which you can OPTIONALLY place a numeric data item associated with the corresponding string item. If ItemData is used, each element is always associated with the corresponding element of the List array; it is not an independent array.



You cannot tell VB ahead of time how many items will be placed in the ListBox. The number of elements in the ListBox is maintained by VB via the ListCount property (a read-only property of ListBoxes), which is dependent on the use of the AddItem method to get items into the ListBox and the RemoveItem method to get items out of the ListBox.



Getting Data into a ListBox



The AddItem Method



To get data into the ListBox, you use the AddItem method. The syntax is:



ListboxName.AddItem string



This syntax places the string item into an element of the List property array. It does not affect the ItemData property array; the AddItem method is not used to place data in the ItemData array. The technique to place an item into the corresponding element of the ItemData array will be shown a little further below. Remember that the use of the ItemData array is optional, and if your application does not need to have numeric items associated with the string items in the ListBox, then you need not use it.



With the AddItem method, the issue of which element of the List array an item is placed in depends on the setting of the Sorted property. You can set the Sorted property of the ListBox control at design time to True or False; by default it is set to False.



When Sorted is False, VB adds items to the ListBox in sequential order. For example, consider the following three statements to add the items "Orange", "Apple", and "Banana" to a ListBox called lstFood:



lstFood.AddItem "Orange"

lstFood.AddItem "Apple"

lstFood.AddItem "Banana"



The data would be stored in the ListBox in the following manner:

 

ARRAY INDEX
 

Array Index #  List Array Elements

0 = Orange

1 = Apple

2 = Banana

When Sorted is True, VB automatically adjusts the indexes as items are added to the ListBox so that the elements of the List array are in ascending sequence. Once again, assume you coded the following:

 

lstFood.AddItem "Orange"

lstFood.AddItem "Apple"

lstFood.AddItem "Banana"

Removing Duplicate Entries in a ListBox
This example incorporates a general Sub procedure that you can use to remove duplicate entries from a listbox. The code for the Sub is:



'-----------------------------------------------------------------------------

Public Sub RemoveListBoxDuplicates(pobjLB As ListBox)

'-----------------------------------------------------------------------------



'REMOVE DUPLICATES FROM LISTBOX

Dim intI As Integer

Dim intJ As Integer

With pobjLB

For intI = 0 To .ListCount - 1

For intJ = .ListCount To (intI + 1) Step -1

If .List(intJ) = .List(intI) Then

.RemoveItem intJ

End If

Next

Next

End With



End Sub

 

In the sample program, clicking the "Load ListBox" button loads the listbox with 100 entries, where an entry will be any one of 20 company names selected at random:

Download it here Download


By: Mark Sheldon Wong - Bluebox Global Solutions
 

 

 

 

 

Bluebox Global Solutions