Question

How do I make a TLabel behave like a hyperlink in Delphi?

Note: I'm not interested in using TLinkLabel because of backwards compatibility issues.

Was it helpful?

Solution

Colour it blue, set style to underline and add an OnClick event!

procedure TForm1.Label1Click(Sender: TObject);
var
  MyLink: string;
begin
  MyLink := 'http://www.mysite.com/';
  ShellExecute(Application.Handle, PChar('open'), PChar(MyLink),    
   nil, nil, SW_SHOW);
end;

OTHER TIPS

It depends on what you require of your hyperlinks. I'd just...

  • set the font color to blue
  • use the OnMouse[Enter|Leave|Move] events to appropriately apply the underline style to the font
  • use the OnClick event to spawn a browser & change the font color, as desired.

What version of Delphi are you using? Looking at my Delphi 4 IDE, TLabel has no OnMouseEnter/OnMouseLeave event, which would be necessary to change the cursor to a "Hand" when the user hovers over the "link".

It does have the OnClick event, which you can wire up to launch the user's web browser:

How to bring front or launch browser in Delphi

One can tab to and give focus to links in a browser. Therefore I would consider using a windowed control (like an owner-drawn TButton) for this task.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top