-1

I'm trying to dynamically create a link from a Windows Form to our website when certain conditions are met (it's a warning message with further information in our online manual).

Currently I'm finding LinkLabel quite unwieldy to use in this situation: having to set up LinkClicked handlers on the fly for a straightforward hyperlink seems inelegant.

Is there a wrapper or alternative that fulfills the following requirements?:

  • Inherits from System.Windows.Forms.Control (so I can use it in a TableLayoutPanel)
  • Has reasonably low setup (no strange LinkClicked function pre-visit checking, for example)
  • Isn't bound to a specific browser
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Stu Pegg
  • 1,287
  • 2
  • 14
  • 41

3 Answers3

2

What is your problem with the LinkClicked event handler? You would have to do the same for almost any control in order to do anything useful.

Anyway, it would be trivial to implement yourself - create a class that inherits from LinkLabel, add a string URL property (you may need an attribute to make this show in the designer properties panel if you want to set it that way) and provide an event handler that opens the browser with that URL.

Then you can just add the control in the designer (or at runtime), set the URL property and it will work without having to set event handlers.

mdm
  • 12,480
  • 5
  • 34
  • 53
  • I was indeed considering writing a control that inherits from LinkLabel, but I was hoping there was an alternative (or an existing code sample) to save me the trouble. Inevitably I would have coded such only to discover there was an alternative class all along. – Stu Pegg Jul 01 '11 at 15:16
  • 2
    AFAIK there isn't anything unless you want a 3rd party component (which your previous comments rule out). I can see why you think the LinkLabel is unwieldy - you could always set the event handler with an anonymous function (nice example at http://stackoverflow.com/questions/4359629/is-there-anything-wrong-with-using-lambda-for-winforms-event), which looks a bit cleaner and lets you set the URL on-the-fly when the control is created/event handler set. – mdm Jul 01 '11 at 15:34
1

Did you use the LinkClicked event instead of OnClick? Then you can use this in the event handler:

    (sender as LinkLabel).LinkVisited = true;
    System.Diagnostics.Process.Start("http://example.com");

It's not bound a specific browser - opens in the user's default browser. The setup is low - just instantiate the LinkLabel, add an event hookup to LinkClicked (which is one two-line method) and add it to the page. What's unwieldy about this approach?

Nick B
  • 1,101
  • 9
  • 19
  • Ah, I think that's too much time with JavaScript lately I my part: LinkClicked is the name of the event I was thinking of (editing now). – Stu Pegg Jul 01 '11 at 15:11
  • No problem. I'm not sure what your hesitation is with this - could you explain more fully? – Nick B Jul 01 '11 at 15:15
  • It's the need to create the label mid-code in possibly more than one instance (with different links) that makes creating those event handlers awkward. – Stu Pegg Jul 01 '11 at 15:24
0

In the end I used LinkLabel.Links.Add to modify the link destination dynamically..

Stu Pegg
  • 1,287
  • 2
  • 14
  • 41