-
C# 教程之LinkLabel
- 2016-01-19 20:26 来源:未知
LinkLabel
Windows 窗体LinkLabel控件使您可以向 Windows 窗体应用程序添加 Web 样式的链接。
一切可以使用Label控件的地方,都可以使用LinkLabel控件;还可以将文本的一部分设置为指向某个文件、文件夹或网页的链接。使用 LinkLabel 控件可以实现的功能除了具有Label控件的所有属性、方法和事件以外,LinkLabel控件还有针对超链接和链接颜色的属性。LinkArea属性设置激活链接的文本区域。LinkColor、VisitedLinkColor和ActiveLinkColor属性设置链接的颜色。LinkClicked事件确定选择链接文本后将发生的操作。LinkLabel控件的最简单用法是使用LinkArea属性显示一个链接,但您也可以使用Links属性显示多个超链接。Links属性使您可以访问一个链接集合。也可以在每个单个LinkLabel..::.Link对象的LinkData属性中指定数据。LinkData属性的值可以用来存储要显示文件的位置或网站的地址。
[C#]
// Create a new LinkLabel control.
private LinkLabel linkLabel1 = new LinkLabel();
public void InitializeMyLinkLabel()
{
// Set the control to autosize based on the text content.
linkLabel1.AutoSize = true;
// Position and size the control on the form.
linkLabel1.Location = new System.Drawing.Point(8,16);
linkLabel1.Size = new System.Drawing.Size(135,13);
// Set the text to display in the label.
linkLabel1.Text = "Click here to get more info.";
// Create a new link using the Add method of the LinkCollection class.
linkLabel1.Links.Add(6,4,"www.microsoft.com");
// Create an event handler for the LinkClicked event.
linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
// Add the control to the form.
this.Controls.Add(linkLabel1);
}
private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
// Determine which link was clicked within the LinkLabel.
linkLabel1.Links[linkLabel1.Links.IndexOf(e.Link)].Visited = true;
// Display the appropriate link based on the value of the LinkData property of the Link object.
System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
}
Windows 窗体LinkLabel控件使您可以向 Windows 窗体应用程序添加 Web 样式的链接。
一切可以使用Label控件的地方,都可以使用LinkLabel控件;还可以将文本的一部分设置为指向某个文件、文件夹或网页的链接。使用 LinkLabel 控件可以实现的功能除了具有Label控件的所有属性、方法和事件以外,LinkLabel控件还有针对超链接和链接颜色的属性。LinkArea属性设置激活链接的文本区域。LinkColor、VisitedLinkColor和ActiveLinkColor属性设置链接的颜色。LinkClicked事件确定选择链接文本后将发生的操作。LinkLabel控件的最简单用法是使用LinkArea属性显示一个链接,但您也可以使用Links属性显示多个超链接。Links属性使您可以访问一个链接集合。也可以在每个单个LinkLabel..::.Link对象的LinkData属性中指定数据。LinkData属性的值可以用来存储要显示文件的位置或网站的地址。
[C#]
// Create a new LinkLabel control.
private LinkLabel linkLabel1 = new LinkLabel();
public void InitializeMyLinkLabel()
{
// Set the control to autosize based on the text content.
linkLabel1.AutoSize = true;
// Position and size the control on the form.
linkLabel1.Location = new System.Drawing.Point(8,16);
linkLabel1.Size = new System.Drawing.Size(135,13);
// Set the text to display in the label.
linkLabel1.Text = "Click here to get more info.";
// Create a new link using the Add method of the LinkCollection class.
linkLabel1.Links.Add(6,4,"www.microsoft.com");
// Create an event handler for the LinkClicked event.
linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
// Add the control to the form.
this.Controls.Add(linkLabel1);
}
private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
// Determine which link was clicked within the LinkLabel.
linkLabel1.Links[linkLabel1.Links.IndexOf(e.Link)].Visited = true;
// Display the appropriate link based on the value of the LinkData property of the Link object.
System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
}