Listbox & Checkbox style ?
Hello,
Just had a general question: is there a way to limit a user's selection in a listbox? Here's the example:
I have a listbox w/ the style as "checkbox", my code only works when the user clicks inside of the checkbox to select the list of names (it is in an array and you can't select more than 6 people). This code is fine, but only if the user clicks on the checkbox and not the actual person's name that's next to the checkbox.
VB Code:
Private mActiveListCount As Integer 'global count indicating the number of selected people from listbox
Public Sub lstTest_Click()
'Checking the list box
If lstTest.Selected(lstTest.ListIndex) Then
mActiveListCount = mActiveListCount + 1
If mActiveListCount > 6 Then
MsgBox "Please choose six or less people only.", vbInformation
cmdSix.Enabled = False
lstTest.Selected(lstTest.ListIndex) = False
mActiveListCount = 6
End If
'Unchecking the list box if there's more than 6
Else
mActiveListCount = mActiveListCount - 1
If mActiveListCount < 0 Then
mActiveListCount = 0
End If
End If
'Enabling cmdSix button
If mActiveListCount >= 1 Then
cmdSix.Enabled = True
cmdPreview.Enabled = True
Else
cmdSix.Enabled = False
cmdPreview.Enabled = False
End If
However, if you click outside of the checkbox like on the actual person's name (text) the blue highlighter goes to the item, but the checkbox isn't checked and the code I did doesn't work.
I thought of 2 possible solutions:
1. Is there a way to prevent the user from clicking on the person's name w/out the highlighter selecting it? (which messes up the flow of the application since the highlighter is on the name but the checkbox isn't checked and VB thinks it's "checked" )
-or-
2. If a user does click outside of the checkbox and on the name like "Chris," then the checkbox will be automatically set as "checked." (so that it goes with the flow of the application)
Any suggestions?
Thanks for your time!
Chris