Unbound List box .Selected property not working correctly.

GBalcom

Much to learn!
Local time
, 18:03
Joined
Jun 7, 2012
Messages
459
I have two unbound list boxes. The first one is populated with File names from a given folder. I want to be able to push them to the other list box, using the multi-select, selected property. Then, remove the selected items from the first listbox. The .selected only seems to work on the last value selected. The others are highlighted, but do not come in as selected.

Code:
Public Sub MoveListBoxSelections(ActiveCtrl As ListBox, PassiveCtrl As ListBox)

    Dim i As Integer
    Dim varItem As Variant
   
    Dim ListCount As Integer
    ListCount = ActiveCtrl.ListCount
   
   
    For i = ListCount - 1 To 0 Step -1
        If ActiveCtrl.Selected(i) Then
             PassiveCtrl.AddItem (ActiveCtrl.ItemData(i))
            ActiveCtrl.RemoveItem (i)
        End If
   
    Next i

I get all of the correct items to the next list box, but it ends up deleting the wrong ones.
 

Attachments

  • Overall Form.PNG
    Overall Form.PNG
    15.2 KB · Views: 479
Hi. Did you step through your code? When I tried it, the selected items were reset as soon as you remove the first moved item to the second listbox.
 
Hi. In case you're interested, the approach I was able to use to make it work was a four-step process.
  1. Transfer the selected items from Listbox1 to Listbox2
  2. Select the newly transferred items in Listbox2
  3. Delete selected items from Listbox1
  4. Deselect all items in Listbox2
I think you can eliminate Steps 2 and 4 if you don't need to reselect the same items from Listbox1 to Listbox2. Or just eliminate Step 4 if you want to keep the same items selected in Listbox2.

Hope that helps...
 
DB Guy,
Thanks a bunch for taking a look at this. Can you explain step 3 above? How are you grabbing all of those selected, to remove, if after the first one in that group is removed, it is resetting?

I also tried this code to remove them, but It runs from front to back of the loop, and screws it all up. I think because let's say item #3 is removed, now all the ones behind it move one step closer to the front of the line, and the next one was supposed to remove #5, but ends up removing #4, etc. Hope that makes sense.

Code:
    For Each varItem In ActiveCtrl.ItemsSelected
      'PassiveCtrl.AddItem ActiveCtrl.ItemData(varItem)
         ActiveCtrl.RemoveItem (varItem)
    Next varItem
 
DB Guy,
Thanks a bunch for taking a look at this. Can you explain step 3 above? How are you grabbing all of those selected, to remove, if after the first one in that group is removed, it is resetting?
For Step 3, I am simply using this code.
Code:
'delete
For x = 0 To Me.List2.ListCount - 1
    On Error Resume Next
    Me.List0.RemoveItem Me.List2.ItemData(x)
Next
The reason I created Step 2 was because I thought about using it to avoid the On Error Resume Next line, which would look something like this.
Code:
'or delete this way
For Each x In Me.List2.ItemsSelected
    For y = Me.List0.ListCount - 1 To 0 Step -1
        If Me.List0.ItemData(y) = Me.List2.ItemData(x) Then
            Me.List0.RemoveItem (y)
            Exit For
        End If
    Next
Next
Hope that helps...
 
I use this functionality a lot, so I automated this so I can use it without rewriting any code. I do not use a multiselect list, because it saves no time. Easier to have it fire on a double click or single click. If interested.
The code handles both value lists, lists bound to a query, or not bound.
 

Attachments

First, loop to determine which items are selected.

After that, loop through to remove items.

Do not mix the two, this changes the index that the loop is based on, mid-flight. This is why you are having problems.
 
Gentlemen,
After being off this project for a while, I was able to get back to it today and with your help, fix this task. Thanks again for your expertise.
 
Gentlemen,
After being off this project for a while, I was able to get back to it today and with your help, fix this task. Thanks again for your expertise.
Congratulations! Good luck with your project.
 

Users who are viewing this thread

Back
Top Bottom