CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 1999
    Posts
    13

    ListBox.Style "Check Box - 1" option

    I'm using a listbox that has the Style property set to the "CheckBox - 1" option. Is there a way to disable the other checkboxes if the user clicks a certain checkbox (so he/she cannot make multiple selections). For example, say I have items A thru H and an item called "Select All" in the listbox. If the user clicks "Select All" obviously I don't want the user to still be able to check other boxes--how do I disable them when "Select All" is clicked.

    Thanks,
    John


  2. #2
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: ListBox.Style "Check Box - 1" option

    Hi,

    When the style is set to 1, the multi selection automatically is on, though List.Multiselect = 0.
    So, if you want your user not to be able to click ( and list box not to respond by checking the box) then you should probably subclass the listbox and do all the things like painting non-active elements in different clrs etc.. yourself.

    For a simpler implementation you could just catch the 'Click' event and see if the "Select All" item is checked and check/uncheck all the elements yourself. with something like this:
    This will have "funny" look of check mark appearing & disappearing when other items are selected after selecting "Select All" (checked)

    private Sub List1_Click()
    Dim i, selallIndex
    static alreadyrunning as Boolean ' to stop recursive runs
    If alreadyrunning = true then Exit Sub
    ' else implied..
    alreadyrunning = true

    selallIndex = 0 ' my "select all" is the first item of the list
    If List1.Selected(selallIndex) = true then
    for i = 0 to List1.ListCount - 1
    If (i <> selallIndex) And (List1.Selected(i) = true) then
    List1.Selected(i) = false
    End If
    next i
    End If
    alreadyrunning = false
    End Sub




    Better would be to check all other ietms when "Select All" is clicked, with something like this:

    private Sub List1_Click()
    Dim i, selallIndex, prevLI
    static alreadyrunning as Boolean ' to stop recursive runs
    If alreadyrunning = true then Exit Sub
    ' else implied..
    alreadyrunning = true
    prevLI = List1.ListIndex ' we will be changing the list index.
    ' so save it and put it back in the end
    selallIndex = 0 ' my "select all" is the first item of the list
    If List1.Selected(selallIndex) = true then
    for i = 0 to List1.ListCount - 1
    If (i = selallIndex) then
    List1.Selected(i) = false ' remove the check. No point in having.
    else
    List1.Selected(i) = true ' check all
    ' This line will also generate another Click event,
    ' and it needs to be blocked using static variable.
    End If
    next i
    End If
    List1.ListIndex = prevLI
    alreadyrunning = false
    End Sub




    Hope this is ok for your requirement.
    Ravi


  3. #3
    Join Date
    May 1999
    Posts
    13

    Re: ListBox.Style "Check Box - 1" option

    Ravi:

    Thanks for the long code example! Didn't expect to get such a good answer.

    John


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

Featured