1

can someone please explain to me how i can get the Text property of the linklabel that i have created at runtime?

I have tried:

string str = e.Link.LinkData;

...but that just displays an empty messagebox.

Thanks lots :)

jay_t55
  • 11,362
  • 28
  • 103
  • 174

2 Answers2

2

Since you have a mysterious "e" in your code, I assume you're trying to do this in the LinkLabel's LinkClicked event. To do this, you need to cast "sender" as a LinkLabel, like so:

private void linkLabel1_LinkClicked(object sender, 
    LinkLabelLinkClickedEventArgs e)
{
    LinkLabel lnklbl = (LinkLabel)sender;
    string str = lnklbl.Text;
}
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
1

EDIT: Now that we know the type of e, try:

string str = e.Link.Description;

If the LinkLabel.Link doesn't have enough information, you'll have to refer to the LinkLabel itself. That may be the sender of the event (as suggested by MusiGenesis) but if it's not, I suggest you use a lambda expression or anonymous method to subscribe to the event - that way you can capture the LinkLabel and refer to the Text property directly.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • private void llabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { string str = e.Text; } Yes Mr Skeet, I have. But it comes up with a squiggly red line under 'Text' when I use the above code. – jay_t55 Sep 12 '09 at 14:19
  • 1
    If you'd explained the type of `e` to start with, that would have helped. Editing... – Jon Skeet Sep 12 '09 at 14:22
  • I just tried this, and e.Link.Description returns a blank string. – MusiGenesis Sep 12 '09 at 15:21
  • It's possible that it was accepted due to the "capture" part of the answer. Hard to say. – Jon Skeet Sep 12 '09 at 15:34