LinkLabel.LinkCollection.Add Method (Int32, Int32, Object)


Adds a link to the collection with information to associate with the link.

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

public LinkLabel.Link Add(
	int start,
	int length,
	object linkData


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.

Type: System.Object

The object containing the information to associate with the link.

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. This version of the Add method enables you to provide additional information that can be associated with the link through the linkData parameter. For example, you can pass a String to the linkData parameter that contains a URL to display when the link is clicked. You can then use this information in your handler for the LinkClicked event of the LinkLabel control to display the URL in Microsoft Internet Explorer.


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 code example creates a LinkLabel control that displays a link, and displays the Microsoft Web site in the default browser when the link defined in the control's text is clicked. The example defines a method that initializes the LinkLabel control as well as a method that will handle the LinkClicked event of the control. The event handler of the LinkClicked event uses the LinkData property of the LinkLabel.Link class to determine the URL to display in the default browser. This example requires that it is located within a Form class.

    // Create a new LinkLabel control.
private LinkLabel linkLabel1 = new LinkLabel();

public void InitializeMyLinkLabel()

	// Set the control to autosize based on the text content.
	linkLabel1.AutoSize = true;
	// Position and size the control on the form.
	linkLabel1.Location = new System.Drawing.Point(8,16);
	linkLabel1.Size = new System.Drawing.Size(135,13);
	// Set the text to display in the label.
	linkLabel1.Text = "Click here to get more info.";

	// Create a new link using the Add method of the LinkCollection class.

	// Create an event handler for the LinkClicked event.
	linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);

	// Add the control to the form.

private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
	// Determine which link was clicked within the LinkLabel.
	linkLabel1.Links[linkLabel1.Links.IndexOf(e.Link)].Visited = true;
	// Display the appropriate link based on the value of the LinkData property of the Link object.

.NET Framework
Available since 1.1
Return to top