Dear all

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\matt\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

Matt

All controls have a Tag property which is there for you to put whatever you want in it. Store the full path there and you are golden.

Momerath

Such a simple answer which totally solves the problem for me. I feel embarassed !

(told you I was new to this game !)

Cheers mate

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.