?
Solved

Two listboxes with the same info. (Almost).

Posted on 1999-10-30
14
Medium Priority
?
165 Views
Last Modified: 2010-05-02
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?


0
Comment
Question by:kkoser
14 Comments
 
LVL 4

Expert Comment

by:Juilette
ID: 2172447
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
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 2172522
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
 

Author Comment

by:kkoser
ID: 2172540
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 14

Expert Comment

by:mcrider
ID: 2172577
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
 

Author Comment

by:kkoser
ID: 2172924
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
 

Author Comment

by:kkoser
ID: 2173013
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
 
LVL 2

Expert Comment

by:charlescope
ID: 2173205
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
 
LVL 14

Accepted Solution

by:
mcrider earned 160 total points
ID: 2173694
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
 

Author Comment

by:kkoser
ID: 2175643
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
 
LVL 14

Expert Comment

by:mcrider
ID: 2178152
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
 

Author Comment

by:kkoser
ID: 2178389
 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
 
LVL 14

Expert Comment

by:mcrider
ID: 2179523
Glad I could help!
0
 

Author Comment

by:kkoser
ID: 2181576
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
 
LVL 14

Expert Comment

by:mcrider
ID: 2184452
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month4 days, 5 hours left to enroll

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question