|
-
Jan 8th, 2004, 12:45 PM
#1
Thread Starter
Lively Member
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
-
Jan 8th, 2004, 01:42 PM
#2
Addicted Member
Hi Chris,
I threw your code into a smple project and it worked fine. If an item in the listbox is simply selected (blue highlight) and not checked, "lstTest.Selected(lstTest.ListIndex)" should return false, are you saying yours is returning true? If so, what version of VB are you running?
Let me know....
Sibby
If you can think it....you can code it....
-
Jan 8th, 2004, 02:31 PM
#3
Frenzied Member
Remove the checkbox option and just have a list to pick from? If it's causing that much grief, sounds like it's more trouble than it's worth.
-
Jan 8th, 2004, 02:45 PM
#4
A problem like this can be a little more involved than you may initially think, due to VB's handling of events. I messed around and came up with this, which I think will do what you want. (You can open a new VB project, slap a listbox with checkbox style on the form, paste in this code, and check it out.)
VB Code:
Option Explicit
Dim mblnSuppressClick As Boolean
Dim mblnAfterItemCheck As Boolean
Private Sub Form_Load()
Dim intX As Integer
For intX = 1 To 10
List1.AddItem "Person #" & intX
Next
End Sub
Private Sub List1_Click()
If mblnAfterItemCheck = True Then
mblnAfterItemCheck = False
Exit Sub
End If
If mblnSuppressClick = True Then
mblnSuppressClick = False
Exit Sub
End If
mblnSuppressClick = True
If List1.SelCount > 6 Then
MsgBox "A maximum of 6 people can be selected."
List1.Selected(List1.ListIndex) = False
Else
List1.Selected(List1.ListIndex) = Not List1.Selected(List1.ListIndex)
If List1.SelCount > 6 Then
MsgBox "A maximum of 6 people can be selected."
List1.Selected(List1.ListIndex) = False
End If
End If
End Sub
Private Sub List1_ItemCheck(Item As Integer)
If mblnSuppressClick = True Then
mblnSuppressClick = False
mblnAfterItemCheck = True
Exit Sub
End If
mblnSuppressClick = True
If List1.SelCount > 6 Then
MsgBox "A maximum of 6 people can be selected."
List1.Selected(List1.ListIndex) = False
End If
End Sub
"It's cold gin time again ..."
Check out my website here.
-
Jan 8th, 2004, 05:57 PM
#5
Thread Starter
Lively Member
Thank you all for replying, Bruce your code runs smoothly now i'm slapping myself for even thinking of using the checkbox style in the listbox.
Taking it off is the easiest solution and using a different style. Thanks for your prompt posts!
I ran into another problem though..... (this is only if you take off checkbox as a style and put on multiselelection as "extended),
Yes we all know how to use a mouse, but from a user's standpoint, what if you clicked on the name, and dragged it alittle bit (even after a maximum of 6) it is still being selected instead of the message box telling you "you can't select more than one!")
any suggestions?
Chris
Last edited by Trancedified; Jan 8th, 2004 at 07:32 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|