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:
  1. Private mActiveListCount As Integer 'global count indicating the number of selected people from listbox
  2.  
  3. Public Sub lstTest_Click()
  4.     'Checking the list box
  5.     If lstTest.Selected(lstTest.ListIndex) Then
  6.         mActiveListCount = mActiveListCount + 1
  7.         If mActiveListCount > 6 Then
  8.             MsgBox "Please choose six or less people only.", vbInformation
  9.             cmdSix.Enabled = False          
  10.             lstTest.Selected(lstTest.ListIndex) = False
  11.             mActiveListCount = 6
  12.         End If
  13.     'Unchecking the list box if there's more than 6
  14.     Else
  15.         mActiveListCount = mActiveListCount - 1
  16.         If mActiveListCount < 0 Then
  17.             mActiveListCount = 0
  18.         End If
  19.     End If
  20.     'Enabling cmdSix button
  21.     If mActiveListCount >= 1 Then
  22.         cmdSix.Enabled = True
  23.         cmdPreview.Enabled = True
  24.     Else
  25.         cmdSix.Enabled = False
  26.         cmdPreview.Enabled = False
  27.     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