Add a string property to a visual c# linklabel?
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 wo开发者_开发知识库rk
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
Use Tag
property, then it can be retrieved by casting first parameter of LinkClick
(object sender
) to LinkLabel
:
mylinklabel.Tag = line;
in LinkClick
:
((LinkLabel)sender).Tag
Store full path in LinkLabel
Tag
Property, you could get the full path like
string full path = myLinkLabel.Tag.ToString();
Hope this help.
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:
<Programs>
<Program Name="Calculator" Path="calc">
<Program Name="Notepad" Path="C:\blabla">
</Programs>
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.
精彩评论