Skip to content

Latest commit

 

History

History
183 lines (156 loc) · 7.74 KB

link-to-an-object-or-web-page-with-wf-linklabel-control.md

File metadata and controls

183 lines (156 loc) · 7.74 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
Link to an Object or Web Page with LinkLabel Control
Learn how to create Web-style links to an object or web page with the Windows Forms LinkLabel control.
03/30/2017
csharp
vb
cpp
examples [Windows Forms], LinkLabel control
Windows Forms, linking to objects
Web page link control
linking [Windows Forms], to other forms
Windows Forms, linking to Web pages
links [Windows Forms], to other forms
LinkLabel control [Windows Forms], linking to object or Web page
LinkLabel control [Windows Forms], examples
6c91c975-3cb7-4504-82f0-fc6255f8fb85

How to: Link to an Object or Web Page with the Windows Forms LinkLabel Control

The Windows Forms xref:System.Windows.Forms.LinkLabel control allows you to create Web-style links on your form. When the link is clicked, you can change its color to indicate the link has been visited. For more information on changing the color, see How to: Change the Appearance of the Windows Forms LinkLabel Control.

Linking to Another Form

To link to another form with a LinkLabel control

  1. Set the xref:System.Windows.Forms.LinkLabel.Text%2A property to an appropriate caption.

  2. Set the xref:System.Windows.Forms.LinkLabel.LinkArea%2A property to determine which part of the caption will be indicated as a link. How it is indicated depends on the appearance-related properties of the link label. The xref:System.Windows.Forms.LinkLabel.LinkArea%2A value is represented by a xref:System.Windows.Forms.LinkLabel.LinkArea%2A object containing two numbers, the starting character position and the number of characters. The xref:System.Windows.Forms.LinkLabel.LinkArea%2A property can be set in the Properties window or in code in a manner similar to the following:

    ' In this code example, the link area has been set to begin
    ' at the first character and extend for eight characters.
    ' You may need to modify this based on the text entered in Step 1.
    LinkLabel1.LinkArea = New LinkArea(0, 8)
    // In this code example, the link area has been set to begin
    // at the first character and extend for eight characters.
    // You may need to modify this based on the text entered in Step 1.
    linkLabel1.LinkArea = new LinkArea(0,8);
    // In this code example, the link area has been set to begin
    // at the first character and extend for eight characters.
    // You may need to modify this based on the text entered in Step 1.
    linkLabel1->LinkArea = LinkArea(0,8);
  3. In the xref:System.Windows.Forms.LinkLabel.LinkClicked event handler, invoke the xref:System.Windows.Forms.Form.Show%2A method to open another form in the project, and set the xref:System.Windows.Forms.LinkLabel.LinkVisited%2A property to true.

    [!NOTE] An instance of the xref:System.Windows.Forms.LinkLabelLinkClickedEventArgs class carries a reference to the xref:System.Windows.Forms.LinkLabel control that was clicked, so there is no need to cast the sender object.

    Protected Sub LinkLabel1_LinkClicked(ByVal Sender As System.Object, _
       ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _
       Handles LinkLabel1.LinkClicked
       ' Show another form.
       Dim f2 As New Form()
       f2.Show
       LinkLabel1.LinkVisited = True
    End Sub
    protected void linkLabel1_LinkClicked(object sender, System. Windows.Forms.LinkLabelLinkClickedEventArgs e)
    {
       // Show another form.
       Form f2 = new Form();
       f2.Show();
       linkLabel1.LinkVisited = true;
    }
    private:
       void linkLabel1_LinkClicked(System::Object ^  sender,
          System::Windows::Forms::LinkLabelLinkClickedEventArgs ^  e)
       {
          // Show another form.
          Form ^ f2 = new Form();
          f2->Show();
          linkLabel1->LinkVisited = true;
       }

Linking to a Web Page

The xref:System.Windows.Forms.LinkLabel control can also be used to display a Web page with the default browser.

To start Internet Explorer and link to a Web page with a LinkLabel control

  1. Set the xref:System.Windows.Forms.LinkLabel.Text%2A property to an appropriate caption.

  2. Set the xref:System.Windows.Forms.LinkLabel.LinkArea%2A property to determine which part of the caption will be indicated as a link.

  3. In the xref:System.Windows.Forms.LinkLabel.LinkClicked event handler, in the midst of an exception-handling block, call a second procedure that sets the xref:System.Windows.Forms.LinkLabel.LinkVisited%2A property to true and uses the xref:System.Diagnostics.Process.Start%2A method to start the default browser with a URL. To use the xref:System.Diagnostics.Process.Start%2A method you need to add a reference to the xref:System.Diagnostics?displayProperty=nameWithType namespace.

    [!IMPORTANT] If the code below is run in a partial-trust environment (such as on a shared drive), the JIT compiler fails when the VisitLink method is called. The System.Diagnostics.Process.Start statement causes a link demand that fails. By catching the exception when the VisitLink method is called, the code below ensures that if the JIT compiler fails, the error is handled gracefully.

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, _
       ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _
       Handles LinkLabel1.LinkClicked
       Try
          VisitLink()
       Catch ex As Exception
          ' The error message
          MessageBox.Show("Unable to open link that was clicked.")
       End Try
    End Sub
    
    Sub VisitLink()
       ' Change the color of the link text by setting LinkVisited
       ' to True.
       LinkLabel1.LinkVisited = True
       ' Call the Process.Start method to open the default browser
       ' with a URL:
       System.Diagnostics.Process.Start("http://www.microsoft.com")
    End Sub
    private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
    {
       try
       {
          VisitLink();
       }
       catch (Exception ex )
       {
          MessageBox.Show("Unable to open link that was clicked.");
       }
    }
    
    private void VisitLink()
    {
       // Change the color of the link text by setting LinkVisited
       // to true.
       linkLabel1.LinkVisited = true;
       //Call the Process.Start method to open the default browser
       //with a URL:
       System.Diagnostics.Process.Start("http://www.microsoft.com");
    }
    private:
       void linkLabel1_LinkClicked(System::Object ^  sender,
          System::Windows::Forms::LinkLabelLinkClickedEventArgs ^  e)
       {
          try
          {
             VisitLink();
          }
          catch (Exception ^ ex)
          {
             MessageBox::Show("Unable to open link that was clicked.");
          }
       }
    private:
       void VisitLink()
       {
          // Change the color of the link text by setting LinkVisited
          // to true.
          linkLabel1->LinkVisited = true;
          // Call the Process.Start method to open the default browser
          // with a URL:
          System::Diagnostics::Process::Start("http://www.microsoft.com");
       }

See also