I am brand new to Visual Studio so please forgive my current lack of knowledge but I've tried searching everywhere for this and am relatively surprised that I couldn't find something about this elsewhere. I am using a currently using an Openfiledialogue with multi-file selection allowed. It then adds the files into a ListBox. I plan on eventually switching to going with FolderBrowserDialogue but that should not change what I am trying to find out now.

I use SafeFileNames (as shown below) to print only the filename and file extension to the listbox as using FileNames looked horrible in the list box as it included the path to the file. So this makes the list of files (Videos in this case) much more compact and neater. However, what I am creating is supposed to allow you to play the file that you have selected in the listbox but I cannot get it to do so with SafeFileNames because the selected item no longer 'sends' the location (in this case path location) to the player. So now my files will not play.

    Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click

        OpenFileDialog1.ShowDialog()
        If Windows.Forms.DialogResult.OK Then
            For Each track As String In OpenFileDialog1.SafeFileNames
                ListBox1.Items.Add(track)
            Next
        End If
    End Sub

The result of FileNames would be like:
C:/Users/Me/Videos/Videos_From_Various_Vacations/kayak.mp4
C:/Users/Me/Videos/Videos_From_Various_Concerts/disturbed_bootleg.mp4
C:/Users/Me/Videos/Videos_From_Various_Concerets/rolling_stones.avi

The result of SafeFileNames:
kayak.mp4
disturbed_bootleg.mp4
rolling_stones.avi

SafeFileNames makes the list look exactly how I want it to (meaning that I don't need a really long list box for a user to be able to the actual file after the path) but then the control doesn't know the location of the file with that name.

    Private Sub button2_Click(sender As Object, e As EventArgs) Handles button2.Click
        AxWindowsMediaPlayer1.URL = listbox1.SelectedItem
    End Sub

I was thinking of appending the path to the filename and extension that end up getting sent to the "play" button which tells WMP to play the selected file. But with the videos potentially residing in many different folders all over a user's computer, I am not sure that would work so well.

If anyone has any ideas as to how, after a user has added files from different folders on their system, that the listbox will still show only the filename and extension but the button 2 code (for the Start) button will know the location of the file to be played...I would be ever so grateful!

You can use an array to save the list of all selected file names with full path and show only the file name to the listbox.

Here I just creae a structure to hold the filename with full path and it can return the filename to the listbox. The structure is

Structure SelectedFile
        Dim fileName As String

        Public Function PlayFile() As String
            'Function returns file name with full path
            Return fileName
        End Function

        Public Function GetFileName() As String
            'Function returns only the file name
            Dim nmstr() As String

            nmstr = Split(fileName, "\")

            Return nmstr(nmstr.GetUpperBound(0))
        End Function
    End Structure

Now you have to declare a form lavel dynamic array.

Dim OpenFileNames() As SelectedFile

Nowyou can use it to hold and return the file name only or with full path.
The full codes are

Public Class Form1

    'Creating a structure
    Structure SelectedFile
        Dim fileName As String

        Public Function PlayFile() As String
            'Function returns file name with full path
            Return fileName
        End Function

        Public Function GetFileName() As String
            'Function returns only the file name
            Dim nmstr() As String

            nmstr = Split(fileName, "\")

            Return nmstr(nmstr.GetUpperBound(0))
        End Function
    End Structure

    'Declaring arrays and variables
    Dim OpenFileNames() As SelectedFile
    Dim i As Integer = 0

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.Filter = "All Files|*.*"
        OpenFileDialog1.Multiselect = True


        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            For Each track As String In OpenFileDialog1.FileNames

                'Redimensioning the Array and hold the filename with path
                ReDim Preserve OpenFileNames(i)
                OpenFileNames(i).fileName = track

                'Return only filename to the listbox
                ListBox1.Items.Add(OpenFileNames(i).GetFileName())

                'incrementing the value of i
                i += 1
            Next

        End If



    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        'Making loops for continuous playing
        For i = 0 To ListBox1.Items.Count - 1
            AxWindowsMediaPlayer1.URL=OpenFileNames(i).PlayFile()
        Next
    End Sub
 End Class

If you want to play only the selectedfiles from the listbox, you can use this

 Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            'playing selected file
                AxWindowsMediaPlayer1.URL=OpenFileNames(ListBox1.SelectedIndex).PlayFile()
 End Sub

Hope it can help you.

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.