我完全不熟悉Visual C#。虽然我可以管理控制台应用程序,但在编写表单时我很容易迷失方向。将字符串属性添加到可视化C#链接标签?
我目前正在制作一个逐行读取文本文件的“应用程序启动器”。每一行都是我个人电脑上某个有用程序的路径。文本文件中的每个路径(即每行)都会自动创建一个链接标签。
我希望链接标签的.Text属性是路径的缩写形式(即只是文件名,而不是整个路径)。我已经发现如何以这种方式缩短字符串(到目前为止这么好!)
但是,我还想将某个地方的完整路径存储 - 因为这是我的linklabel需要链接到的地方。在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();
}
所以,我怎么能存储一部全路径作为一个字符串里面的链接标签用于我以后的onclick函数?
非常感谢提前
吉姆