Media Player c# -'Index was outside the bounds of the array.' for listbox c#

Hemanth B 886 Reputation points
2021-07-15T14:40:54.44+00:00

Hi, I have created a media player in C#. This is the code for open Audio:
This opens and adds the audio file name and path to a list box
private void OpenAudio()
{

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                axWindowsMediaPlayer1.Ctlcontrols.stop();
                file = ofd.SafeFileNames;
                paths = ofd.FileNames;
                for (int i = 0; i < file.Length; i++)
                {
                    this.Width = 883;
                    listBox1.Items.Add(file[i]);
                    listBox1.SelectedIndex = -1;
                }
            }
        }

Now this is the code when user double clicks an item:
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
button3.Visible = false;
button1.Visible = true;

                axWindowsMediaPlayer1.URL = paths[listBox1.SelectedIndex];

        }

This code actually works perfectly when I firstly import a file.
Meaning:
If I import a file without previously importing any other previous files into the listbox, it plays the audio smoothly.
But when I add another file, with files already present in the listbox and then double click that new added file/ listbox item,
It throws this exception: 'Index was outside the bounds of the array.'.
Please help

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,906 questions
{count} votes

Accepted answer
  1. Viorel 117.1K Reputation points
    2021-07-15T15:09:58.78+00:00

    Try replacing 'paths = ofd.FileNames' with something like this:

    paths = ( paths ?? Enumerable.Empty<string>( ) ).Concat( ofd.FileNames ).ToArray( );


1 additional answer

Sort by: Most helpful
  1. Anonymous
    2021-07-15T15:13:28.533+00:00

    Hello,

    Try this:

            private List<String> Files = new List<string> { };
            private List<String> Paths = new List<string> { };
    
            private void OpenAudio()
            {
    
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Multiselect = true;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    axWindowsMediaPlayer1.Ctlcontrols.stop();
                    for (int i = 0; i < ofd.FileNames.Length; i++)
                    {
                        this.Width = 883;
                        Files.Add(System.IO.Path.GetFileNameWithoutExtension(ofd.FileNames[i]));
                        Paths.Add(ofd.FileNames[i]);
    
                        listBox1.Items.Add(Files[i]);
                        listBox1.SelectedIndex = -1;
                    }
                }
            }
            private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                button3.Visible = false;
                button1.Visible = true;
                axWindowsMediaPlayer1.URL = Paths[listBox1.SelectedIndex];
            }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.