Search code examples
c#hyperlinktabpage

Making a string clickable in the list inside tabpage


I'm working on a tab page. Inside the tab page, there is a list that collects items to be displayed (it is attached to the tab page). I'm trying to make the files that are added into the list clickable then open up the directory where they are in.

I have tried two different ways. I can't get them to work.

Method one- create a linklabel and attach System.Diagnostics.Process.Start("@"path"); then add that linklabel into my list.

Method two- simply using string and make the string clickable.

This is example code for method one which I modified using Microsoft example to demonstrate what I need - The problem with this is not only it doesn't work and it only sets one item to be clickable.

public Form1()
{
    this.linkLabel1 = new System.Windows.Forms.LinkLabel();

    this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);

    this.linkLabel1.Text = "Open folder";

    //Assume the list called ItemList is declared properly above
    //ItemList is attached to the tab page and whatever in it will be displayed item by item in seperated line.
    //I need to make them clickable.
    ItemList.Add(linkLabel1);
}

private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
    // Navigate to a URL.
    System.Diagnostics.Process.Start("@"path");
}

Method two -

string item1 = "file.txt";
fullPath = Path.GetFullPath(item1);
string Link = String.Format("<a href=\"{0}\">Click here</a>", fullPath);
ItemList.Add(Link);

Problem with method two is that it simply displays the whole link starting with a href....

I may have other work around but assume I must get one of these way to work.. Please help, thank you.


Solution

  • You can't make values in a list or a string clickable, clickable can be only controls that the user can see on his screen. You have to rethink your approach: what the user will see, what will he click and what do you want to happen at that moment.

    Also you don't need hyperlinks at all for opening files or folders on local machine, so probably LinkLabel isn't the best choice. Maybe you should better take something more appropriate like ListBox to list your filenames.