Two listboxes with the same info. (Almost).

ok. here goes...

Part of my app searches the drive for files, then displays them in 2 listboxes.

One (List1) displays the filename only.

The other one (List2), displays the entire path.

I synchronize these listboxes with,

List2.ListIndex = List1.ListIndex

When the .Sorted Property is set to False on the listboxes, they work fine. But when they are set to True, they don't match up. Which is almost common sense that they won't seeing as there is different data displayed.

Mark2150 suggested that I should use an Array.(This was at the end of a different question). I probably did it wrong, but all i got was one file displayed 27 times which happens to be the amount of files that i had searched for.

So I am asking this question in hopes that someone may show me either how to use an Array Properly, or to show me how we can take it in a different direction to get the same desired results.


How can I synchronize two Listboxes with the .Sorted Property set to True?


kkoserAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JuiletteCommented:
If you are getting the same file 27 times it means your increment is outside the counter..or you are using list.text which will remain the same unless the index is moved.

If you fill the array at the same time with the same code you fill the list you should be ok.
ie..
if you have

for i = 1 to filecount '[whatever you use as filecount]
 list1.additem filename
 sArray(i) = filename
next i

I'm not sure what exactyly you are after but that would fill the array with each filename that you put into the list box.




0
ThaSmartUnoCommented:
this could be too long but what u could do is whenever you want to add an item to list1

then you could call a sub and in the sub you could clear the list2 and then just add everything in list1 to list2 again
0
kkoserAuthor Commented:
Maybe this will help explain.



Private Sub Form_Load()
   
Dim strFind() As String
Dim lngFound As Long
Dim lngCount As Long

Set objFind = New clsFileFinder

 Me.MousePointer = vbHourglass
 DoEvents
 Command1.Enabled = False
 lngFound = objFind.LocateFileLocally("*.avi", strFind)
   If objFind.blnFindAbort Then
    Exit Sub
   End If
 Command1.Enabled = True
 Me.MousePointer = vbDefault
 For lngCount = 0 To lngFound - 1
 
  List1.AddItem sGetFileName(strFind(lngCount)) 'No path
  List2.AddItem strFind(lngCount)                      'Path Included
 
 Next

Set objFind = Nothing

End Sub



Function sGetFileName(sFullPath As String) As String
Dim i As Integer
   For i = Len(sFullPath) To 1 Step -1
    If (Mid$(sFullPath, i, 1) = "\") Then Exit For
   Next
   sGetFileName = Mid$(sFullPath, i + 1)
End Function
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

mcriderCommented:
Here is a really small example of how to match up two listbox items when one of them is sorted.

1) create a new project.

2) add two listboxes to Form1.

3) set the SORTED property of List1 to TRUE.

4) Paste this code into the declarations secion of the form:

'-------------------------------------------------------------------------------
Private Sub Form_Load()
    List1.AddItem "xxx"
    List2.AddItem "xxx full path"
    List1.ItemData(List1.NewIndex) = List2.NewIndex
    List1.AddItem "ccc"
    List2.AddItem "ccc full path"
    List1.ItemData(List1.NewIndex) = List2.NewIndex
    List1.AddItem "aaa"
    List2.AddItem "aaa full path"
    List1.ItemData(List1.NewIndex) = List2.NewIndex
End Sub
Private Sub List1_Click()
    MsgBox "Matching item in list 2 is: " + List2.List(List1.ItemData(List1.ListIndex))
End Sub
'-------------------------------------------------------------------------------

5) run the program and click on an item in List1... A messagebox will appear with the correct element from List2


Cheers!
0
kkoserAuthor Commented:
your program wouldn't let me add  anything to the listboxes.
I can populate my listboxes but i can't sort them and still keep them in synch.
0
kkoserAuthor Commented:
ok got it working. Sometimes when you cut and paste from here you have to adjust it.

I still don't understand how to make it work with my app.

Must have a brain cramp...
0
charlescopeCommented:
mcrider gave the correct answer. Use the item data array to sort and keep the index together. If you go to help on MSDN and search on ItemData you will see the example and maybe you will grasp concept.
0
mcriderCommented:
The code I provided was a working example.  In my example, the form load subroutine does all the work...  In the following code List1's SORTED property has been set to TRUE, so:


    List1.AddItem "xxx"
    List2.AddItem "xxx full path"
    List1.ItemData(List1.NewIndex) = List2.NewIndex
   

When list1.additem executes, the list1.newindex is set with the position that the new item was added to in the sorted listbox.  The same thing occurs when you do list2.

ItemData is an indexed property that is assigned to listboxes.  There is one for each item added to the list.  Using the ItemData to store off the NewIndex is how you keep the 2 listboxes in sync.

As long as you add items using the lines to code above, your items will stay in sync.

Cheers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kkoserAuthor Commented:
Private Sub Form_Load()


Dim strFind() As String
Dim lngFound As Long
Dim lngCount As Long

Set objFind = New clsFileFinder

DoEvents
 
 lngFound = objFind.LocateFileLocally("*.avi", strFind)
   If objFind.blnFindAbort Then
    Exit Sub
   End If
 
 For lngCount = 0 To lngFound - 1
 
  List1.AddItem sGetFileName(strFind(lngCount)) 'no path
  List2.AddItem (strFind(lngCount))  'path included
  List1.ItemData(List1.NewIndex) = List2.NewIndex 'line from mcrider
 
 Next

Set objFind = Nothing

End Sub


Function sGetFileName(sFullPath As String) As String
Dim i As Integer
   For i = Len(sFullPath) To 1 Step -1
    If (Mid$(sFullPath, i, 1) = "\") Then Exit For
   Next
   sGetFileName = Mid$(sFullPath, i + 1)
End Function


Thanx mcrider. I understand now how it  keeps it in order. However, it still doesn't work on this app. I don't understand why it isn't working. I pasted the Form_Load and the Function to rid the one listbox of the path.

Could there be a conflict here?
0
mcriderCommented:
In your example above, if list1's SORTED property is true and list2's SORTED property is false, the code should work.

what references have you added to your project?
0
kkoserAuthor Commented:
 Dim colX As Collection
  Dim colY As Collection
  Private objFind As clsFileFinder



i had a whole bunch more, took them out, then the app works like a charm. I will start putting them back in one at a time until it freaks again.



Thanx again for the help. You have enlightened me on listbox controls.
0
mcriderCommented:
Glad I could help!
0
kkoserAuthor Commented:
woa! It still doesn't work. Let me ask another question. I had the sorted property set to false on both listboxes and didn't realize it.
0
mcriderCommented:
Even with the sorted property set to FALSE on both listboxes, this should work.  If the example I gave earlier works on your system, then there is no reason why it should not work in your program.

Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.