Search code examples
c#windows-explorer

Linking words to windows Explorer c#


Question: How can I make a list of link buttons so that when a user clicks on a link it opens it up in windows explorer.

So I have a rich text box that contains a list of all folder names and I have a list of all the folder paths. I want to be able to click on the folder name and have it linked using the path to the correct folder in windows explorer.

                LinkLabel link = new LinkLabel();
                link.Text = transfer2;
                //link.Text = "something";
                link.Name = dirName;
                link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
                LinkLabel.Link data = new LinkLabel.Link();
                data.LinkData = @"C:\";
                link.Links.Add(data);
                link.AutoSize = true;
                link.Location =
                this.Display_Rich_Text_Box.GetPositionFromCharIndex(this.Display_Rich_Text_Box.TextLength);
                this.Display_Rich_Text_Box.Controls.Add(link);
                this.Display_Rich_Text_Box.AppendText(link.Text + "   ");
                this.Display_Rich_Text_Box.SelectionStart = this.Display_Rich_Text_Box.TextLength;

I started with this code. I am using a foreach statement to get the folder names and path. I tried to change the name of link so it will appear to the user that they are clicking on folder A, but when you click on folder A it uses the path to open the windows explorer where the folder is from.

Any ideas or help would be greatly appreciated.

Update

I changed the code a little so now it will display, but I cant scroll down. It appears to be only on the surface as I ran something in the rich textbox and it was scrollable while the link stayed on the surface.

enter image description here I also added a picture so you can see what the problem is. I scrolled a little bit so it would b easy to see.

                LinkLabel link = new LinkLabel();
                link.Text = dirName;
                //link.Text = "something";
                link.Name = transfer2;
                //link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
                LinkLabel.Link data = new LinkLabel.Link();
                data.LinkData = @"C:\";
                link.Links.Add(data);
                link.AutoSize = true;
                link.Location =
                this.Display_Rich_Text_Box.GetPositionFromCharIndex(this.Display_Rich_Text_Box.TextLength);
                this.Display_Rich_Text_Box.Controls.Add(link);
                this.Display_Rich_Text_Box.AppendText(link.Text + "\n");
                this.Display_Rich_Text_Box.SelectionStart = this.Display_Rich_Text_Box.TextLength;

Update: I am trying to make essentially a list of hyperlinks so I don't think I can use linklabel as I think it is in a fixed position.


Solution

  • For the first part of your problem, opening Explorer, you can do this on the click event for each item in the list (or the click event of the whole list area, as I describe later):

    System.Diagnostics.Process.Start("explorer.exe", "\"" + path + "\"");
    

    (the quote/slash thing is to make sure paths with spaces work)

    For the UI bits, I've never even seen LinkLabels before, so I don't know how you got on the road to that, lol! I'm not sure whether you're using WinForms or WPF but in either you'd generally want to use something like a ListBox (or a custom control that behaves/looks precisely how you want, but I'd guess you aren't ready for that). In WPF you could set the ListBox's ItemsSource to your data and DisplayMemberPath to whatever property the text comes from (if it is only strings then just don't set DisplayMemberPath). You'd then set up an event for clicking on the ListBox and respond to that by checking what item is selected and running the code above to open Explorer.

    If you want to get your UI working with minimal changes, try replacing the LinkLabels with Buttons (you can style them to look like links if you want, at least in WPF) since those work the same way.