我对视觉 C# 完全陌生。虽然我可以管理控制台应用程序,但在编写表单时我很容易迷失方向。
我目前正在制作一个“应用程序启动器”,它逐行读取文本文件。每一行都是通往我电脑上其他地方的有用程序的路径。自动为文本文件中的每个路径(即每一行)制作一个链接标签。
我希望链接标签的 .Text 属性是路径的缩写形式(即只是文件名,而不是整个路径)。我发现了如何以这种方式缩短字符串(到目前为止一切都很好!)
但是,我还想将完整路径存储在某处 - 因为这是我的链接标签需要链接到的内容。在Javascript中,我几乎可以像这样将这个属性添加到linklabel:mylinklabel.fullpath=line; (其中 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 函数中使用?
提前谢谢了
吉姆