Select and de-select from a list box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tara99
    New Member
    • Oct 2006
    • 106

    Select and de-select from a list box

    I have 4 list boxes in the same form and few buttons which performs operation on the selection from the list.
    Now, I want the user to only select from one list at a time. For example if user already has selected an item from list1 and he/she selects an other item from list2 then list1 be de-selected.

    Can any one help????

    Thanks
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by tara99
    I have 4 list boxes in the same form and few buttons which performs operation on the selection from the list.
    Now, I want the user to only select from one list at a time. For example if user already has selected an item from list1 and he/she selects an other item from list2 then list1 be de-selected.

    Can any one help????

    Thanks
    When an item is selected in one listbox, you can probably pass the others to a fairly simple function, something like this...
    ClearOut List0
    ClearOut List1
    ClearOut List2
    Code:
    Private Sub ClearOut(Src As ListBox)
    
      Dim I As Long
      With Src
        For I = 0 To .ListCount - 1
          If .Selected(I) Then
            .Selected(I) = False
          End If
        Next
      End With
            
    
    End Sub
    Note, I wrote this in Access VB editor, but have not tested it. I tend to think on VB6, so good luck. :)
    Last edited by Killer42; Nov 2 '06, 02:20 AM. Reason: Typo

    Comment

    • tara99
      New Member
      • Oct 2006
      • 106

      #3
      Originally posted by Killer42
      When an item is selected in one listbox, you can probably pass the others to a fairly simple function, something like this...
      ClearOut List0
      ClearOut List1
      ClearOut List2
      Code:
      Private Sub ClearOut(Src As ListBox)
      
        Dim I As Long
        With Src
          For I = 0 To .ListCount - 1
            If .Selected(I) Then
              .Selected(I) = False
            End If
          Next
        End With
              
      
      End Sub
      Note, I wrote this in Access VB editor, but have not tested it. I tend to think on VB6, so good luck. :)
      I tried it it didn't work,
      Can you please Clarify
      Thanks

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        Originally posted by tara99
        I have 4 list boxes in the same form and few buttons which performs operation on the selection from the list.
        Now, I want the user to only select from one list at a time. For example if user already has selected an item from list1 and he/she selects an other item from list2 then list1 be de-selected.

        Can any one help????

        Thanks
        This should do it:

        Code:
         
        Private Sub List2_AfterUpdate()
        Dim i As Integer
        	For i = 0 To List1.ListCount - 1
        		Me.List1.Selected(i) = False
        	Next i
        	
        End Sub

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by mmccarthy
          This should do it:
          ...
          Hah! Beat you to the punch by a couple of minutes. :)

          Seriously, it looks as though we came up with the same solution (not surprising, given the simplicity), except that I threw it into a generic Sub.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by Killer42
            Hah! Beat you to the punch by a couple of minutes. :)

            Seriously, it looks as though we came up with the same solution (not surprising, given the simplicity), except that I threw it into a generic Sub.
            Oops!

            I only read the last entry.

            Hm... any idea why mine didn't work? I know it sounds like a cop-out, but under the circumstances I'm inclined to blame the implementation.

            Comment

            • tara99
              New Member
              • Oct 2006
              • 106

              #7
              Originally posted by mmccarthy
              This should do it:

              Code:
               
              Private Sub List2_AfterUpdate()
              Dim i As Integer
              	For i = 0 To List1.ListCount - 1
              		Me.List1.Selected(i) = False
              	Next i
              	
              End Sub
              Hi mmccarthy
              I have 4 different lists, does this works for all of them?
              Sorry the code looks simple but I didn't understand it.
              Can you explain?

              Thanks

              Comment

              • MMcCarthy
                Recognized Expert MVP
                • Aug 2006
                • 14387

                #8
                Originally posted by Killer42
                Oops!

                I only read the last entry.

                Hm... any idea why mine didn't work? I know it sounds like a cop-out, but under the circumstances I'm inclined to blame the implementation.
                I agree

                I'd guess the procedure wasn't called correctly.

                Anyway,

                Great minds think alike ...

                I always forget the second line of that quote <grin>

                Mary

                Comment

                • MMcCarthy
                  Recognized Expert MVP
                  • Aug 2006
                  • 14387

                  #9
                  Originally posted by tara99
                  Hi mmccarthy
                  I have 4 different lists, does this works for all of them?
                  Sorry the code looks simple but I didn't understand it.
                  Can you explain?

                  Thanks
                  Tara this is just a sample of the code:

                  In the AfterUpdate event of the each of the four listboxes you would put the code to clear the other 3. For example, if listbox List2 has an item selected then the following code would clear the other 3. You need to try these things out Tara and come back then if you have any problems.

                  [code]

                  Private Sub List2_AfterUpda te()
                  Dim i As Integer

                  ' this clears listbox List1 by deselecting all items on the list
                  For i = 0 To List1.ListCount - 1
                  Me.List1.Select ed(i) = False
                  Next i

                  ' this clears listbox List3 by deselecting all items on the list
                  For i = 0 To List3.ListCount - 1
                  Me.List3.Select ed(i) = False
                  Next i

                  ' this clears listbox List4 by deselecting all items on the list
                  For i = 0 To List4.ListCount - 1
                  Me.List4.Select ed(i) = False
                  Next i

                  End Sub

                  Comment

                  • tara99
                    New Member
                    • Oct 2006
                    • 106

                    #10
                    Originally posted by mmccarthy
                    Tara this is just a sample of the code:

                    In the AfterUpdate event of the each of the four listboxes you would put the code to clear the other 3. For example, if listbox List2 has an item selected then the following code would clear the other 3. You need to try these things out Tara and come back then if you have any problems.

                    [code]

                    Private Sub List2_AfterUpda te()
                    Dim i As Integer

                    ' this clears listbox List1 by deselecting all items on the list
                    For i = 0 To List1.ListCount - 1
                    Me.List1.Select ed(i) = False
                    Next i

                    ' this clears listbox List3 by deselecting all items on the list
                    For i = 0 To List3.ListCount - 1
                    Me.List3.Select ed(i) = False
                    Next i

                    ' this clears listbox List4 by deselecting all items on the list
                    For i = 0 To List4.ListCount - 1
                    Me.List4.Select ed(i) = False
                    Next i

                    End Sub
                    Thank you so much

                    It is working

                    cheers

                    Comment

                    • tara99
                      New Member
                      • Oct 2006
                      • 106

                      #11
                      It is solved

                      Thanks

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Originally posted by tara99
                        It is solved

                        Thanks
                        About that explanation you asked for.

                        The .Selected property of the listbox control holds an array of boolean values as long as the number of items in the list. Each is true or false, indicating whether that item is selected. Changing the value also selects or de-selects the item. The listbox also has a property called ListCount which reflects the number of items in the list.

                        The loop that we were playing with simply goes through all the items in the list (they are numbered starting at 0 - don't get me started!) and turns off the Selected property for each one.

                        Personally, I still feel that my generic routine is the better way to go, rather than hard-coding the process for each listbox. But hey, if it works, it works.

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Originally posted by mmccarthy
                          ...
                          Great minds think alike ...
                          I always forget the second line of that quote<grin>
                          I don't think I've ever heard a second line, so the <grin> was probably wasted on me.

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32645

                            #14
                            Originally posted by Killer42
                            I don't think I've ever heard a second line, so the <grin> was probably wasted on me.
                            Great minds think alike - but fools seldom differ!

                            Comment

                            • Killer42
                              Recognized Expert Expert
                              • Oct 2006
                              • 8429

                              #15
                              Originally posted by NeoPa
                              Great minds think alike - but fools seldom differ!
                              Hm... now that I "hear" it, this does seem to ring a bell. I probably heard it years ago.

                              Comment

                              Working...