Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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
)

Parameters

start
Type: System.Int32
The starting character within the text of the label where the link is created.
length
Type: System.Int32
The number of characters after the starting character to include in the link text.
linkData
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.

NoteNote

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.
			linkLabel1.Links.Add(6,4,"www.microsoft.com");

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

			// Add the control to the form.
			this.Controls.Add(linkLabel1);
		}

		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.
			System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
		}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.