[Yugong Series] Detailed Explanation of LinkLabel Controls on Winform Controls in September 2023


foreword

Winform controls are user interface elements in Windows Forms. They can be used to create various visual and interactive components of Windows applications, such as buttons, labels, text boxes, drop-down list boxes, check boxes, radio boxes, progress bars, etc. . Developers can use Winform controls to build user interfaces and respond to user actions to create powerful desktop applications.

1. Detailed explanation of LinkLabel control

LinkLabel (link label) control is a standard control in Windows Forms, used to display hyperlinks in the form. It is similar to a normal Label control, but it can automatically convert URLs, email addresses or local file paths in the text into clickable links, so that users can jump to the corresponding location.

In Winform, it can be added to the form through the LinkLabel control in the Toolbox. At design time, you can set the properties of the control, such as text content, font, color, link color, font style, etc.

In code, you can respond to the user clicking a link by setting the control's LinkClicked event handler. For example, the following code shows how to open the system default browser and jump to the specified URL in the LinkClicked event:

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    
    
    System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
}

When using the LinkLabel control, you need to pay attention to the following points:

  • If there is a space in the text, you need to use " " (non-breaking space) instead, otherwise the space will be automatically replaced with "%20";
  • If there are special characters in the text, you need to use the "&" symbol to escape, such as "<" instead of the less than sign, ">" instead of the greater than sign;
  • If you want the link to open a local file, you need to add "file://" prefix before the link, such as "file://C:/path/to/file";
  • If you want the link to appear as plain text instead of a link, you can set the control's LinkBehavior property to NeverUnderline.

1. Attribute introduction

1.1 LinkArea

The LinkArea property of the LinkLabel control is used to specify the text area that needs to be associated with the hyperlink. By default, the LinkLabel control converts any string in the text that matches the format of a URL, e-mail address, or local file path into a hyperlink. But sometimes we only need to set a certain part of the text as a hyperlink, which can be controlled by setting the LinkArea property.

The LinkArea property accepts a LinkArea structure as a value, which defines the text range that needs to be associated with the hyperlink. The structure has two properties: Start and Length, which respectively represent the starting position and length of the text to be associated. For example, the following code sets the first word in a LinkLabel control as a hyperlink:

linkLabel1.Text = "Click here to go to Google";
linkLabel1.LinkArea = new LinkArea(0, 5);

It should be noted that when setting the LinkArea property, the starting position and length are zero-based indexes. If you need to display complex hyperlinks on the LinkLabel control, it is recommended to use the RichTextBox control, which supports richer text formatting and style settings.

insert image description here

1.2 LinkBehavior

The LinkBehavior property of the LinkLabel control is used to set the display mode of the hyperlink. Can be set to the following values:

  1. SystemDefault: The default value, using the existing styles in the system to display hyperlinks.

  2. AlwaysUnderline: Always display an underline.

  3. HoverUnderline: Displays an underline when the mouse hovers over it.

  4. NeverUnderline: Never display underlines.

Instructions:

Select the LinkLabel control in the design window, find the LinkBehavior property in the property window, and select the desired value. It can also be set in code:

linkLabel1.LinkBehavior = LinkBehavior.AlwaysUnderline; 

This will make hyperlinks always appear underlined.

1.3 ActiveLinkColor和DisabledLinkColor和LinkColor

LinkLabel control is one of the commonly used controls in Winform, used to display hyperlink text. It has three important properties: LinkColor, ActiveLinkColor, and DisabledLinkColor, which are used to control the three states of the link text in LinkLabel: default state, state when the mouse moves over the link, and disabled state.

LinkColor Property

The LinkColor property is used to control the default color of the link text in the LinkLabel. By default, the color of the link text in LinkLabel is blue, if you need to change it, you can achieve it by setting the LinkColor property.

For example, to set the color of link text in LinkLabel to red:

linkLabel1.LinkColor = Color.Red;

ActiveLinkColor Property

The ActiveLinkColor property is used to control the color of the link text in the LinkLabel when it is selected by the mouse. When the mouse moves over the link text, the link text will change to the color set by ActiveLinkColor.

For example, to set the color of the linked text in the LinkLabel to green when it is selected by the mouse:

linkLabel1.ActiveLinkColor = Color.Green;

DisabledLinkColor Property

The DisabledLinkColor property is used to control the color of the link text in the LinkLabel when it is disabled. When the LinkLabel is disabled (Enabled = false), the link text will change to the color set by DisabledLinkColor.

For example, to set the color of the disabled state of a link in a LinkLabel to gray:

linkLabel1.DisabledLinkColor = Color.Gray;

It should be noted that when the LinkLabel is disabled, the link will not respond and cannot be clicked. Therefore, when using the LinkLabel control, you need to control the value of the Enabled property according to actual needs.

1.4 Image

The Image property of the LinkLabel control is used to set the image next to the link text. If the Image property is set, an image is displayed next to the link text, and the link text is automatically sized and positioned as necessary.

