Here's code for Form1 in a new project, containing a listbox named lstBox
having its MultiSelect property set to 1 - Simple, and a command button
named Command1.
Hope it helps,
Pete
'========================================
Option Explicit
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
Private Const LB_ERR = (-1)
Private Const LB_GETSELITEMS = &H191
Private Sub Command1_Click()
Call gFnGen_GetSelItemFromList(lstBox)
End Sub
Private Sub Form_Load()
lstBox.AddItem "A"
lstBox.AddItem "B"
lstBox.AddItem "C"
lstBox.AddItem "D"
lstBox.AddItem "E"
lstBox.AddItem "F"
End Sub
Private Sub gFnGen_GetSelItemFromList(lstBox As ListBox)
Dim SELITEMS() As Long
Dim SelectedCount As Long
Dim i As Long
Dim sMsg As String
Dim rc As Long
SelectedCount = lstBox.SelCount
If SelectedCount > 0 Then
ReDim SELITEMS(SelectedCount - 1)
rc = SendMessage(lstBox.hwnd, LB_GETSELITEMS, SelectedCount,
SELITEMS(0))
End If
If rc <> LB_ERR Then
If rc > 0 Then
sMsg = "Selected items:" & vbCr & vbCr
For i = 0 To rc - 1
sMsg = sMsg & lstBox.List(SELITEMS(i)) & vbCr
Next i
MsgBox sMsg, , "Selected items"
Else
MsgBox "No items were selected", , "Selected items"
End If
Else
MsgBox "The list box is a single-selection list box"
End If
End Sub
'========================================
-----Original Message-----
From: Sai Krishnan [mailto:saikrishnan_d@y...]
Sent: Thursday, September 19, 2002 5:26 AM
To: professional vb
Subject: [pro_vb] Finding selected items in a listbox
Hello all,
I found a piece of code on the net to find the selected items in a list
box. I don't know.. there's some unforgiving error when I call this api
function. . VB crashes.. Can somebody help me out please???
Here is my code.
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal
lParam As Long) As Long
Private Sub gFnGen_GetSelItemFromList(lstBox As ListBox)
Dim SelItems() As Long
Dim SelectedCount As Long
Dim i As Long
Dim sMsg As String
SelectedCount = lstBox.SelCount
If SelectedCount > 0 Then
ReDim SelItems(SelectedCount - 1)
SendMessage lstBox.hWnd, LB_GETSELITEMS, ByVal SelectedCount, SelItems
(0)
End If
If SelectedCount > 0 Then
sMsg = "Selected items:" & vbCr & vbCr
For i = 0 To SelectedCount - 1
sMsg = sMsg & lstBox.List(SelItems(i)) & vbCr
Next i
MsgBox sMsg, , "Selected items"
Else
MsgBox "No items were selected", , "Selected items"
End If
End Sub
Thanx in advance
---
Visual C# - A Guide for VB6 Developers
This book will make it easy to transfer your skills
from Visual Basic 6 to C#, the language of choice
of the .NET Framework.
http://www.wrox.com/ACON11.asp?ISBN=1861007175&p2p0059