How to fix this IndexOutOfBoundsException

Posted by Rida on Stack Overflow See other posts from Stack Overflow or by Rida
Published on 2012-09-08T14:36:05Z Indexed on 2012/09/08 15:38 UTC
Read the original article Hit count: 198

Filed under:

My program runs fine, but there is a little problem. When I add new tracks previously existing file to the ListBox, the program experiences an error. The code seems unwilling to do looping in a new file which was added in a different time. Please help me. Thanks ....

public partial class Form1 : Form
{
    //...

    string[] files, paths;

    private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            files = openFileDialog1.SafeFileNames;
            paths = openFileDialog1.FileNames;
            for (int i = 0; i < files.Length - 1; i++)
            {
                listBox1.Items.Add(files[i]);
            }
        }
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        axWindowsMediaPlayer1.URL = paths[listBox1.SelectedIndex];
    }
}

© Stack Overflow or respective owner

Related posts about c#