views:

1444

answers:

3

I put a TLinkLabel on my form, filled it in with a caption including a valid HTML link, and got some nice blue underlined text. When I ran the program, I expected it to invoke Firefox (my default browser) and open the link automatically. Apparently that's not the case.

The helpfile says I have to code this in an OnLinkClick event handler. It doesn't say anything about how to do that, though. It'll pass in a string value called "Link". How do I say "invoke the default browser and have it open Link"?

+10  A: 

You can call ShellExecute. I wrote this method for generic calls, and should works in your case.

procedure ShellOpen(const Url: string; const Params: string = '');
begin
  ShellAPI.ShellExecute(0, 'Open', PChar(Url), PChar(Params), nil, SW_SHOWNORMAL);
end;

In your code you should call this

procedure TForm1.LinkLabelClick(Sender: TObject);
begin
  ShellOpen(LinkLabel.Caption);
end;
Cesar Romero
Thanks! That's a good function. Though, looking at the documentation, it looks like you can get away with passing nil to the 2nd parameter. Any particular reason to explicitly say "Open"?
Mason Wheeler
You're telling ShellExecute() which action to perform with the third parameter. If you just specify nil instead of an action, it will use whatever the default action for that third parameter instead. In the case of a URL, the default is usually open. That's not true for other things, though.
Ken White
@Mason: Ken already answered your question about 'Open'.@Ken: Tnx.
Cesar Romero
@Cesar: You're welcome. <g> Nice answer you gave, too.
Ken White
+3  A: 

TLinkLabel provides a label that looks like a link. It's your job as the programmer to make it act like a link because only you can know what links are supposed to act like in your program. You wanted the label to automatically open the user's default Web browser using the URL in the label, but that's not the only thing links do. For example:

  • Internet Explorer is not my default browser, but when I click a link in Internet Explorer, I do not expect the linked page to open in Firefox.
  • When I click a link in the help program, I expect the linked topic to appear in the help program, not in any Web browser at all.
  • The preference pages in Eclipse are very complicated. Settings on one page are sometimes related to settings on another page. There are links on those pages that take the user directly to the related page. There is no URL and no HTML involved in this case, and yet they're still labels with underlined text.

Some programs try to offer a choice between opening links in new windows versus re-using old windows. You can't implement that feature without knowing which browser is in use. Your program might offer the user a choice to ignore the default browser setting and always use a specific one. To do that, your UI control can't make too many assumptions about what the program is supposed to do.

I'm guessing you're referring to a TLinkLabel control that comes with Delphi. (My versions don't have such a component.) I imagine that the Delphi control is meant to mimic the one in the .Net class library. It can hold multiple links, and each link can do something different.

If you want a control that always does the shell's default action for URLs, then consider using a different TLinkLabel; the one by Alexander Bach does exactly what you expected. It's from Delphi 3, but it should work unmodified in all later versions as well, including Delphi 2009. If you look at the code, you'll see how it works. It simply calls ShellExecute, as Cesar's answer demonstrates.

Rob Kennedy
A: 

Hello,

I have all sorts of problems with TLinkLabel that ships with delphi 2010. a) The control does not render as a hyperlink but as a simple label text on the form. b) the cursor does not change to point out this is a link even though I set the Cursor property. c) the OnLinkClick event does not fire at all. I am working on windows 7.

So, as far as I am concerned, TLinkLabel does nothing as it should and is useless. ShellExecute is the only solution and must be placed in the OnClick event.

dpant
You need to write the link as an HTML hyperlink tag. That's what makes it show up.
Mason Wheeler