Pregunta

I've been having a problem with the following code:

namespace Viewer
{
    public partial class Form1 : Form
    {
        int count = 0;
        LinkLabel[] linkLabel = new LinkLabel[200];
        string filename;
        string extension;
        string filepath;

        private void btnLoad_Click(object sender, EventArgs e)
        {
            // Creates a Directory for the Movies Folder
            DirectoryInfo myDirectory = new DirectoryInfo(@"C:\Users\User\Movies");

            // Creates a list of "File info" objects
            List<FileInfo> ls = new List<FileInfo>();

            // Adds filetypes to the list
            ls.AddRange(myDirectory.GetFiles("*.mp4"));
            ls.AddRange(myDirectory.GetFiles("*.avi"));

            // Orders the list by Name
            List<FileInfo> orderedList = ls.OrderBy(x => x.Name).ToList();

            // Loop through file list to act on each item
            foreach (FileInfo filFile in orderedList)
            {
                // Creates a new link label
                linkLabel[count] = new LinkLabel();

                // Alters name info for display and file calling
                filepath = filFile.FullName;
                extension = filFile.Extension;
                filename = filFile.Name.Remove(filFile.Name.Length - extension.Length);

                // Write to the textbox for functional display
                textBox1.AppendText(filename + "\r\n");

                // Alters link label settings
                linkLabel[count].Text = filename;
                linkLabel[count].Links.Add(0, linkLabel[count].Text.ToString().Length, filepath);
                linkLabel[count].LinkClicked += new LinkLabelLinkClickedEventHandler(LinkedLabelClicked);

                // Adds link label to table display
                tblDisplay.Controls.Add(linkLabel[count]);

                // Indexes count up for arrays
                count = count + 1;
            }
        }

        private void LinkedLabelClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(filepath);
        }

    }
}

My goal is to generate a table of links to all of the media files that I add at launch, and have the links open the files in their respective players.

As of right now, it generates all of the links properly, but whenever I click on any of them, it launches the last item in the list.

For example, if the list contains "300", "Gladiator", and "Top Gun", no matter which link I click, it opens "Top Gun".

I assume that this has to do with it calling the variable "filepath" in the click event, which is left in it's final state. However, I'm not exactly clear on how to create a static link value or action on each individual link, as all of the answers I've researched are in regards to single linklabel situations, not dynamic set-ups.

Any help/advice would be appreciated!

¿Fue útil?

Solución

Try as below:
In foreach loop add one line more like:

linkLabel[count].Tag = filepath;

then in click event get this path as blow,

private void LinkedLabelClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    string filepath = ((LinkLabel)sender).Tag.tostring();
    System.Diagnostics.Process.Start(filepath);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top