c# - Is there any way to place multiple links in the LinkLabel control in Windows Forms -
is there way place multiple links in linklabel
control in windows forms?
if set this
this.linklabel.text = ""; foreach (string url in urls) { this.linklabel.text += url + environment.newline; }
it merges 1 link.
thanks in advance.
yes, though there isn't way can tell directly designer, easy manage via code:
var linklabel = new linklabel(); linklabel.text = "(link 1) , (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);
basically, links collection on label can host bunch of links in linklabel
. linkclicked
event contains reference specific link clicked can access link data associated link, among other things.
the designer exposes linkarea
property defaults include of text of linklabel
. first link
add links
collection automatically change linkarea
property reflect first link in collection.
something little closer you're asking this:
var addresses = new list<string> { "http://www.example.com/page1", "http://www.example.com/page2", "http://www.example.com/page3", }; var stringbuilder = new stringbuilder(); var links = new list<linklabel.link>(); foreach (var address in addresses) { if (stringbuilder.length > 0) stringbuilder.appendline(); // cannot add new linklabel.link linklabel yet because // there no text in label yet, label complain // link location being out of range. we'll temporarily store // links in collection , add them later. links.add(new linklabel.link(stringbuilder.length, address.length, address)); stringbuilder.append(address); } var linklabel = new linklabel(); // must set text before add 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); };
i'm attaching url linkdata
link's i'm creating in loop can extract out string when linkclicked
event fired.
Comments
Post a Comment