首页 新闻 会员 周边

C# winform点击LinkLabel打开超链接问题。

0
悬赏园豆:10 [已解决问题] 解决于 2014-08-25 16:34

我在panel里边动态的添加了很多LinkLabel,点击每个LinkLabel都打开指定的超链接,不可能每个LinkLabel都写一个LinkClick事件吧?怎么实现点击所有的LinkLabel都实现同一个事件呢?有没有更好的方法?

RinSing.Feng的主页 RinSing.Feng | 初学一级 | 园豆:30
提问于:2014-08-25 15:22
< >
分享
最佳答案
0
收获园豆:5
519740105 | 大侠五级 |园豆:5810 | 2014-08-25 15:33

。。。内容找不到

RinSing.Feng | 园豆:30 (初学一级) | 2014-08-25 15:34

@RinSing.Feng: 再刷新看。下面是代码,你可以参考下:

 

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);
        }
    }
}
519740105 | 园豆:5810 (大侠五级) | 2014-08-25 15:35
其他回答(2)
0

那你在添加的时候就注册事件!

收获园豆:2
潮流还是非主流 | 园豆:306 (菜鸟二级) | 2014-08-25 15:25

不太懂,可以具体点!!!

支持(0) 反对(0) RinSing.Feng | 园豆:30 (初学一级) | 2014-08-25 15:27

@RinSing.Feng: http://blog.csdn.net/wonsoft/article/details/4351406 看看这个把,

LinkButton lb = new LinkButton();
lb.Text = "123assa";
lb.Command += lb_Command;
lb.CommandName = "click";
lb.Click += lb_Click;
Panel1.Controls.Add(lb);

 

 

支持(0) 反对(0) 潮流还是非主流 | 园豆:306 (菜鸟二级) | 2014-08-25 15:41
0
private void button1_Click(object sender, EventArgs e)
        {
            LinkLabel linkLabel1 = new LinkLabel();

            linkLabel1.AutoSize = true;
            linkLabel1.Location = new System.Drawing.Point(20, 14);
            linkLabel1.Name = "linkLabel1";
            linkLabel1.Size = new System.Drawing.Size(55, 13);
            linkLabel1.TabIndex = 0;
            linkLabel1.TabStop = true;
            linkLabel1.Text = "linkLabel1";
            linkLabel1.Click += linkLabel_Click;//注册事件

            LinkLabel linkLabel2 = new LinkLabel();
            linkLabel2.AutoSize = true;
            linkLabel2.Location = new System.Drawing.Point(23, 52);
            linkLabel2.Name = "linkLabel2";
            linkLabel2.Size = new System.Drawing.Size(55, 13);
            linkLabel2.TabIndex = 1;
            linkLabel2.TabStop = true;
            linkLabel2.Text = "linkLabel2";
            linkLabel2.Click += linkLabel_Click;//注册事件 和linkLabel1为同一个

            this.panel1.Controls.Add(linkLabel1);
            this.panel2.Controls.Add(linkLabel2);
        }

        void linkLabel_Click(object sender, EventArgs e)
        {
            LinkLabel ll = sender as LinkLabel;
            MessageBox.Show(ll.Name);//逻辑处理代码
        }
收获园豆:3
Firen | 园豆:5385 (大侠五级) | 2014-08-25 16:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册