打印本文 打印本文 关闭窗口 关闭窗口
【C#控件】LinkLabel控件使用方法
作者:佚名  文章来源:本站原创  点击数1565  更新时间:2012/2/23 10:43:18  文章录入:mintao  责任编辑:mintao
【C#控件】LinkLabel控件使用方法

作用:链接其他应用程序或者链接某个网站

其属性和具有功能:

0、连接上的文字,是用Text属性来设置。

1、针对超链接颜色的属性LinkColor、VisiteLinkColor、ActiveLinkColor

2、设置激活链接的区域LinkArea

3LinkClicked事件,确定选择链接后将发生的操作。

一、如何:使用 Windows 窗体 LinkLabel 控件链接到对象或网页

(一)使用LinkLabel控件链接另一个窗体:

1、将Text 设置为相应的标题;

2、设置LinkArea属性,确定标题哪部分作为相应的链接

LinkArea(a,b) ,a为标题起始位置,b为从起始位置开始向后b个字符。

例如:linklabel,    从第一个字母向后一共七个字母,那么就是LinkArea(0,7)     

3、在LinkClicked事件处理程序中,调用Show()方法以打开项目中另一窗体,并将LinkVisite属性设为TrueLinkVisite属性(链接显示访问过为True,显示没有被访问过为False)

4、form1窗口代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

        {

           

            Form2 frm2 = new Form2();//注意,调用另一个窗体时,必须要将另一个窗体实例化对象。

            frm2.Show();

        }

    }

}

(二)使用 LinkLabel 控件启动 Internet Explorer 并链接到 Web 页

1、将Text 设置为相应的标题;

2、设置LinkArea属性,确定标题哪部分作为相应的链接

3、在 LinkClicked 事件处理程序的异常处理块中间,调用将 LinkVisited 属性设置为 true 的第二个过程,并使用 Start 方法和一个 URL 启动默认浏览器。若要使用 Start 方法,需要添加对 System.Diagnostics 命名空间的引用。 System.Diagnostics.Process.Start()

4、相关代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

        {

           //调用另一个窗体

            Form2 frm2 = new Form2();

            frm2.Show();//show()方法

            //调用一个网页

        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

        {

            System.Diagnostics.Process.Start("http://www.liuzhihuablog.blog.suhu.com");//调用网页的方法

        }

}

截个图给大家:

调用Form2窗体

链接WEB网页:

打印本文 打印本文 关闭窗口 关闭窗口