有没有办法在Windows窗体中的LinkLabel控件中放置多个链接

有没有办法在Windows窗体中的LinkLabel控件中放置多个链接?

如果我只是这样设置它

 this.linkLabel.Text = ""; foreach (string url in urls) { this.linkLabel.Text += url + Environment.NewLine; } 

它将它合并为一个链接。

提前致谢。

是的,虽然没有办法让我直接从设计师那里做到,但通过代码很容易管理:

 var linkLabel = new LinkLabel(); linkLabel.Text = "(Link 1) and (Link 2)"; linkLabel.Links.Add(1, 6, "Link data 1"); linkLabel.Links.Add(14, 6, "Link data 2"); linkLabel.LinkClicked += (s, e) => Console.WriteLine(e.Link.LinkData); 

基本上,标签上的Links集合可以在LinkLabel托管一堆链接。 LinkClicked事件包含对单击的特定链接的引用,因此您可以访问与链接关联的链接数据等。

设计者只公开一个LinkArea属性,默认包含LinkLabel所有文本。 您添加到Links集合的第一个Link将自动更改LinkArea属性以反映集合中的第一个链接。

更接近你问的东西看起来像这样:

 var addresses = new List { "http://www.example.com/page1", "http://www.example.com/page2", "http://www.example.com/page3", }; var stringBuilder = new StringBuilder(); var links = new List(); foreach (var address in addresses) { if (stringBuilder.Length > 0) stringBuilder.AppendLine(); // We cannot add the new LinkLabel.Link to the LinkLabel yet because // there is no text in the label yet, so the label will complain about // the link location being out of range. So we'll temporarily store // the links in a collection and add them later. links.Add(new LinkLabel.Link(stringBuilder.Length, address.Length, address)); stringBuilder.Append(address); } var linkLabel = new LinkLabel(); // We must set the text before we add the links. linkLabel.Text = stringBuilder.ToString(); foreach (var link in links) { linkLabel.Links.Add(link); } linkLabel.AutoSize = true; linkLabel.LinkClicked += (s, e) => { System.Diagnostics.Process.Start((string)e.Link.LinkData); }; 

我将URL本身作为LinkData到我正在循环中创建的链接,因此当LinkClicked事件被触发时,我可以将其作为字符串提取出来。