-
Hi,
I have a form with a ListBox (with Style = 1-Checkbox) and a command button. At any time the listbox contains between 5 and 15 itmes that user can check (select). I want the command button to be enabled ONLY if 1 or more items in the listbox are CHECKED (not just highlighted). And as soon as user deselects ALL items I want to disable the command button.
Your help is appreciated.
-
Try this:
Code:
Private Sub List1_ItemCheck(Item As Integer)
static selcount As Integer
selcount = selcount - (List1.Selected(Item)) * 2 - 1
Command1.Enabled = selcount
End Sub
-
Thanks Kedaman, It works great!
Can you explain how it works, I'm puzzled by the code.
Specially those two lines:
selcount = selcount - (List1.Selected(Item)) * 2 - 1
Command1.Enabled = selcount
-
The selected property returns true if the item specified by index, is selected. And of course false if not.
Well in vb
false = 0 and
true = -1
The selcount i declared static there should count the amount of items selected, if the item that was clicked is set false then you need to decrease selcount, on the other hand true should increase selcount. To get -1 and +1 instead of 0 and -1 you multiply by 2 and substract 1.
Enable is a boolean property and should be set to true if selcount is non-0 that means the implicit convertion used is cbool().