Export (0) Print
Expand All

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

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Function Add( _
   ByVal start As Integer, _
   ByVal length As Integer, _
   ByVal linkData As Object _
) As Link
[C#]
public Link Add(
   int start,
 int length,
 object linkData
);
[C++]
public: Link* Add(
   int start,
 int length,
 Object* linkData
);
[JScript]
public function Add(
   start : int,
 length : int,
 linkData : Object
) : Link;

Parameters

start
The starting character within the text of the label where the link is created.
length
The number of characters after the starting character to include in the link text.
linkData
The object containing the information to associate with the link.

Return Value

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

Remarks

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 object 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.

Note   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.

Example

[Visual Basic, C#, C++] The following 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 assumes that it is located within a Form class.

[Visual Basic] 
' Create a new LinkLabel control.
Private linkLabel1 As New LinkLabel()


Public Sub 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.
    AddHandler linkLabel1.LinkClicked, AddressOf Me.linkLabel1_LinkClicked
    
    ' Add the control to the form.
    Me.Controls.Add(linkLabel1)
End Sub 'InitializeMyLinkLabel


Private Sub linkLabel1_LinkClicked(sender As Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
    ' 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())
End Sub 'linkLabel1_LinkClicked

[C#] 
// 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());
  }

[C++] 
// Create a new LinkLabel control.
private:
LinkLabel* linkLabel1;

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 = System::Drawing::Point(8, 16);
    linkLabel1->Size = System::Drawing::Size(135, 13);

    // Set the text to display in the label.
    linkLabel1->Text = S"Click here to get more info.";

    // Create a new link using the Add method of the LinkCollection class.
    linkLabel1->Links->Add(6, 4, S"www.microsoft.com");

    // Create an event handler for the LinkClicked event.
    linkLabel1->LinkClicked +=
        new System::Windows::Forms::LinkLabelLinkClickedEventHandler(this, 
        &Form1::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->Item[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());
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

LinkLabel.LinkCollection Class | LinkLabel.LinkCollection Members | System.Windows.Forms Namespace | LinkLabel.LinkCollection.Add Overload List

Show:
© 2015 Microsoft