How to: Change the Appearance of the Windows Forms LinkLabel Control

You can change the text displayed by the LinkLabel control to suit a variety of purposes. For example, it is common practice to indicate to the user that text can be clicked by setting the text to appear in a specific color with an underline. After the user clicks the text, the color changes to a different color. To control this behavior, you can set five different properties: the LinkBehavior, LinkArea, LinkColor, VisitedLinkColor, and LinkVisited properties.

To change the appearance of a LinkLabel control

  1. Set the LinkColor and VisitedLinkColor properties to the colors you want.

    This can be done either programmatically or at design time in the Properties window.

    ' You can set the color using decimal values for red, green, and blue  
    LinkLabel1.LinkColor = Color.FromArgb(0, 0, 255)  
    ' Or you can set the color using defined constants  
    LinkLabel1.VisitedLinkColor = Color.Purple  
    
    // You can set the color using decimal values for red, green, and blue  
    linkLabel1.LinkColor = Color.FromArgb(0, 0, 255);  
    // Or you can set the color using defined constants  
    linkLabel1.VisitedLinkColor = Color.Purple;  
    
    // You can set the color using decimal values for red, green, and blue  
    linkLabel1->LinkColor = Color::FromArgb(0, 0, 255);  
    // Or you can set the color using defined constants  
    linkLabel1->VisitedLinkColor = Color::Purple;  
    
  2. Set the Text property to an appropriate caption.

    This can be done either programmatically or at design time in the Properties window.

    LinkLabel1.Text = "Click here to see more."  
    
    linkLabel1.Text = "Click here to see more.";  
    
    linkLabel1->Text = "Click here to see more.";  
    
  3. Set the LinkArea property to determine which part of the caption will be indicated as a link.

    The LinkArea value is represented with a LinkArea containing two numbers, the starting character position and the number of characters. This can be done either programmatically or at design time in the Properties window.

    LinkLabel1.LinkArea = new LinkArea(6,4)  
    
    linkLabel1.LinkArea = new LinkArea(6,4);  
    
    linkLabel1->LinkArea = LinkArea(6,4);  
    
  4. Set the LinkBehavior property to AlwaysUnderline, HoverUnderline, or NeverUnderline.

    If it is set to HoverUnderline, the part of the caption determined by LinkArea will only be underlined when the pointer rests on it.

  5. In the LinkClicked event handler, set the LinkVisited property to true.

    When a link has been visited, it is common practice to change its appearance in some way, usually by color. The text will change to the color specified by the VisitedLinkColor property.

    Protected Sub LinkLabel1_LinkClicked (ByVal sender As Object, _  
       ByVal e As EventArgs) Handles LinkLabel1.LinkClicked  
       ' Change the color of the link text  
       ' by setting LinkVisited to True.  
       LinkLabel1.LinkVisited = True  
       ' Then do whatever other action is appropriate  
    End Sub  
    
    protected void LinkLabel1_LinkClicked(object sender, System.EventArgs e)  
    {  
       // Change the color of the link text by setting LinkVisited
       // to True.  
       linkLabel1.LinkVisited = true;  
       // Then do whatever other action is appropriate  
    }  
    
    private:  
       System::Void linkLabel1_LinkClicked(System::Object ^  sender,  
          System::Windows::Forms::LinkLabelLinkClickedEventArgs ^  e)  
       {  
          // Change the color of the link text by setting LinkVisited
          // to True.  
          linkLabel1->LinkVisited = true;  
          // Then do whatever other action is appropriate  
       }  
    

See also