sb.Remove(0, sb.Length);
//Always add the root level to the string builder, since the root will always show in the link.
sb.Append(ROOT);
// Obviously, Only add levels to the LinkLabel Links collection if our list of levels is not empty.
// If our list is empty, we only have the root, which will be the only string shown in the link label text, and not as a link.
// Root will be shown as a clickable link if at least one more level is shown next to it.
if (PathLevelList.Count > 0)
linkLabel1.Links.Add(0, ROOT.Length - 1, ROOT);
// Loop through our list of levels and add each level from the list to the LinkLabel Links collection, except for the last level in the list.
// The last level text is diplayed in the text of the LinkLabel control, but not shown as a link.
// Also add a back slash separator between each level name.
foreach (string level in PathLevelList)
{
sb.Append(level);
sb.Append("\\");
//Don't make the last level's text a link, since it is the current level. It should not be clickable, only the ones before it are, meaning its parents.
if (wordCount == PathLevelList.Count - 1)
break;
linkLabel1.Links.Add(linkTextCharCount, level.Length, level);
// Keep track of the starting index of the next link text. We add 1 to the position to account for the back slash.
linkTextCharCount += (level.Length + 1);
wordCount++;
}
return sb.ToString();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
int indx = linkLabel1.Links.IndexOf(e.Link);
//Based on the index returned, which tells us which level in the Links collection was selected.
// we can remove the levels that follow the one selected from our list of levels.
// we call our private helper method GetLinkPath(), the right path string will be returned, which we can use.
// to set the Text property of our LinkLabel control.
while (PathLevelList.Count > indx)
PathLevelList.RemoveAt(PathLevelList.Count - 1);
linkLabel1.Text = GetLinkPath();
}