在C#中使用Delegate打开链接

时间:2016-05-12 20:08:09

标签: c# winforms

我正在尝试在网络浏览器中启用我的链接。

for循环中的委托一切正常,但在eventGen()方法中,我试图获取单击链接的名称,其中一个链接将字符串nameOfLink作为...

  

" System.Windows.Forms.LinkLabel,Text:Quora For Programmers"

使用if语句验证所选名称并打开链接所有if语句总是被跳过,即使我将文本更改为

nameOfLink.Equals("Quora For Programmers");


namespace VisitedSites
{
    public partial class frmRecentlyVisitedSites : Form
    {
        // Create an array of Link labels
        LinkLabel[] sites = new LinkLabel[3];

        // Create links with the tool tips
        LinkLabel lnkQuora = new LinkLabel();
        ToolTip quora = new ToolTip();

        // Create links with the tool tips
        LinkLabel lnkIndeed = new LinkLabel();
        ToolTip indeed = new ToolTip();

        // Create links with the tool tips
        LinkLabel lnkHackerRank = new LinkLabel();
        ToolTip hackerRank = new ToolTip();

        public frmRecentlyVisitedSites()
        {
            InitializeComponent();

        }

        private void frmRecentlyVisitedSites_Load(object sender, EventArgs e)
        {
            // Populate the array with links 
            sites[0] = lnkQuora;
            quora.SetToolTip(lnkQuora, "Quora For Questions!");
            sites[1] = lnkIndeed;
            indeed.SetToolTip(lnkIndeed, "Qualified For Jobs!");
            sites[2] = lnkHackerRank;
            hackerRank.SetToolTip(lnkHackerRank, "Comfortable Coding?");

            // Configure the LinkLabel's properties. 
            this.lnkQuora.Location = new System.Drawing.Point(37, 36);
            this.lnkQuora.AutoSize = true;
            this.lnkQuora.Text = "Quora For Programmers";

            // Configure the LinkLabel's properties. 
            this.lnkIndeed.Location = new System.Drawing.Point(37, 56);
            this.lnkIndeed.AutoSize = true;
            this.lnkIndeed.Text = "Indeed For The Skilled";

            // Configure the LinkLabel's properties. 
            this.lnkHackerRank.Location = new System.Drawing.Point(37, 76);
            this.lnkHackerRank.AutoSize = true;
            this.lnkHackerRank.Text = "Hacker Rank For The Robust";

            for (int i = 0; i < sites.Length; i++)
            {
                this.Controls.Add(sites[i]);
                sites[i].LinkClicked += new LinkLabelLinkClickedEventHandler(EventGen);
            }

        }
        private void EventGen(object sender, LinkLabelLinkClickedEventArgs e)
        {
            // Determine the name of the link
            string nameOfLink = sender.ToString();

            // process the link depending on the name
            if(nameOfLink.Equals("System.Windows.Forms.LinkLabel, Text:Quora For Programmers", StringComparison.InvariantCultureIgnoreCase))
            {
                lnkQuora.LinkVisited = true;
                System.Diagnostics.Process.Start("https://www.quora.com/");
            }
            else if(nameOfLink.Equals("System.Windows.Forms.LinkLabel, Text:Indeed For The Skilled", StringComparison.InvariantCultureIgnoreCase))
            {
                lnkQuora.LinkVisited = true;
                System.Diagnostics.Process.Start("https://www.indeed.com/");
            }
            else if(nameOfLink.Equals("System.Windows.Forms.LinkLabel, Text:Hacker Rank For The Robust", StringComparison.InvariantCultureIgnoreCase))
            {
                lnkQuora.LinkVisited = true;
                System.Diagnostics.Process.Start("https://www.hackerrank.com/");
            }


        }
    }
}

2 个答案:

答案 0 :(得分:0)

为什么不添加链接,例如&#34; https://www.quora.com/&#34;到链接数据如link.LinkData =&#34; https://www.quora.com/&#34;,然后使用Process.Start打开该链接(e.Link.LinkData作为字符串)?

答案 1 :(得分:0)

刚试过你的代码,你错过了一个空间 是

System.Windows.Forms.LinkLabel, Text: Quora For Programmers

而不是

System.Windows.Forms.LinkLabel, Text:Quora For Programmers