Export (0) Print
Expand All

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

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

To link to another form with a LinkLabel control

  1. Set the Text property to an appropriate caption.

  2. Set the LinkArea 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 LinkArea value is represented by a LinkArea object containing two numbers, the starting character position and the number of characters. The LinkArea 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);
    
  3. In the LinkClicked event handler, invoke the Show method to open another form in the project, and set the LinkVisited property to true.

    Note Note

    An instance of the LinkLabelLinkClickedEventArgs class carries a reference to the LinkLabel control that was clicked, so there is no need to cast thesender object.

    protected void linkLabel1_LinkClicked(object sender, System. Windows.Forms.LinkLabelLinkClickedEventArgs e)
    {
       // Show another form.
       Form f2 = new Form();
       f2.Show();
       linkLabel1.LinkVisited = true;
    }
    

The 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 Text property to an appropriate caption.

  2. Set the LinkArea property to determine which part of the caption will be indicated as a link.

  3. In the LinkClicked event handler, in the midst of an exception-handling block, call a second procedure that sets the LinkVisited property to true and uses the Start method to start the default browser with a URL. To use the Start method you need to add a reference to the System.Diagnostics namespace.

    Security note Security Note

    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. TheSystem.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 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");
    }
    
Show:
© 2014 Microsoft