Using the LinkLabel Class

Using the LinkLabel Class

The LinkLabel class is similar to the Label class. Instead of providing only a descriptive label, however, a LinkLabel object has the appearance of an HTML hyperlink on a Web page. This control is often used to provide a link to a relevant Web page or as a navigation control for forms that employ a Web-like user interface.

Handling the Click Event

A LinkLabel object has some of the same properties exposed by the Button and Label classes. For example, the caption displayed by the LinkLabel object is set with the Text property, as shown here:

linkLabel.Text = "Support Page";

The LinkLabel class raises a LinkClicked event when the user clicks the label control. To open a Web page in response to the event, use the Process.Start method from the System.Diagnostics namespace, as shown here:

private void linkLabel_LinkClicked(object sender, 
                                   LinkLabelLinkClickedEventArgs e)
{
    Process.Start("http://www.microsoft.com");
}

In this example, the Process.Start method will open the user’s Web browser and navigate to the specified URL.

Of course, you’re not limited to simply navigating to a URL. In the LinkClicked event handler, you also can open another form or take some other action, as shown here:

private void linkLabel_LinkClicked(object sender, 
                                   LinkLabelLinkClickedEventArgs e)
{
    reservationForm = new ReservationForm();
    reservationForm.ShowDialog();
}
Unique LinkLabel Properties

The LinkLabel class has several properties that enable it to have the same characteristics as an HTML hyperlink. For example, you can define specific colors that will be used when drawing the link’s text, depending on the link’s state. The properties used to define the link colors are listed here:

  • ActiveLinkColor  Used to specify the color of a link that’s in the process of being clicked

  • DisabledLinkColor  Used to specify the color of a link that’s been disabled by setting the control’s Enabled property to false

  • LinkColor  Used to specify the color of a link in its normal state, before it’s been clicked or visited

  • VisitedLinkColor  Used to specify the color of a link that’s been visited, which is specified via the LinkVisited property

The colors used by a LinkLabel object are typically set to different values, using either the Properties window or through code, as shown here:

public WebLookForm()
{
    
    

    linkLabel.ActiveLinkColor = Color.Lime;
    linkLabel.DisabledLinkColor = Color.DarkOrange;
    linkLabel.LinkColor = Color.DarkGreen;
    linkLabel.VisitedLinkColor = Color.DarkOliveGreen;
}

The default colors provided by the LinkLabel class are listed in Table 12-6.

The default color used for DisabledLinkColor is a variation of gray that isn’t included in the Color enumeration.

A LinkLabel object has no built-in ability to know whether a link has been visited. Because navigation behavior for the control is really specified by the LinkClicked event handler that you implement, you’re responsible for specifying whether the link has been visited. The following code is an example of setting the LinkVisited property to true inside the LinkClicked event handler:

private void linkLabel_LinkClicked(object sender, 
                                   LinkLabelLinkClickedEventArgs e)
{
    
    

    linkLabel.LinkVisited = true;
}

If you prefer not to provide a different link color for visited link label controls on your forms, simply refrain from setting the LinkVisited property.

By default, when you drop a LinkLabel control on a form, the entire control is clickable. Although this is often a reasonable way to use the LinkLabel control, there are times when it make sense to have just a portion of the control serve as a clickable link. The LinkArea property is used to define the portion of the control that’s clickable and is set using the LinkArea class, as follows:

public WebLookForm()
{
    
    

    LinkArea range = new LinkArea(8, 4);
    linkLabel.LinkArea = range;
}

As shown in this code, the LinkArea class is initialized by passing two values to its constructor: the index where the clickable area begins and the clickable area’s length.

In addition to specifying the region of the LinkLabel control that serves as the clickable region, you also can specify the appearance of the link using the LinkBehavior property. For example, you can specify that the link will be underlined only when the mouse pointer is over the control, as is done for many Web pages. Alternatively, you can elect to turn the underline off altogether, which is useful if you intend to stop processing Click events. The LinkBehavior property must be set to a value from the LinkBehavior enumeration, shown in Table 12-7.

The default value for the LinkBehavior property is SystemDefault.



Part III: Programming Windows Forms