Link to home
Start Free TrialLog in
Avatar of kkoser
kkoser

asked on

removing the path in a listbox.

My app searches the drive for specific files then displays them in a listbox.(The whole path). The user then selects from there. What I would like to do is display only the filename but have the program remember where the file is located for them to use. I suspect that I could have 2 listboxes displaying the same info, but one not visible that includes the path.

My question is how can I display only the filenames and not the path too?
ASKER CERTIFIED SOLUTION
Avatar of Vbmaster
Vbmaster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vbmaster
Vbmaster

Hmm change the line..

  sGetFileName = Mid$(sFullPath, i + 1, Len(sFullPath) - i)

...to..

  sGetFileName = Mid$(sFullPath, i + 1)

No need for that last parameter.
Avatar of kkoser

ASKER

how do I make the listbox display only the filename using your function.



I use this line to add filenames to the listbox now.



List1.AddItem strFind(lngCount)



do I use your function before or after this?
As I read it you were gonna have two textboxes, one visible with only filenames and one invisible with full pathname, right?

If this is so you can just add

List2.Additem sGetFileName(strFind(lngCount))

after or before that line of yours. Then you just hide the List1 by setting it's Visible property to False.
Avatar of kkoser

ASKER

It works like a charm. Now I can shrink the Listbox for a little more room.

Can I ask how to synchronize the 2 listboxes properly here, or should i ask a different question?

Ican bump up the points some...
Watch out for the .Sorted property. If your listboxes are sorted then this won't work.

You can also get good results with a sorted list box if you just use a string array. Keep expanding the array with REDIM PRESERVE when you add an item to the list. Then you set the List1.ListData property to the current index of the array. When you read back out, look at the .ItemData property of the .Selected item and then use that as your array index. This will give you the proper file.

This sounds complex but really isn't. It allows you to have your list sorted (helpful with just filenames) and still keep the full path where you need it.

DIM Array() AS String
DIM N AS Integer
N = 0

Saving becomes:

List1.Additem FileName
N = N + 1
REDIM Preserve Array(N)
Array(N) = FilePath
List1.ItemData(List1.NewIndex) = N

Lookup becomes something like:

FilePath = Array(List1.ItemData(List1.ListIndex))

Use something like VBmasters code to separate out the filename from the path.
            
M
Avatar of kkoser

ASKER

I forgot. Mark, Did you give me the Drive Search code that i am using now?
What do you mean with synchronizing the two listboxes? I thought they were rather synchronized ;)

What do you mean with synchronizing the two listboxes? I thought they were rather synchronized ;)

Avatar of kkoser

ASKER

Must be an echo in here.:)

An example would be that if i had selected an item from one listbox, it would update the other listbox to the same selection.
No, there's no echo it's just me being a little schizophrenic.

But I don't understand WHY you would want to do that, since that other list is invisible.. No need to make it slower than it already is..

(The code would be, if you by some good reason (??) would wanna do this, List2.Listindex = List1.Listindex in the List1_Click event )

Avatar of kkoser

ASKER

Not slow at all. Working awesome. All I am doing is letting the user select a .avi and decide whether or not they want to play it.

Small program, that's all. But it helps me learn. Thanx.

Avatar of kkoser

ASKER

How do i do the same thing but with one filename in a textbox?
Avatar of kkoser

ASKER

you're right mark2150. When i changed the .sorted property to True, it didn't stay in synch. I don't understand how to apply your code. When i tried to paste,

DIM Array() AS String

It said it was looking for an expected identifier.

Where does this go?

 DIM Array() AS String
 DIM N AS Integer
 N = 0



 Saving becomes:
 
Where do i put this?

  List1.Additem FileName
  N = N + 1
  REDIM Preserve Array(N)
  Array(N) = FilePath
  List1.ItemData(List1.NewIndex) = N

  Lookup becomes something like:

  FilePath = Array(List1.ItemData(List1.ListIndex))
It probably doesn't like the word ARRAY as a variable. It's a *placeholder* for the array name that you would use in your code. As are FilePath and FileName. These tend to be reserved words, but the intent is to show *what* goes where instead of actual names. Kind of {Insert your array name here} but that's too much to type.

In effect your using a 2nd (invisible) list box as the storage array for the full paths. That's fine, if a little inefficient since you don't need all the other bells and whistles that a list box comes with as you're just treating it as an array.

In my example we go ahead and create an array (perhaps MyArray would have been a better choice in the example...) to hold the info.

The reason that the .Sorted property screws everything up in the first example is that the paths & files order differently that just the files alone. If you have say two files:

C:\DirA\Z.AVI
C:\DirB\A.AVI

Then the "path" list will sort in the order shown but the "file" list will reverse 'em. Oops!

Anyway, replace the Array() with, say, MyArray() and likewise rename the FilePath and FileName vars and it should work.

Sorry for such sloppy work on my part but it was late and I was more concerned about illustrating the concept than the actual code.

M
Avatar of kkoser

ASKER

At Load it looks like this:

Where do I put your code?
Do I have to have the listboxes' .sorted property to False?



Private Sub Form_Load()

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

Set objFind = New clsFileFinder

Me.MousePointer = vbHourglass
DoEvents
Command1.Enabled = False
lngFound = objFind.LocateFileLocally("*.mp3", 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))
    List2.AddItem strFind(lngCount)
Next

Set objFind = Nothing

End Sub





Avatar of kkoser

ASKER

the *.mp3 files have been changed to *.avi.  Sorry about that.
Avatar of kkoser

ASKER

o.k..... I think i got it to work. a little. It searches the hard drive for files then lists only 1 file in the listbox. 27 times. Which is how many .avi files are on the drive.

What am i doing wrong?
Avatar of kkoser

ASKER

Private Sub Form_Load()

Dim strFind() As String
Dim lngFound As Long
Dim lngCount As Long
Dim MyArray() As String
Dim N As Integer
N = 0

'Lookup becomes something like:
'FilePath = MyArray(List1.ItemData(List1.ListIndex))

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
'Saving becomes:
List1.AddItem sGetFileName(strFind(longCount))
N = N + 1
ReDim Preserve MyArray(N)
MyArray(N) = FilePath
List1.ItemData(List1.NewIndex) = N

List2.AddItem (strFind(longCount))
N = N + 1
ReDim Preserve MyArray(N)
MyArray(N) = FilePath
List2.ItemData(List2.NewIndex) = N

''    List1.AddItem sGetFileName(strFind(lngCount))
''    List2.AddItem strFind(lngCount)
Next

Set objFind = Nothing

End Sub
Avatar of kkoser

ASKER

This line gives me an ivalid use of an array error message.



FilePath = MyArray(List1.ItemData(List1.ListIndex))
Check to see that List1.ListIndex is =>0 or else no item has been selected. If this is the case it'll return -1 and that makes the array refernce go TILT!

M