# How do i use OpenFileDialog in vb 2005



## biddog989 (Jan 21, 2008)

Hi,
I have been playing around with the Windows Media Player component in visual basics 2005 on my XP pro computer. I created a listbox that was set up to look in a specific place when the form was loaded for mp3 files and when the user click on a file in the listbox it would play that file in the media player.

What I am now looking for is a way the user can browse files on their computer and add the files they want to the listbox. From what I have read off the net, I need to use the "OpenFileDialog" code but I have no idea how to do so.

Please note Im an extreme beginner so you may have to spell the whole thing out for me 

I have attached my vb 2005 files compressed using rar so you can see what Im talking about, if you any suggestions you are more then welcome to share them. 
Thanks


----------



## artur02 (Sep 24, 2007)

You can read a description here: http://msdn2.microsoft.com/en-us/library/aa984392(VS.71).aspx

Take the following steps:

 Create a variable for OpenFileDialog
 Set members
 Call ShowDialog() and check for DialogResult, it should be OK
 Read FileName property for file path
The details are in the article linked above.


----------



## biddog989 (Jan 21, 2008)

Thanks, I had a look at that website and I got some of the code working, as I said before Im very new to visual basic so the steps you listed made nearly no sense to me but I did come out with this code for my button....

Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Mp3 Files|*.mp3"
openFileDialog1.Title = "Select a Mp3 File"
If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
ListBox1.Items.Add(openFileDialog1.FileName)
End If
End Sub

I know Ive probably go very bad code any suggestions welcome

The button does let me select files to add to my listbox but the problem I have now is that I can only add one file at a time, is there a way that I can make it let me select more then one file at a time, remembering that each files source and name must be shown in the listbox


----------



## artur02 (Sep 24, 2007)

You should set *Multiselect* property to true. You can access the selected file names reading *FileNames* property. Read the articles below.

Multiselect: http://msdn2.microsoft.com/en-us/library/system.windows.forms.openfiledialog.multiselect.aspx
Filenames: http://msdn2.microsoft.com/en-us/library/system.windows.forms.filedialog.filenames.aspx


----------



## biddog989 (Jan 21, 2008)

Thanks again.

Got that working great, my last problem is trying to get the player to automatically go to the next song in the list box when the current song has finished playing. I got the listbox to move to the next song but the song doesnt start playing, media player shows "opening media" but never plays

Have a look at my new code to see what I mean.


----------

