LinkLabel.LinkCollection.Add Method (Int32, Int32)


Adds a link to the collection.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public LinkLabel.Link Add(
	int start,
	int length


Type: System.Int32

The starting character within the text of the label where the link is created.

Type: System.Int32

The number of characters after the starting character to include in the link text.

Return Value

Type: System.Windows.Forms.LinkLabel.Link

A LinkLabel.Link representing the link that was created and added to the collection.

A LinkLabel control can display multiple links within the text of the control. The Add method enables you to convert text within the LinkLabel control to a link that can be clicked on by the user to perform tasks similar to a Button control. This method adds the link that is created to the LinkLabel.LinkCollection for the LinkLabel. For example, if you want to set the word "quick" in the label text, "The quick brown fox", you call this method with the start parameter set to the value of four (4), and the length parameter to five (5). The word "quick" then changes to a link and the link is added to the collection. If you want to associate information with the link, such as the URL to display or a file to open when the user clicks on the link, use the other version of the Add method.


Two links cannot share the same text. If you create a link that uses text that is already used by another link, an exception is thrown.

The following example demonstrates setting the LinkBehavior property using the LinkBehavior enumeration. It also demonstrates setting the ActiveLinkColor, LinkColor, Text and LinkVisited properties and adding items to a LinkLabel.LinkCollection. To run this example, paste it in a blank form and call InitializeLinkLabel from the form's constructor or Load method.

// Declare the LinkLabel object.
internal System.Windows.Forms.LinkLabel LinkLabel1;

// Declare keywords array to identify links
string[] keywords;

private void InitializeLinkLabel()
	this.LinkLabel1 = new System.Windows.Forms.LinkLabel();

	// Set the location, name and size.
	this.LinkLabel1.Location = new System.Drawing.Point(10, 20);
	this.LinkLabel1.Name = "CompanyLinks";
	this.LinkLabel1.Size = new System.Drawing.Size(104, 150);

	// Set the LinkBehavior property to show underline when mouse
	// hovers over the links.
	this.LinkLabel1.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
	string textString = "For more information see our" +
		" company website or the research page at Contoso Ltd. ";

	// Set the text property.
	this.LinkLabel1.Text = textString;

	// Set the color of the links to black, unless the mouse
	// is hovering over a link.
	this.LinkLabel1.LinkColor = System.Drawing.Color.Black;
	this.LinkLabel1.ActiveLinkColor = System.Drawing.Color.Blue;

	// Associate the event-handling method with the LinkClicked
	// event.
	this.LinkLabel1.LinkClicked += 
		new LinkLabelLinkClickedEventHandler(LinkLabel1_LinkClicked);

	// Add links to the LinkCollection using starting index and
	// length of keywords.
	keywords = new string[]{"company", "research"};
	foreach ( string keyword in keywords )
		this.LinkLabel1.Links.Add(textString.IndexOf(keyword), keyword.Length);

	// Add the label to the form.

private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

	string url = "";

	// Determine which link was clicked and set the appropriate url.
		case 0:
			url = "";

		case 1:
			url = "";

	// Set the visited property to True. This will change
	// the color of the link.
	e.Link.Visited = true;

	// Open Internet Explorer to the correct url.
	System.Diagnostics.Process.Start("IExplore.exe", url);

.NET Framework
Available since 1.1
Return to top