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

LinkLabel.LinkCollection.Add Method

Adds a link to the collection.

Overload List

Adds a link to the collection.

[Visual Basic] Overloads Public Function Add(Integer, Integer) As Link
[C#] public Link Add(int, int);
[C++] public: Link* Add(int, int);
[JScript] public function Add(int, int) : Link;

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

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

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, C#, C++] Note   This example shows how to use one of the overloaded versions of Add. For other examples that might be available, see the individual overload topics.
[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.

See Also

LinkLabel.LinkCollection Class | LinkLabel.LinkCollection Members | System.Windows.Forms Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.