Listboxes
Well I have two listboxes (one thats in standard style and the other in checkboxes style) and I want to know how I can select items that are in the checkboxes listbox that are in the standard listbox.
Any help?
Thanks
# 1 Re: Listboxes
Didn't I answer this question for you last week?
# 2 Re: Listboxes
Yes however that was in VB.Net.
Any help with this in VB Classic?
# 3 Re: Listboxes
Can anybody help me with this?
# 4 Re: Listboxes
Hi,
I've searched the net and I didn't find anything. I found some adapted ocx files but no tutorials about this. I'm wondering if this is possible, I 'm curious.
# 5 Re: Listboxes
I'm sure you can figure it out, just break the problem down into steps:
1. Loop through items in standard listbox
2. For each item, search for matching item in checked listbox
3. If a match is found, check it
# 6 Re: Listboxes
Hi,
I found a solution:
Dim intIndex As Integer
Dim intCheckIndex As Integer
'Loop trough standard box list3
intIndex = 0
Do While intIndex < Form1.List3.ListCount
'Loop trough check-Listbox list2
intCheckIndex = 0
Do While intCheckIndex < Form1.List2.ListCount
If Form1.List3.List(intIndex) = Form1.List2.List(intCheckIndex) Then
List2.Selected(intCheckIndex) = True
End If
intCheckIndex = intCheckIndex + 1
Loop
intIndex = intIndex + 1
Loop
My problem was how to set the checkbox of the item to true.
=> List2.Selected(intCheckIndex) = True
Learned something :)
# 7 Re: Listboxes
OK, good (although I was kind of hoping jobartley would figure it out ;-). You could also use a For...Next loop:
Dim I As Integer
Dim J As Integer
For I = 0 To lstBox.ListCount - 1
For J = 0 To lstCheck.ListCount - 1
If lstBox.List(I) = lstCheck.List(J) Then
lstCheck.Selected(J) = True
End If
Next
Next
# 8 Re: Listboxes
Thanks you guys are great. (Sometimes my mind just can't wrap around these things)
# 9 Re: Listboxes
Oh by the way I was hoping you could help me with another problem...
I want to save which items are checked off (in the checked listbox) to the registry.
I've tried this:
Dim I As Integer
For I = 0 To chklist.SelCount - 1
SaveSetting "MyProgram", "Settings", chklist.List(I), "True"
Next
But it doesn't seem to work
(If I check two checkboxes near the bottom of the list it only save the first two)
Thanks again
# 10 Re: Listboxes
If your list contains, say, 10 items, they will be List(0) through List(9). If you check 4 of them, SelCount will return 4. But you don't want to save the first 4 items in the list (which is what your code is doing), you want to save the items that are checked.
You'll need to loop through the entire list and only save those items that are Selected.
# 11 Re: Listboxes
I'm not sure how to do that... :)
# 12 Re: Listboxes
OK, meet me halfway. ;-) Post the code to loop through the entire list, and I'll add the code to check if each item is Selected (that's a hint, by the way).
# 13 Re: Listboxes
I finally got it, take a look...
Do While chklist.ListCount > 0
If chklist.Selected(0) = True Then
SaveSetting "MyProgram", "Settings", chklist.List(0), "True"
End If
chklist.RemoveItem (0)
Loop
Is this what you meant?
Thanks anyway
# 14 Re: Listboxes
I would have done it this way (doesn't require removing items from the list):
For I = 0 To chkList.ListCount - 1
If chkList.Selected(I) Then
SaveSetting "MyProgram", "Settings", chklist.List(I), "True"
End If
Next