将字符串属性添加到视觉 C# 链接标签?
我对 Visual 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 函数稍后
再说
吗?
I am totally new to visual C#. Whilst I can sort of manage console apps, I easily get lost when it comes to coding forms.
I am currently making an "app launcher" which reads a text file line by line. Each line is a path to a useful program somewhere else on my pc. A link label is automatically made for each path (i.e. each line) in the text file.
I would like the .Text property of the link label to be an abbreviated form of the path (i.e. just the file name, not the whole path). I have found out how to shorten the string in this way (so far so good !)
However, I would also like to store the full path somewhere - as this is what my linklabel will need to link to. In Javascript I could pretty much just add this property to linklabel like so: mylinklabel.fullpath=line; (where line is the current line as we read through the text file, and fullpath is my "custom" property that I would like to try and add to the link label. I guess it needs declaring, but I am not sure how.
Below is the part of my code which creates the form, reads the text file line by line and creates a link label for the path found on each line:
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();
}
So, how can I store a full path as a string inside the linklabel for use in my onclick function later on?
Many thanks in advance
Jim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
Tag
属性,然后可以通过将LinkClick
(object sender
) 的第一个参数转换为LinkLabel
来检索它:在
LinkClick
中:Use
Tag
property, then it can be retrieved by casting first parameter ofLinkClick
(object sender
) toLinkLabel
:in
LinkClick
:将完整路径存储在
LinkLabel
Tag
属性中,您可以获得像希望这样的帮助的完整路径。
Store full path in
LinkLabel
Tag
Property, you could get the full path likeHope this help.
从文本文件中读取并不是很好。您可以从 xml 文件中读取内容,然后创建链接标签和其他内容将非常简单。一个 xml 示例:
然后您可以创建一个名称变量和一个路径变量并从文件中加载值。但如果你是初学者,那么 txt 文件也可以,但从文件中加载每一行的值很痛苦。
Reading from a text file isn't pretty good. You could read from a xml file, then it would be very simple to create the linklabels and other stuff. A xml sample:
Then you could make a name variable, and a path variable and load the values from the file. But if your a beginner, then a txt file will also do, but it's a pain to load each line's values from the file.