简体   繁体   English

动态将LinkLabels添加到TableLayoutPanel

[英]Dynamically adding LinkLabels to a TableLayoutPanel

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". 例如,如果列表包含“ 300”,“角斗士”和“壮志凌云”,则无论我单击哪个链接,都将打开“壮志凌云”。

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. 我认为这与在click事件中调用变量“ filepath”有关,该事件处于最终状态。 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! 任何帮助/建议将不胜感激!

Try as below: 尝试如下:
In foreach loop add one line more like: 在foreach循环中,再添加一行,例如:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM