Recent

Author Topic: URL Link from a Label Caption  (Read 507 times)

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 278
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
URL Link from a Label Caption
« on: November 12, 2024, 07:17:14 pm »
I thought this wouldn't be a complicated issue but can't find the equivalent in Laz for ShellExecute like used to be in Delphi 7. I think I used to use this but it's been a long time ago. See the Click Procedure code below which Laz errors: Identifier not found "ShellExecute" and also on the same line Error: Identifier not found "SW_SHOWNORMAL":

Code: Pascal  [Select][+][-]
  1. procedure TFrmAboutPMS.LblNFPAClick(Sender: TObject);
  2. var
  3.   strURL  : string;
  4. begin
  5.   strURL:= LblNFPA.Caption;
  6.   if Pos('https:\\', strURL) = 0 then
  7.     strURL:='https:\\'+strURL;
  8.   ShellExecute(handle,'open','extract strURL',nil,nil,SW_SHOWNORMAL);
  9.   //ShellExecute(0,nil,PChar(strURL),nil,nil,SW_SHOWNORMAL);
  10. end;
  11.  
« Last Edit: November 12, 2024, 07:31:09 pm by 1HuntnMan »

BSaidus

  • Hero Member
  • *****
  • Posts: 601
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: URL Link from a Label Caption
« Reply #1 on: November 12, 2024, 07:21:06 pm »
Did you added windows in uses clause

Code: Pascal  [Select][+][-]
  1.  
  2. uses
  3.   Windows, ...
  4.  
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Thaddy

  • Hero Member
  • *****
  • Posts: 16199
  • Censorship about opinions does not belong here.
Re: URL Link from a Label Caption
« Reply #2 on: November 12, 2024, 07:30:01 pm »
It is also one of the first examples given in Delphi in 1996... How long is probably Chinese? Without disrespect to Chinese users, it it common to to refer to Chinese beijng complex, beijng is an intentional joke  :-X )
If I smell bad code it usually is bad code and that includes my own code.

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 278
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: URL Link from a Label Caption
« Reply #3 on: November 12, 2024, 07:43:35 pm »
Woops, yup, forgot to use Winders.  Added that and compiles but the Label doesn't react to the OnClick. I wonder if I should use TStaticText instead?  I'm trying that...
Code: Pascal  [Select][+][-]
  1. procedure TFrmAboutPMS.StaticTxtNFPAClick(Sender: TObject);
  2. var
  3.   strURL  : string;
  4. begin
  5.   strURL:= StaticTxtNFPA.Caption;
  6.   if Pos('https:\\', strURL) = 0 then
  7.     strURL:='https:\\'+strURL;
  8.   ShellExecute(handle,'open','extract strURL',nil,nil,SW_SHOWNORMAL);
  9.   //ShellExecute(0,nil,PChar(strURL),nil,nil,SW_SHOWNORMAL);
  10.   //ShellExecute(handle,'open','extract DBEditClientURL',nil,nil,SW_SHOWNORMAL);}
  11. end;
  12.  

Compiles but doesn't react to OnClick

BSaidus

  • Hero Member
  • *****
  • Posts: 601
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: URL Link from a Label Caption
« Reply #4 on: November 12, 2024, 08:06:32 pm »
Use a simple label,
it should work,
just do
'http://' 
  rather then
'http:\\'

Code: Pascal  [Select][+][-]
  1.   ShellExecute(0, 'open', PChar(label1.Caption), Nil, Nil, SW_SHOWNORMAL);
  2.  
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Bart

  • Hero Member
  • *****
  • Posts: 5469
    • Bart en Mariska's Webstek
Re: URL Link from a Label Caption
« Reply #5 on: November 12, 2024, 10:49:19 pm »
Why don't you use OpenURL()?

Bart

TRon

  • Hero Member
  • *****
  • Posts: 3650
Re: URL Link from a Label Caption
« Reply #6 on: November 13, 2024, 12:55:24 am »
@Bart:
Must be a firm decision as something similar was asked (and same openURL suggested by me) a couple of days ago by TS here.

@1HuntnMan:
I see you do exactly the same as in the other thread.

Could you enlighten us (or at least me because I am truly puzzled) , what in earth does 'extract strURL' actually do ?

Is that a tool registered to the URL mimetype on your system or something ?

edit:
Just make a simple test to check if things work as expected
Code: Pascal  [Select][+][-]
  1. procedure TFrmAboutPMS.StaticTxtNFPAClick(Sender: TObject);
  2. var
  3.   strURL  : string;
  4. begin
  5.   strURL:= 'https://www.google.com';
  6.   ShellExecute(0,nil,PChar(strURL),nil,nil,SW_SHOWNORMAL);
  7. end;
  8.  
If that works for you then work on obtaining the URL from the caption and modifying it. If the text in the string isn't an actual URL then shellexecute does not know what to actually do with that string either.

« Last Edit: November 13, 2024, 01:30:12 am by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1431
    • Lebeau Software
Re: URL Link from a Label Caption
« Reply #7 on: November 13, 2024, 03:38:44 am »
the Label doesn't react to the OnClick.

Did you check to make sure you have your handler assigned to the OnClick event? Have you put a breakpoint in it and try to debug it?

I wonder if I should use TStaticText instead?

I wouldn't. TStaticText is a windowed control, so it uses more resources than a TLabel does.

Compiles but doesn't react to OnClick

How do you know? Your code is not doing anything if ShellExecute() fails. Like passing a malformed url to it...
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018