Here are the steps to use the Image property of the LinkLabel control:

  1. Add a LinkLabel control to the form.
  2. Open the Properties pane in the form designer.
  3. In the properties pane, find the Image property and click the button next to it to open the image picker dialog.
  4. In the image selector dialog, select the image to display next to the link text, and click the OK button.
  5. Check the display on the LinkLabel control and adjust the position and size of the link text if necessary.

Note that the Image property of the LinkLabel control can only display one image. If you need to display multiple images next to the link text, you need to use other controls or custom controls to achieve this.

1.5 LinkVisited和VisitedLinkColor

LinkLabel control is a commonly used control in Winform, used to display hyperlinks. In the LinkLabel control, there are two attributes related to the visit state of the link, namely LinkVisited and VisitedLinkColor.

  1. LinkVisited property

The LinkVisited attribute indicates whether the link has been visited, it is a Boolean attribute. By default, the link in the LinkLabel control has not been visited, and the LinkVisited property value is false. When the user clicks on the link and visits, the LinkVisited attribute value will be automatically set to true. When the LinkVisited property is set to true, the LinkLabel control will use the color specified by the VisitedLinkColor property to display the visited links.

  1. VisitedLinkColor Property

The VisitedLinkColor property is used to set the color of the visited links. By default, visited links are colored blue. If the VisitedLinkColor property is set, links that have been visited will be displayed in the specified color.

The following is an example of using the LinkVisited and VisitedLinkColor properties in the LinkLabel control:

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    
    
    // 设置LinkVisited属性为true,以便已访问过的链接显示不同的颜色
    this.linkLabel1.LinkVisited = true;
    
    // 在浏览器中打开链接
    System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
}

private void linkLabel1_MouseEnter(object sender, EventArgs e)
{
    
    
    // 当鼠标移动到链接上时,将链接的颜色改为橙色
    this.linkLabel1.LinkColor = Color.Orange;
}

private void linkLabel1_MouseLeave(object sender, EventArgs e)
{
    
    
    // 当鼠标移开链接时,将链接的颜色改回默认颜色
    this.linkLabel1.LinkColor = Color.Blue;
}

private void Form1_Load(object sender, EventArgs e)
{
    
    
    // 设置VisitedLinkColor属性为绿色
    this.linkLabel1.VisitedLinkColor = Color.Green;
    
    // 添加链接
    this.linkLabel1.Links.Add(0, 3, "https://www.baidu.com");
}

In the example code above, when the mouse is over the link, the color of the link is changed to orange; when the mouse is moved away from the link, the color of the link is changed back to the default color (blue). In the Form1_Load event, set the VisitedLinkColor property to green and add a link. When the user clicks on the link and visits it, the link will be displayed in green to show the visited status.

2. Common scenarios

LinkLabel control is one of the commonly used controls in Winform, it is mainly used to display hyperlink text, when the user clicks the link, it can trigger the corresponding event. The following are common scenarios for the LinkLabel control:

  1. Display URL link: When you need to display URL link in Winform, you can use the LinkLabel control, so that users can directly access the URL when they click the link.

  2. Display help document link: When it is necessary to provide a help document link in Winform, you can use the LinkLabel control, so that when the user clicks the link, the corresponding help document can be opened.

  3. Display copyright and legal notices: When you need to display copyright and legal notices in Winform, you can use the LinkLabel control, so that users can view the corresponding copyright and legal notices when they click the link.

  4. Display other application links: When you need to display other application links in Winform, you can use the LinkLabel control, so that when the user clicks the link, the corresponding application can be launched.

3. Specific cases

One possible use case for using the LinkLabel control is to create a help document presentation section in an application form.

First, open Visual Studio and create a new Windows Forms application project. Drag and drop a LinkLabel control, a Label control, and a TextBox control on the form.

Set the Text property of the LinkLabel control to "Help Documentation", and set the LinkColor and VisitedLinkColor properties to emphasize that the control is a link. You can set other properties as needed, such as Tooltip, Font and so on.

Next, handle the Click event of the LinkLabel control. Open the code view and add the following code to the form class:

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    
    
    textBox1.Text = "这里展示帮助文档内容。";
    label1.Visible = true;
}

This method will be called when the user clicks the LinkLabel control, and the help document content will be displayed in the TextBox control. In addition, a prompt message is displayed by setting the Visible property of the Label control to tell the user that the help document has been loaded.

Finally, in order to improve the user experience, some initialization code can be added to the Load event processing method of the form class, such as setting the Visible property of the Label control to false, and hiding the prompt information when starting the application.

private void Form1_Load(object sender, EventArgs e)
{
    
    
    label1.Visible = false;
}

Now run the application, when the user clicks the LinkLabel control, you should be able to see the content of the help document displayed in the TextBox, and the prompt information is displayed.

insert image description here

Guess you like

Origin blog.csdn.net/aa2528877987/article/details/132683882