kkoser
asked on
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?
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?
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
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
ASKER
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(lngCo unt)) '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
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(
If objFind.blnFindAbort Then
Exit Sub
End If
Command1.Enabled = True
Me.MousePointer = vbDefault
For lngCount = 0 To lngFound - 1
List1.AddItem sGetFileName(strFind(lngCo
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
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.NewIn dex) = List2.NewIndex
List1.AddItem "ccc"
List2.AddItem "ccc full path"
List1.ItemData(List1.NewIn dex) = List2.NewIndex
List1.AddItem "aaa"
List2.AddItem "aaa full path"
List1.ItemData(List1.NewIn dex) = List2.NewIndex
End Sub
Private Sub List1_Click()
MsgBox "Matching item in list 2 is: " + List2.List(List1.ItemData( List1.List Index))
End Sub
'------------------------- ---------- ---------- ---------- ---------- ---------- ----
5) run the program and click on an item in List1... A messagebox will appear with the correct element from List2
Cheers!
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.NewIn
List1.AddItem "ccc"
List2.AddItem "ccc full path"
List1.ItemData(List1.NewIn
List1.AddItem "aaa"
List2.AddItem "aaa full path"
List1.ItemData(List1.NewIn
End Sub
Private Sub List1_Click()
MsgBox "Matching item in list 2 is: " + List2.List(List1.ItemData(
End Sub
'-------------------------
5) run the program and click on an item in List1... A messagebox will appear with the correct element from List2
Cheers!
ASKER
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.
I can populate my listboxes but i can't sort them and still keep them in synch.
ASKER
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...
I still don't understand how to make it work with my app.
Must have a brain cramp...
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(lngCo unt)) 'no path
List2.AddItem (strFind(lngCount)) 'path included
List1.ItemData(List1.NewIn dex) = 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?
Dim strFind() As String
Dim lngFound As Long
Dim lngCount As Long
Set objFind = New clsFileFinder
DoEvents
lngFound = objFind.LocateFileLocally(
If objFind.blnFindAbort Then
Exit Sub
End If
For lngCount = 0 To lngFound - 1
List1.AddItem sGetFileName(strFind(lngCo
List2.AddItem (strFind(lngCount)) 'path included
List1.ItemData(List1.NewIn
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?
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?
what references have you added to your project?
ASKER
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.
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.
Glad I could help!
ASKER
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.
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!
Cheers!
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.