Results 1 to 5 of 5

Thread: Listbox & Checkbox style ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    89

    Exclamation 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:
    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

  2. #2
    Addicted Member Sibby's Avatar
    Join Date
    Feb 2001
    Location
    Milwaukee, WI *The United States of America*
    Posts
    144
    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....

  3. #3
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    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.

  4. #4
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    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:
    1. Option Explicit
    2.  
    3. Dim mblnSuppressClick   As Boolean
    4. Dim mblnAfterItemCheck  As Boolean
    5.  
    6. Private Sub Form_Load()
    7.    
    8.     Dim intX    As Integer
    9.    
    10.     For intX = 1 To 10
    11.         List1.AddItem "Person #" & intX
    12.     Next
    13.  
    14. End Sub
    15.  
    16. Private Sub List1_Click()
    17.    
    18.     If mblnAfterItemCheck = True Then
    19.         mblnAfterItemCheck = False
    20.         Exit Sub
    21.     End If
    22.    
    23.     If mblnSuppressClick = True Then
    24.         mblnSuppressClick = False
    25.         Exit Sub
    26.     End If
    27.  
    28.     mblnSuppressClick = True
    29.  
    30.     If List1.SelCount > 6 Then
    31.         MsgBox "A maximum of 6 people can be selected."
    32.         List1.Selected(List1.ListIndex) = False
    33.     Else
    34.         List1.Selected(List1.ListIndex) = Not List1.Selected(List1.ListIndex)
    35.         If List1.SelCount > 6 Then
    36.             MsgBox "A maximum of 6 people can be selected."
    37.             List1.Selected(List1.ListIndex) = False
    38.         End If
    39.     End If
    40.    
    41.    
    42. End Sub
    43.  
    44. Private Sub List1_ItemCheck(Item As Integer)
    45.    
    46.     If mblnSuppressClick = True Then
    47.         mblnSuppressClick = False
    48.         mblnAfterItemCheck = True
    49.         Exit Sub
    50.     End If
    51.  
    52.     mblnSuppressClick = True
    53.    
    54.     If List1.SelCount > 6 Then
    55.         MsgBox "A maximum of 6 people can be selected."
    56.         List1.Selected(List1.ListIndex) = False
    57.     End If
    58.    
    59. End Sub
    "It's cold gin time again ..."

    Check out my website here.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    89

    Red face

    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
  •  



Click Here to Expand Forum to Full Width