How do I make a TLinkLabel work in Delphi?

Mason Wheeler picture Mason Wheeler · Feb 12, 2009 · Viewed 16.7k times · Source

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"?

Answer

Cesar Romero picture Cesar Romero · Feb 12, 2009

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;