Enabling select from ListBox

Hello experts
I have an application that allows the user to select a file (via a button and openfiledialog) and then play that MP3 file via windows media player. It then stores the selected file in a list box. I want to be enable the user to select a file from the ListBox (which shows all the previously played files) and when a file is selected, for it to be played in the Windows Media Player. A bit like an internet history I can do everything apart from the selecting the file in the ListBox, so that it can become the AxMediaPlayer.Filename (ie file that the media player will play).

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

what exactly do you mean 'select' from the listbox ... how do you have the files stored in the listbox?

i havent worked with mediaplayer (development-wise) ... does it just need a file name or the actual mp3 file/object?

i mean ... you could just

Public Class MyMP3

    Public FilePath as string
    Public File as object
    Public Name as string

    Sub New(path as string)
        Me.FilePath = path
    End Sub

    Public Overrides Function ToString() As String
        Return Me.Name
    End Function

End Class

the class could use tags to pull artist/song info ... and private fields / public properties instead of just public fields ... but ... really, you just need an MP3-Holder class, or ... MP3-Name-And-FilePath-Holder ... or some kindof object to throw into your listbox.  so when the user selects one of the mp3s from the listbox you can ...

Private sub lb_selectedIndexChanges(sender.../e...) Handles ListBox1.SelectedIndexChanged
If Listbox1.SelectedIndices.Count < 1 then Exit Sub
Dim idx as integer = Listbox1.SelectedIndices.Item(0)
Dim mp3 as MyMP3 = CType(Listbox1.Items(0), MyMP3)

'// if AxMediaPlayer.Filename is the file path then ...
AxMediaPlayer.Filename = mp3.FileName

End Sub

... something like that?  Not sure exactly what you're having a problem with :/
sebastizAuthor Commented:

It is basically the ability to select a filename in the Listbox, and when it is clicked, get the windows media player to play that selected file. The windows media player only needs the filename to play it.
Sample to select a listbox item....


Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.ListBox1.Location = New System.Drawing.Point(28, 36)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(224, 134)
        Me.ListBox1.TabIndex = 0
        Me.Button1.Location = New System.Drawing.Point(28, 216)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Next"
        Me.Label1.Location = New System.Drawing.Point(32, 184)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(56, 16)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "Selected:"
        Me.TextBox1.Location = New System.Drawing.Point(104, 180)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 3
        Me.TextBox1.Text = ""
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Name = "Form1"
        Me.Text = "frmSelect"

    End Sub

#End Region
    Private Sub frmSelectNext_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = ListBox1.Text
    End Sub
    'OR USE
    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
        TextBox1.Text = ListBox1.Text
    End Sub
End Class
Fernando SotoRetiredCommented:
   Private Sub ListBox1_DoubleClick(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles ListBox1.DoubleClick

        Dim FileName As String

        FileName = ListBox1.SelectedItem.ToString()
        ' Then use FileName to send it to MideaPlayer

    End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

all you needed was the .ToString() function ... ???


[quote] ' Then use FileName to send it to MideaPlayer [/quote]

isn't a big step up from

'// if AxMediaPlayer.Filename is the file path then ...
AxMediaPlayer.Filename = mp3.FileName

but ... okay.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.