【窗体控件】:LinkLabel超链接控件

1、命名空间与继承

命名空间:System.Windows.Forms

继承:Object→MarshalByRefObject→Component→Control→Label→LinkLabel

2、常用属性

  1. ActiveLinkColor : 表示单击链接时的颜色。
  2. LinkColor: 表示链接的初始颜色
  3. VisitedLinkColor: 获取或设置当显示以前访问过的链接时所使用的颜色。
  4. DisabledLinkColor: 表示链接被禁止使用时的颜色。
  5. LinkArea: 获取或设置文本中视为链接的范围。
  6. LinkBehaviour: 获取或设置一个值,表示链接的行为。

注意:VisitedLinkColor
访问过必须是代码设置LinkVisited为true[或者Links[i].Visited为true。本质上一样]

3、常用事件

//超链接被点击事件
private void linkLabelxx_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
	 this.linkLabel1.LinkVisited = true; //设置该link已经被访问
     System.Diagnostics.Process.Start("http://www.baidu.com");
     //System.Diagnostics.Process.Start("IExplore", "http://www.baidu.com"); //指定浏览器
     //System.Diagnostics.Process.Start(@"C:\"); //同时打开C盘
     //System.Diagnostics.Process.Start("Notepad"); //打开NotePad++软件 
}

4、官方示例

链接

using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
{
    private System.Windows.Forms.LinkLabel linkLabel1;
    
    [STAThread]
    static void Main() 
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        // Create the LinkLabel.
        this.linkLabel1 = new System.Windows.Forms.LinkLabel();

        // Configure the LinkLabel's size and location. Specify that the
        // size should be automatically determined by the content.
        this.linkLabel1.Location = new System.Drawing.Point(34, 56);
        this.linkLabel1.Size = new System.Drawing.Size(224, 16);
        this.linkLabel1.AutoSize = true;

        // Configure the appearance. 
        // Set the DisabledLinkColor so that a disabled link will show up against the form's background.
        this.linkLabel1.DisabledLinkColor = System.Drawing.Color.Red;
        this.linkLabel1.VisitedLinkColor = System.Drawing.Color.Blue;
        this.linkLabel1.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
        this.linkLabel1.LinkColor = System.Drawing.Color.Navy;
        
        this.linkLabel1.TabIndex = 0;
        this.linkLabel1.TabStop = true;

        // Add an event handler to do something when the links are clicked.
        this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);

        // Identify what the first Link is.
        this.linkLabel1.LinkArea = new System.Windows.Forms.LinkArea(0, 8);

        // Identify that the first link is visited already.
        this.linkLabel1.Links[0].Visited = true;
        
        // Set the Text property to a string.
        this.linkLabel1.Text = "Register Online.  Visit Microsoft.  Visit MSN.";

        // Create new links using the Add method of the LinkCollection class.
        // Underline the appropriate words in the LinkLabel's Text property.
        // The words 'Register', 'Microsoft', and 'MSN' will 
        // all be underlined and behave as hyperlinks.

        // First check that the Text property is long enough to accommodate
        // the desired hyperlinked areas.  If it's not, don't add hyperlinks.
        if(this.linkLabel1.Text.Length >= 45)
        {
            this.linkLabel1.Links[0].LinkData = "Register";
            this.linkLabel1.Links.Add(24, 9, "www.microsoft.com");
            this.linkLabel1.Links.Add(42, 3, "www.msn.com");
        //  The second link is disabled and will appear as red.
            this.linkLabel1.Links[1].Enabled = false;
        }
        
        // Set up how the form should be displayed and add the controls to the form.
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {this.linkLabel1});
        this.Text = "Link Label Example";
    }

    private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
    {
        // Determine which link was clicked within the LinkLabel.
        this.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.
        string target = e.Link.LinkData as string;

        // If the value looks like a URL, navigate to it.
        // Otherwise, display it in a message box.
        if(null != target && target.StartsWith("www"))
        {
            System.Diagnostics.Process.Start(target);
        }
        else
        {    
            MessageBox.Show("Item clicked: " + target);
        }
    }
}

5、其他

LinkArea 设置超链接范围。如果只有一个LinkLabel只有一个超链接,就使用这个。
如果有多个,可以使用LinkArea设置第一个,其他的用linkLabel.Links.Add分别设置[如官方示例]。

this.linkLabel2.Links.Add(12,2,"www.baidu.com");
this.linkLabel2.Links.Add(15, 2, @"C:\");
this.linkLabel2.Links.Add(18, 2, "www.taobao.com");

当然,如果有多个,为了好理解,可以全部都用Add设置,不使用LinkArea

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值