tags:

views:

74

answers:

2

Possible Duplicate:
Open a URL from Windows Forms

Hello!

I have a C# desktop application and I want to be able to have a link in it that will open a new browser window/tab (on the system default browser) in a specific webpage. I've looked for this in the web but haven't found anything yet. Any help?? thanks...

+6  A: 

If you Process.Start the url, that should do the same as ShellExecute, which is the way you'd do it in native code.

You could use a LinkLabel from the toolbox, to get the link onto the form with the proper behaviour. Example code here.

Simpler version:

  private void Form1_Shown (object sender, EventArgs e)
  {
     linkLabel1.Links.Add (0, 7, "http://bobmoore.mvps.org/");
     linkLabel1.LinkClicked += new LinkLabelLinkClickedEventHandler(linkLabel1_LinkClicked);
  }

  private void linkLabel1_LinkClicked (object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
  {
     this.linkLabel1.Links[linkLabel1.Links.IndexOf (e.Link)].Visited = true;
     string target = e.Link.LinkData as string;

     System.Diagnostics.Process.Start (target);
  }
Bob Moore
Actually it's not 'alternatively'. I LinkLabel doesn't automatically open an URL in the default browser. You'd still need to use Process.Start to do that.
Sani Huttunen
Hmm, I'd always assumed that's what it would do, but the MS code suggests you have to use an event handler - yep, just tried it, the thing just sits there and does nothing <sigh>. I've edited accordingly.
Bob Moore
It is not certain that a link is associated with an URL. You might use a LinkLabel to display another form, go to a certain tab or something else that has nothing to do with browsers. You can also associate several URLs with the LinkLabel. Which should it navigate to if you click on the link? You could look at the LinkLabel as a Button with a special property: Links.
Sani Huttunen
Yeah, I looked at their example and you can add several links (that's why I've edited in a simpler version into my answer). Seems to me like they could have had a SimpleLinkLabel as well, with a single URL as a property, and that would have satisfied 95% of the use cases... and left people who actually need the fancy functionality to struggle with the more powerful linklabel. Hey ho, thanks for the clarification. Learned something today :)
Bob Moore
Oh, and I upvoted you as well.
Bob Moore
+3  A: 

You should use a LinkLabel control and Process.Start.
Here is an example how to use it.

PS. You really should start accepting answers if you want to get help in the future.

Sani Huttunen
yes. thank you. I just haven't seen them yet. I upvoted both and accepted your answer as correct. Again, thank you both for your fast answers.