Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a TLinkLabel work in Delphi?

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

like image 246
Mason Wheeler Avatar asked Feb 12 '09 16:02

Mason Wheeler


2 Answers

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;
like image 103
Cesar Romero Avatar answered Sep 28 '22 05:09

Cesar Romero


LOL, it's funny. So instead of setting crHandPoint as cursor, colored and underlined font and filling the OnClick event to standard TLabel we have component that knows link tag and which at all I need to supply with same On(Link)Click event :))

Only thing it is good for is that it makes easier to embed link into some text and that it is using system style of link...

p.s.: really you have to put Some text with <a href="some URL">link</a> into the Caption and setup OnLinkClick to that ShellExecute...

like image 31
Adam Feistner Avatar answered Sep 28 '22 05:09

Adam Feistner