将字符串属性添加到visual c#linklabel?

use*_*904 1 c# linklabel

我对视觉C#完全陌生.虽然我可以管理控制台应用程序,但在编码表单方面我很容易迷失方向.

我目前正在制作一个"app launcher",它逐行读取文本文件.每一行都是我电脑上其他地方有用程序的路径.为文本文件中的每个路径(即每一行)自动生成一个链接标签.

我希望链接标签的.Text属性是路径的缩写形式(即只是文件名,而不是整个路径).我已经找到了如何以这种方式缩短字符串(到目前为止这么好!)

但是,我还希望将完整路径存储在某个地方 - 因为这是我的链接标签需要链接到的地方.在Javascript中,我几乎可以将此属性添加到linklabel,如下所示:mylinklabel.fullpath = line; (其中行是因为我们通过文本文件读取当前行,FULLPATH是我的"定制"的属性,我想尝试,并添加到链接标签.我想这需要申报,但我不知道怎么样.

下面是我创建表单的代码部分,逐行读取文本文件并为每行上找到的路径创建链接标签:

private void Form1_Load(object sender, EventArgs e)   //on form load
    {
        //System.Console.WriteLine("hello!");
        int counter = 0;
        string line;
        string filenameNoExtension;
        string myfile = @"c:\\users\jim\desktop\file.txt";

        //string filenameNoExtension = Path.GetFileNameWithoutExtension(myfile);


        // Read the file and display it line by line.
        System.IO.StreamReader file = new System.IO.StreamReader(myfile);
        while ((line = file.ReadLine()) != null)
        {
            //MessageBox.Show(line);   //check whats on each line


            LinkLabel mylinklabel = new LinkLabel(); 
            filenameNoExtension = Path.GetFileNameWithoutExtension(line);  //shortens the path to just the file name without extension
            mylinklabel.Text = filenameNoExtension;
            //string fullpath=line;        //doesn't work
            //mylinklabel.fullpath=line;   //doesn't work
            mylinklabel.Text = filenameNoExtension;  //displays the shortened path
            this.Controls.Add(mylinklabel);
            mylinklabel.Location = new Point(0, 30 + counter * 30);
            mylinklabel.AutoSize = true;
            mylinklabel.VisitedLinkColor = System.Drawing.Color.White;
            mylinklabel.LinkColor = System.Drawing.Color.White;



            mylinklabel.Click += new System.EventHandler(LinkClick);


            counter++;
        }

        file.Close();

    }
Run Code Online (Sandbox Code Playgroud)

那么,如何将完整路径作为字符串存储在linklabel中,以便稍后在我的onclick函数中使用?

提前谢谢了

吉姆

And*_*rey 5

使用Tag属性,然后可以通过将LinkClick(object sender)的第一个参数转换为LinkLabel:

mylinklabel.Tag = line;
Run Code Online (Sandbox Code Playgroud)

LinkClick:

((LinkLabel)sender).Tag
Run Code Online (Sandbox Code Playgroud)