Click to See Complete Forum and Search --> : ListBox.Style "Check Box - 1" option
John Watson
May 27th, 1999, 04:17 PM
I'm using a listbox that has the Style property set to the "CheckBox - 1" option. Is there a way to disable the other checkboxes if the user clicks a certain checkbox (so he/she cannot make multiple selections). For example, say I have items A thru H and an item called "Select All" in the listbox. If the user clicks "Select All" obviously I don't want the user to still be able to check other boxes--how do I disable them when "Select All" is clicked.
Thanks,
John
Ravi Kiran
May 27th, 1999, 09:17 PM
Hi,
When the style is set to 1, the multi selection automatically is on, though List.Multiselect = 0.
So, if you want your user not to be able to click ( and list box not to respond by checking the box) then you should probably subclass the listbox and do all the things like painting non-active elements in different clrs etc.. yourself.
For a simpler implementation you could just catch the 'Click' event and see if the "Select All" item is checked and check/uncheck all the elements yourself. with something like this:
This will have "funny" look of check mark appearing & disappearing when other items are selected after selecting "Select All" (checked)
private Sub List1_Click()
Dim i, selallIndex
static alreadyrunning as Boolean ' to stop recursive runs
If alreadyrunning = true then Exit Sub
' else implied..
alreadyrunning = true
selallIndex = 0 ' my "select all" is the first item of the list
If List1.Selected(selallIndex) = true then
for i = 0 to List1.ListCount - 1
If (i <> selallIndex) And (List1.Selected(i) = true) then
List1.Selected(i) = false
End If
next i
End If
alreadyrunning = false
End Sub
Better would be to check all other ietms when "Select All" is clicked, with something like this:
private Sub List1_Click()
Dim i, selallIndex, prevLI
static alreadyrunning as Boolean ' to stop recursive runs
If alreadyrunning = true then Exit Sub
' else implied..
alreadyrunning = true
prevLI = List1.ListIndex ' we will be changing the list index.
' so save it and put it back in the end
selallIndex = 0 ' my "select all" is the first item of the list
If List1.Selected(selallIndex) = true then
for i = 0 to List1.ListCount - 1
If (i = selallIndex) then
List1.Selected(i) = false ' remove the check. No point in having.
else
List1.Selected(i) = true ' check all
' This line will also generate another Click event,
' and it needs to be blocked using static variable.
End If
next i
End If
List1.ListIndex = prevLI
alreadyrunning = false
End Sub
Hope this is ok for your requirement.
Ravi
John Watson
May 27th, 1999, 11:19 PM
Ravi:
Thanks for the long code example! Didn't expect to get such a good answer.
John
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.