[RESOLVED] Listbox Selection Question
I can't seem to figure this one out.
What I am trying to do is cycle through a large list of items in a ListBox and be able to get the text of whatever item is currently selected.
I have a listbox with multiselect enabled. I want it to go through a list of names and grab only the ones that are selected and add them to a second listbox.
Here is what I'm doing.
VB Code:
Dim i As Integer
Do until i = lstnames.ListIndex -1
If lstnames.Selected(i) = True then
lstdept.AddItem 'this is where I get stuck
End If
i = i + 1
Loop
I've tried using lstnames.Text, but that only keeps grabbing the first item selected.
I appreciate the help :)
Thanks
Re: Listbox Selection Question
Unless I'm missing something, try this:
VB Code:
Dim i As Integer
Do until i = lstnames.ListIndex -1
If lstnames.Selected(i) = True then
lstdept.AddItem lstnames.list(i)
End If
i = i + 1
Loop
Re: Listbox Selection Question
I think even if you have selected many items lstnames.Selected(i) = True will actually be one only. You could try a listbox with a checkbox instead...
Re: Listbox Selection Question
Quote:
Originally Posted by dglienna
Unless I'm missing something, try this:
VB Code:
Dim i As Integer
Do until i = lstnames.ListIndex -1
If lstnames.Selected(i) = True then
lstdept.AddItem lstnames.list(i)
End If
i = i + 1
Loop
This returns an error saying invalid use of property.
Thanks tho :)
Re: Listbox Selection Question
Quote:
Originally Posted by dee-u
I think even if you have selected many items lstnames.Selected(i) = True will actually be one only. You could try a listbox with a checkbox instead...
I'm willing to try it if the other way is impossible.
What would I have to do different to the code I posted above for a checked listbox? I've never used it like that before :)
Thanks
Re: Listbox Selection Question
Quote:
Originally Posted by dee-u
I think even if you have selected many items lstnames.Selected(i) = True will actually be one only. You could try a listbox with a checkbox instead...
Works for me :ehh:
This?
VB Code:
For i = 0 To List1.ListCount - 1
If List1.Selected(i) = True Then
list2.AddItem List1.List(i)
End If
Next
Re: Listbox Selection Question
Quote:
Originally Posted by manavo11
Works for me :ehh:
This?
VB Code:
For i = 0 To List1.ListCount - 1
If List1.Selected(i) = True Then
list2.AddItem List1.List(i)
End If
Next
Works like a charm!
Just what I wanted
Thank You :D :wave:
Re: Listbox Selection Question
Quote:
Originally Posted by manavo11
Works for me :ehh:
This?
VB Code:
For i = 0 To List1.ListCount - 1
If List1.Selected(i) = True Then
list2.AddItem List1.List(i)
End If
Next
I stand corrected. :)
Re: Listbox Selection Question
Quote:
Originally Posted by Capp
Works like a charm!
Just what I wanted
Thank You :D :wave:
You're welcome :)
Quote:
Originally Posted by dee-u
I stand corrected. :)
:afrog: