Link to home
Start Free TrialLog in
Avatar of Jordy1212
Jordy1212

asked on

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
Avatar of rockiroads
rockiroads
Flag of United States of America image

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)
ASKER CERTIFIED SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
sorry, that was vb.net posted code. here is the c# equivalent

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

Sorry for the redundant submit. Error happened and I thought it's not through.
Avatar of Jordy1212
Jordy1212

ASKER

Thanks!