13

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

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • Mason, I encourage you to put the question back to my edit. Your question really isn't about TLinkLabel, and neither is the answer you accepted. Re-read your second paragraph, which contains the only question in your text. Your first paragraph is just background. TLinkLabel already works. – Rob Kennedy Feb 12 '09 at 22:00
  • 1
    I don't see it that way. When you create something that takes HTML as input, and outputs a clickable hyperlink, you expect to see web-browser-style behavior: "Click this link and a page loads." POLS and all that. But it turns out that's not what happens. So my question is about how to make it work. – Mason Wheeler Feb 13 '09 at 00:00
  • Most people use TLinkLabels like you would a button, not to launch the default web browser. A TLinkLabel works just fine, what you wanted to know how to do is launch to a web browser in the InLinkClick event. Your question title is akin to asking "how make button work" – Jim McKeeth Feb 13 '09 at 00:27
  • 1
    @MasonWheeler- TLinkLabel is very lame. Check google for Delphi TInternelLabel. It is a much better alternative. With this control you won't have to write code in the OnClick event. – Gabriel Jul 24 '18 at 11:33

7 Answers7

22

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
  • 4,027
  • 1
  • 25
  • 44
  • 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 Feb 12 '09 at 17:42
  • 2
    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 Feb 12 '09 at 17:52
  • @Mason: Ken already answered your question about 'Open'. @Ken: Tnx. – Cesar Romero Feb 12 '09 at 17:57
  • It seems the OnClick event is used instead of OnLinkClick which provides a `Link` parameter which contains the href adress from the HTML marked-up caption. – Tupel Oct 04 '16 at 10:43
8

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
  • 1,622
  • 19
  • 30
5

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.

Community
  • 1
  • 1
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
2

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...

1

I tried this solution but it still gave problems in Delphi XE4, probably becasue ShellOpen does not understand the HTML-code in the Caption. What worked for me was a combination of Cesar Romero (the basic code), Adam Feistner (The HTML-code in the Caption) and an older solution:


  • Put the URL in the HINT field.
  • Change the line: ShellOpen(LinkLabel.Caption); to ShellOpen(LinkLabel.Hint);

This worked for me.

cvz
  • 139
  • 1
  • 7
  • 1
    You are probably using the wrong event. You should use the OnLinkClick event which has a `Link` property that has the href adress. – Tupel Oct 04 '16 at 10:36
1

I use a control called TInternetLabel instead. It does exactly what you want: on click it opens the browser so you don't have to put code in the OnClick event.

Gabriel
  • 20,797
  • 27
  • 159
  • 293
0

In .Caption link you need to format yourself in html < a href = ...

YT: Delphi 027 LinkLabel / 01 Link

mih
  • 81
  • 1
  • 4