Conventional Visual Basic wisdom states that in order to gather
the selected items from a multi-select ListBox, you should loop
through all the items and test the Selected property. As with
all loops, however, this can potentially bog down slower CPU's.
As a much faster and more elegant alternative, you can use the
SendMessage() API function instead.
As you probably know, this function lets you send a message to
one or more windows. The declaration statement conforms to the
following syntax:Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg
_
As Long, ByVal wParam As Long, lParam As Any) As Long
Since we want to gather the listbox's selected items, we'll
send the LB_GETSELITEMS constant in the wMsg argument, which
you declare like so: Private Const LB_GETSELITEMS = &H191
In essence, the LB_GETSELITEMS message fills an array with the
index numbers of all the selected items. As a result, you must
pass two additional arguments with SendMessage(). The first
argument should contain the maximum number of selected items.
To retrieve this value, you can simply use the listbox's
SelCount property. The second argument should hold the array
variable you want to fill with index values. The following
example shows how you might use this function:Dim ItemIndexes() As Long, x As Integer, iNumItems As Integer
iNumItems = ThisBox.SelCount
If iNumItems Then
ReDim ItemIndexes(iNumItems - 1)
SendMessage ListBox1.hwnd, LB_GETSELITEMS, iNumItems, _
ItemIndexes(0)
End If
For x = 0 To iNumItems - 1
MsgBox ListBox1.List(ItemIndexes(x))
Next x
After being passed to the SendMessage function, iNumItems holds
the total number of selected items, and the ItemIndexes array
holds the selected item index values. Notice, that you must
pass a pointer to the ItemIndexes array, and not the array
itself. Thus, we passed ItemIndexes(0) into the SendMessage
function, not ItemIndexes().
Gather multi-selected VB ListBox items
By ElementK Journals, published on 14 Jul 2001
| Filed in
You might also like...
VB 6 forum discussion
-
CorelDRAW VBA: cdrTraceLineDrawing FAILS, producing single linear path instead of Centerline trace?
by dancemanj (0 replies)
-
client/server application using activex
by beautifulheart (0 replies)
-
System Error &H8007007E. The specifed module could not be found.
by swiftsafe (5 replies)
-
Invitation to take part in an academic research study
by researchlab (0 replies)
-
Send SMS with SMPP
by mmahmoud (0 replies)
VB 6 podcasts
-
Stack Overflow Podcast: Podcast #45 – Keeping it Sharp
Published 7 years ago, running time 0h54m
Our guest this week is Eric Lippert – language architect extraordinaire and famous for all his work at Microsoft in developing their languages Eric joined Microsoft right out of college and was originally working on VB It’s time for everyone’s favorite game: Name the Worst Feature of that Microso.
Comments