我试图让我的链接打开在一个网页浏览器。
对于for循环中的委托,一切都很好,但是在eventGen()方法中,我尝试获取单击的链接的名称,其中一个链接将字符串nameOfLink作为.
"System.Windows.Forms.LinkLabel,文本:程序员的Quora“
在使用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/");
}
}
}
}发布于 2016-05-12 20:24:35
刚刚尝试了您的代码,您缺少的一个空格是
System.Windows.Forms.LinkLabel, Text: Quora For Programmers而不是
System.Windows.Forms.LinkLabel, Text:Quora For Programmers发布于 2016-05-12 20:19:34
为什么不将链接,例如"https://www.quora.com/“添加到link.LinkData = "https://www.quora.com/”之类的链接数据中,然后使用Process.Start(e.Link.LinkData作为字符串)打开该链接?
https://stackoverflow.com/questions/37196489
复制相似问题