Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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
[236 byte] By [jobartley515] at [2007-11-11 8:09:49]
# 1 Re: Listboxes
Didn't I answer this question for you last week?
Phil Weber at 2007-11-11 17:25:50 >
# 2 Re: Listboxes
Yes however that was in VB.Net.

Any help with this in VB Classic?
jobartley515 at 2007-11-11 17:26:51 >
# 3 Re: Listboxes
Can anybody help me with this?
jobartley515 at 2007-11-11 17:27:57 >
# 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.
Benjamin at 2007-11-11 17:29:02 >
# 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
Phil Weber at 2007-11-11 17:30:01 >
# 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 :)
Benjamin at 2007-11-11 17:30:59 >
# 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
Phil Weber at 2007-11-11 17:32:03 >
# 8 Re: Listboxes
Thanks you guys are great. (Sometimes my mind just can't wrap around these things)
jobartley515 at 2007-11-11 17:32:58 >
# 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
jobartley515 at 2007-11-11 17:34:06 >
# 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.
Phil Weber at 2007-11-11 17:35:06 >
# 11 Re: Listboxes
I'm not sure how to do that... :)
jobartley515 at 2007-11-11 17:36:01 >
# 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).
Phil Weber at 2007-11-11 17:37:10 >
# 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
jobartley515 at 2007-11-11 17:38:06 >
# 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
Phil Weber at 2007-11-11 17:39:11 >