Export (0) Print
Expand All

Changing the Appearance of the Windows Forms LinkLabel Control

Visual Studio .NET 2003

The text displayed by the Windows Forms LinkLabel control can be changed to suit a variety of purposes. For example, it is common to indicate that the text can be clicked by setting it to appear in a specific color with an underline. After it is clicked, 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

  • Set the LinkColor and VisitedLinkColor properties to the desired colors. This can be done either programmatically or at design time in the Properties window.
    ' Visual Basic
    ' 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
    
    // C#
    // 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;
    
    // C++
    // 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;
    
  • Set the Text property to an appropriate caption. This can be done either programmatically or at design time in the Properties window.
    ' Visual Basic
    LinkLabel1.Text = "Click here to see more."
    
    // C#
    linkLabel1.Text = "Click here to see more.";
    
    // C++
    linkLabel1->Text = S"Click here to see more.";
    
  • 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 object 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.
    ' Visual Basic
    LinkLabel1.LinkArea = new LinkArea(6,4)
    
    // C#
    linkLabel1.LinkArea = new LinkArea(6,4);
    
    // C++
    linkLabel1->LinkArea = LinkArea(6,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.
  • In the LinkClicked event handler, set the LinkVisited property to true. When a link has been visited, it is customary to change its appearance in some way, usually by color. The text will change to the color specified by the VisitedLinkColor property.
    ' Visual Basic
    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
    
    // C#
    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
    }
    
    // C++
    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

Introduction to the Windows Forms LinkLabel Control | Linking to an Object or Web Page with the Windows Forms LinkLabel Control | LinkLabel.LinkArea Property | LinkLabel.LinkColor Property | LinkLabel.VisitedLinkColor Property | LinkLabel.LinkVisited Property | LinkLabel Control (Windows Forms)

Show:
© 2015 Microsoft