This short article shows a simple example of how to use the LinkLabel control to display a hierarchy of levels that can be clicked, such as folder names.
The LinkLabel control provides a collection property named 'Links' that can be used to contain all the links shown in the text of the LinkLabel control.
A link is added to the Links collection of the LinkLabel control as follows:
linkLabel1.Links.Add(index, count, text);
The index arg is the starting index of the link text within the full text shown by the LinkLabel control. count is the number of characters to be shown as a link within the whole text of the LinkLabel, and text is the text of the link to be shown within the text of the link label.
For example have a LinkLabel control show the following text: "How are you?", with only the 2 words "How" and "you" clickable as links. The rest of the text is not clickable.
To do so, the following is done:
linkLabel1.Text = "How are you?";
linkLabel1.Links.Add(0, 3, "How");
// "How" starts at 0, and is 3 chars long
linkLabel1.Links.Add(8, 3, "you");
// "you" starts at 8, and is also 3 chars long
To find out which link was clicked within the LinkLabel control at run-time, the LinkClicked event can be handled, indicating the index of the clicked link within the Links collection.
A sample event handler method is shown at the end of this article as an example.
To make this process more dynamic, we can just use a list that mirrors the Links collection of the LinkLabel control. This list would contain the text of all the links to be shown in the LinkLabel text.
A variation would be that our list would contain all the links and an extra string, since we possibly would not want to make the last string in the LinkLabel text a link, or clickable.
The code below is an example of how a dynamic LinkLabel control text with links is implemented:
private System.Collections.ArrayList PathLevelList = new System.Collections.ArrayList(6);
private StringBuilder sb = new StringBuilder(6);
private const string ROOT = @"Root\";
// Somewhere in the code we add strings to our list to later populate the Links collection of the LinkLabel control.
PathLevelList.Add("Level_One");
PathLevelList.Add("Level_Two");
PathLevelList.Add("Level_Three");
// We use the private method defined below to re-display the LinkLabel control's text
linkLabel1.Text = GetLinkPath();
// After calling the above method, the linkLable text would be:
Root\Level_One\Level_Two\Level_Three", with "Level_Three" not highlighted as a link and not clickable. The back slashes are not clickable either, as can be deduced from the method body shown below.
// Method to figure out the text of the LinkLabel control, and which strings within it are shown as links.
private string GetLinkPath()
{
int wordCount = 0;
int linkTextCharCount = ROOT.Length;
linkLabel1.Links.Clear();
// Clear the string builder of level names
sb.Remove(0, sb.Length);
//Always add the root level to the string builder, since the root will always show in the link.
deepak madireddyPosted Mar 12, 2008, 3:03 AM
get me link label property for navigating by clicking linklabel i have to display next screen how can solve my qury get me thanking you
deepak madireddyPosted Mar 12, 2008, 3:03 AM
get me link label property for navigating by clicking linklabel i have to display next screen how can solve my qury get me thanking you