hello guys can you help me with my problem
i create a media player and of course it has a playlist which is the listbox but my problem is that when i use this

x = system.io.path.getfilename(track)

and when i click the song in the playlist it does not play the song...

for the song is from C:\blah\this is war.mp3

with this code playlist.selecteditem.add(track) it will output in the playlist(listbox) is the C:\blah\this is war.mp3 and when i click that of course it will play the song

but.....

when i use this

x = system.io.path.getfilename(track)  
 playlist.selecteditem.add(x)

it will output in the playlist(listbox)
this is war.mp3 <-- it remove the c:\blah\
but the problem is that it does not play the song..

Can someone help me with problem?

Dani AI

Generated

Good catch and nice follow-up, and were right that the full path must be kept somewhere. The two-ListBox trick works, but it’s fragile and hard to maintain. A cleaner pattern is to keep one list of objects (or a bound list) where each item carries both the display text and the full path. The UI shows the friendly name while your playback code always uses the stored absolute path.

A minimal, safe pattern (VB.NET) is to store a small class for each track and bind it to the ListBox:

Public Class TrackItem
    Public Property Title As String
    Public Property FullPath As String
    Public Overrides Function ToString() As String
        Return Title
    End Function
End Class

' Build a List(Of TrackItem) and set it as the ListBox DataSource

On selection, read the stored full path and hand that to your player. Always guard against nulls and missing files before attempting playback:

Dim chosenPath As String = TryCast(listBox.SelectedItem?.FullPath, String)
If Not String.IsNullOrEmpty(chosenPath) AndAlso IO.File.Exists(chosenPath) Then
    mediaPlayer.URL = chosenPath   ' or set your player source appropriately
End If

Troubleshooting tips: verify the file actually exists (IO.File.Exists), prefer SelectedIndex synchronization if you must keep two controls (sync indices rather than copying item strings), and watch for permission/lock issues on network drives. If a player rejects local paths with spaces, try passing a file URI (New Uri(path).AbsoluteUri). For richer UIs use DisplayMember/ValueMember or a ListView with a Tag to hold the path. This keeps the UI clean and makes playback logic reliable.

Recommended Answers

All 3 Replies

x = system.io.path.getfilename(track) will only give you the filename.

If you want to display only the filename, but still have access to the full path, then I suggest that you add a second, hidden, ListBox where you store the full path and filename.
And every time that you add another track to the playlist, then at the same time add the full path to the second ListBox using the "track" string.

So, when you click the song in the playlist, the song gets played from the second ListBox.

You could also use a ArrayList to store the FullPath of a file.
.Check out this thread for some basic info on using a ArrayList.

thanks guys but I already solve the problem using two listbox the first listbox use the system.io.path.getfilename and the other one not use and in the selected listbox which use the system.io.path.getfilename when the item is selected it will also select the lsitbox2------> [code] listbox1.selecteditem = listbox2.selecteditem [\code] same idea with you oxiegen:) and also codeorder thank to the link another algorithm:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.