OpenFileDialog Help

Hi, I'm trying to make a media player in vb.net. What I'm having trouble with is selecting music to add to a listbox. What I've tried to do is:

        OpenFileDialog1.Filter = "Audio Files (*.mp3)|*.mp3|" & "Audio Files (*wav)|*.wav|" & "All files|*.*"

        OpenFileDialog1.InitialDirectory = System.Environment.CurrentDirectory

        OpenFileDialog1.FilterIndex = 1

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

            lstMusic.Items.Add(OpenFileDialog1.FileNames)


        End If

Open in new window


Every time I added a song it added only 1 song saying System.String[] how do I fix this?

Help will be appreciated
Jordy1212Asked:
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.

rockiroadsCommented:
this line

            lstMusic.Items.Add(OpenFileDialog1.FileNames)

is referring to the list of filenames. It returns the string array

For a single file selection drop the s

   lstMusic.Items.Add(OpenFileDialog1.FileName)
rockiroadsCommented:
How to use the string array

            For Each f In OpenFileDialog1.FileNames
                lstMusic.Items.Add(f)
            Next

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
trestanCommented:
OpenFileDialog1.FileNames is an array, you need to get the value of one element from the array. Or. you can use OpenFileDialog1.FileName, which contains only one file path.

You need to loop through the array to add the string value into the listbox one by one.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

trestanCommented:
OpenFileDialog1.FileNames is an array, you need to get a value of one element from the array using array index. Or. you can use OpenFileDialog1.FileName, which contains only one file path.

You need to loop through the array to add the string values into the listbox one by one.
rockiroadsCommented:
sorry, that was vb.net posted code. here is the c# equivalent

foreach (f in OpenFileDialog1.FileNames) {
      lstMusic.Items.Add(f);
}

trestanCommented:
Sorry for the redundant submit. Error happened and I thought it's not through.
Jordy1212Author Commented:
Thanks!
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
C#

From novice to tech pro — start learning today.