Recent

Author Topic: Hyperlink labels [SOLVED]  (Read 9430 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Hyperlink labels [SOLVED]
« on: May 18, 2021, 06:21:23 pm »
لديّ هذه المحاولة البسيطة لعمل نموذج form يحوي بعض الروابط ولكن المشكلة هي أنّني أودّ عندما أنقر على الرابط أن ينفتح في مُستعرض الويب.

google translate:

"I have this simple attempt to create a form that contains some links, but the problem is "I want" that when I click on the link, it opens in my web browser."

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.  
  4.   label1.Caption:='Arabic FaceBook: https://www.facebook.com/people/Ahmed-Crow/100009232142912/';
  5.   label2.Caption:='English FaceBook: https://www.facebook.com/ahmed.crow.988/';
  6.   label3.Caption:='Blog: https://paracletus0de0evangelio.blogspot.com/';
  7. end;    
  8.  
« Last Edit: May 18, 2021, 07:28:57 pm by pascal111 »

Handoko

  • Hero Member
  • *****
  • Posts: 4286
  • My goal: build my own game engine using Lazarus
Re: Hyperlink labels
« Reply #1 on: May 18, 2021, 06:46:55 pm »
I haven't tried but I know you can open a link by using:
https://wiki.freepascal.org/Webbrowser#OpenURL

So you need to put the OpenURL command inside the TLabel.OnClick event.
« Last Edit: May 18, 2021, 06:49:42 pm by Handoko »

howardpc

  • Hero Member
  • *****
  • Posts: 3715
Re: Hyperlink labels
« Reply #2 on: May 18, 2021, 06:48:48 pm »
Generate an OnClick handler for each label by double-clicking on it in the object inspector.
Then add code such as
Code: Pascal  [Select][+][-]
  1. uses  LCLIntf;
  2. procedure TForm1.Label1Click(Sender: TObject);
  3. begin
  4.   OpenURL(Copy(Label1.Caption, 18, MaxInt));
  5. end;
The other labels will need a different value from "18" of course.

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Hyperlink labels
« Reply #3 on: May 18, 2021, 07:22:06 pm »
I haven't tried but I know you can open a link by using:
https://wiki.freepascal.org/Webbrowser#OpenURL

So you need to put the OpenURL command inside the TLabel.OnClick event.

نعم كما في مثال @howardpc

google translate:

"Yes, as in the @howardpc example"

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Hyperlink labels
« Reply #4 on: May 18, 2021, 07:28:26 pm »
Generate an OnClick handler for each label by double-clicking on it in the object inspector.
Then add code such as
Code: Pascal  [Select][+][-]
  1. uses  LCLIntf;
  2. procedure TForm1.Label1Click(Sender: TObject);
  3. begin
  4.   OpenURL(Copy(Label1.Caption, 18, MaxInt));
  5. end;
The other labels will need a different value from "18" of course.

لماذا إستخدمتَ MaxInt بالرغم أنّها الحد الأقصى للقيمة الصحيحة كما في الرابط https://wiki.freepascal.org/maxint أم تعني أنّها الحد الأقصى كذلكـ لطول القيمة الحرفيّة في الـ label.caption؟

google translate:

"Why do you use MaxInt even though it is the maximum valid "integer type" value as in https://wiki.freepascal.org/maxint or does it mean that it is also the maximum length of the literal "string" value in the label.caption?"

wp

  • Hero Member
  • *****
  • Posts: 8359
Re: Hyperlink labels
« Reply #5 on: May 18, 2021, 08:09:09 pm »
Why do you use MaxInt even though it is the maximum valid "integer type" value as in https://wiki.freepascal.org/maxint or does it mean that it is also the maximum length of the literal "string" value in the label.caption?"
In the copy() command, the last parameter is the count of chars to be copied. Suppose, you want to copy the substring starting at position 5 up to the end, and the source string is 10 characters long - this means you must copy the chars at position 5, 6, 7, 8, 9, 10 = 5 chars, or Length(sourcestring) - startpos. This calculation can be avoided because copy stops automatically when the last character of the source string has been copied; therefore, it is enough to simply use a sufficiently large number here, and MaxInt (maximum integer) is certainly larger than the string length).

BTW, for a typical hyperlink label you should also use a blue text color and a mouse-over effect which underlines the font whenever the mouse is over the hyperlink label. You can use the OnMouseEnter and OnMouseLeave events for this. And you should signal the hyperlink to the user by changing the mouse cursor to a hand. I apply this technique occasionally in my own programs - find the About box of my Corona program in the attachment. In the project this technique is also applied to images.
« Last Edit: May 18, 2021, 08:12:45 pm by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Hyperlink labels
« Reply #6 on: May 18, 2021, 09:34:08 pm »
Why do you use MaxInt even though it is the maximum valid "integer type" value as in https://wiki.freepascal.org/maxint or does it mean that it is also the maximum length of the literal "string" value in the label.caption?"
In the copy() command, the last parameter is the count of chars to be copied. Suppose, you want to copy the substring starting at position 5 up to the end, and the source string is 10 characters long - this means you must copy the chars at position 5, 6, 7, 8, 9, 10 = 5 chars, or Length(sourcestring) - startpos. This calculation can be avoided because copy stops automatically when the last character of the source string has been copied; therefore, it is enough to simply use a sufficiently large number here, and MaxInt (maximum integer) is certainly larger than the string length).

BTW, for a typical hyperlink label you should also use a blue text color and a mouse-over effect which underlines the font whenever the mouse is over the hyperlink label. You can use the OnMouseEnter and OnMouseLeave events for this. And you should signal the hyperlink to the user by changing the mouse cursor to a hand. I apply this technique occasionally in my own programs - find the About box of my Corona program in the attachment. In the project this technique is also applied to images.

حيلة ذكيّة مع الدالة copy.

برنامج رائع مُبرمج بطريقة ذكيّة ودقيقة تؤول إلى الكمال ،لا أدري عندما أُشاهد مثل هذه الروعة أن لو توجّهت إلى أمريكا في فرصة زيارةٍ لها أن هل أتعلّم البرمجة أم صناعة الأفلام وهل لو رجعت أرجع إلى مصر أم إلى السعوديّة ﻷُفيدَ بما تعلّمت أو ربّما ﻷصون ما قد أحمل معي من كنوز ،كان أبي يتمنّى لي الذهاب إلى الصين ويتشائم من ذهابي إلى أمريكا.

google translate:

"A clever trick with the copy function.

A wonderful program programmed in an intelligent and accurate way that leads to perfection, I do not know when I see such a wonderful thing that if I went to America on a visit to it, would I learn programming or film-making, and if I returned to "I'll return to" Egypt or Saudi Arabia, I would be informed "to benefit others" of what I learned, or perhaps I would preserve what I have learned. I carry with me some "of" treasures, my father used to wish me to go to China and pessimism about my going to America."

jamie

  • Hero Member
  • *****
  • Posts: 4569
Re: Hyperlink labels [SOLVED]
« Reply #7 on: May 18, 2021, 11:16:58 pm »
I don't know how much you can learn in America but I promise you if you look me up I can teach you how to play a good game of TENNIS!

I have a friend that is currently staying here for a time and he is also from your location and I also instructed him the game of Tennis..

 He claimed he already knew things about programming so I could not help him there!

 Have a good day.

The only true wisdom is knowing you know nothing

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Hyperlink labels [SOLVED]
« Reply #8 on: May 19, 2021, 12:35:13 am »
I don't know how much you can learn in America but I promise you if you look me up I can teach you how to play a good game of TENNIS!

موضوع التعلّم مرهون بكم سأمكُث في أمريكا وما الذي من مواهبي سيتفتّح هناكـ وكيف سيكون وضع الحياة هناكـ ﻷنّ الأمر بعدُ مجهول.

لا بأس بالتنس ما دُمتَ تعرف كيف تلعبها فسيُمكنكـ تعليمها لي وستكونُ شيئاً جديداً أُمارسُه.

google translate:

"The subject of learning depends on how long I will stay in America and what of my talents will unfold there - and how the situation of life will be there - because the matter is still unknown.

It's okay with tennis, as long as you know how to play it, you can teach it to me and it will be something new to play."

I have a friend that is currently staying here for a time and he is also from your location and I also instructed him the game of Tennis..

رُبّما أودّ أن أمكُث في أمريكا إلى نهاية الأيام ولكن بسبب ذلكـ على ما أظن يجب أن أعود مرّةً أخرى إلى الشرق الأوسط.

google translate:

"Maybe I would like to stay in America until the end of days, but because of that - I think I have to go back to the Middle East again."

He claimed he already knew things about programming so I could not help him there!

 Have a good day.

لقد كانت لديه الفرصة ﻷن يتعلّم من المصادر نفسها التي أنشأت لغات البرمجة ،هو حر أو تصرف كما يُعجبه.

فلتكن الـ bugs بعيدةً عنكـ.

google translate:

"He has had the opportunity to learn from the same sources that created programming languages, he is free or act as he likes.

Get "Let" the bugs "be" away from you."

PascalDragon

  • Hero Member
  • *****
  • Posts: 3059
  • Compiler Developer
Re: Hyperlink labels
« Reply #9 on: May 19, 2021, 09:18:28 am »
Why do you use MaxInt even though it is the maximum valid "integer type" value as in https://wiki.freepascal.org/maxint or does it mean that it is also the maximum length of the literal "string" value in the label.caption?"
In the copy() command, the last parameter is the count of chars to be copied. Suppose, you want to copy the substring starting at position 5 up to the end, and the source string is 10 characters long - this means you must copy the chars at position 5, 6, 7, 8, 9, 10 = 5 chars, or Length(sourcestring) - startpos. This calculation can be avoided because copy stops automatically when the last character of the source string has been copied; therefore, it is enough to simply use a sufficiently large number here, and MaxInt (maximum integer) is certainly larger than the string length).

Two remarks:
  • The length of a string is a SizeInt not a LongInt, so that code would in principle fail on a 64-bit system with a really long string
  • At least since the 3.0.x series FPC supports a two parameter Copy where you only need to specify the starting position (it's correctly mentioned in the daily documentation already, while the 3.2.0 one says that the Delphi mode is required (which is wrong))

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Hyperlink labels
« Reply #10 on: May 19, 2021, 06:33:34 pm »

Two remarks:
  • The length of a string is a SizeInt not a LongInt, so that code would in principle fail on a 64-bit system with a really long string
  • At least since the 3.0.x series FPC supports a two parameter Copy where you only need to specify the starting position (it's correctly mentioned in the daily documentation already, while the 3.2.0 one says that the Delphi mode is required (which is wrong))

- بالنّسبة لـ SizeInt فلقد توجّهت إلى هذا الرابط https://www.freepascal.org/docs-html/rtl/system/sizeint.html ولكن كلّ ما فهمته هو أنّ "type SizeInt = LongInt; " كما هو مُبيّن في الرابط أي لم أُدركـ الفارق صراحةً.

- بالنّسبة لتمرير بارامترين اثنين فقط - بارامتر الثابت الحرفي وبارامتر نقطة الإبتداء - إلى الدّالة copy فلقد نجحت محاولتي في ذلكـ كما هو مُبيّن في الكود التالي:

google translate:

"- For SizeInt, I went to this link https://www.freepascal.org/docs-html/rtl/system/sizeint.html But all I understood is that "type SizeInt = LongInt;" As shown in the link, meaning I did not explicitly realize the difference.

- As for passing only two parameters - the literal "string" constant parameter and the starting point parameter - to the copy function, my attempt succeeded - as shown in the following code:"

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.FormCreate(Sender: TObject);
  3.  
  4. var
  5.  
  6.   s1,s2:string;
  7.  
  8. begin
  9.  
  10.   s1:='Hello world!';
  11.   s2:=copy(s1,2);
  12.  
  13.   showmessage(s2);
  14.  
  15. end;          
  16.  

Output:

ello world!

howardpc

  • Hero Member
  • *****
  • Posts: 3715
Re: Hyperlink labels
« Reply #11 on: May 19, 2021, 07:37:24 pm »
"- For SizeInt, I went to this link https://www.freepascal.org/docs-html/rtl/system/sizeint.html But all I understood is that "type SizeInt = LongInt;" As shown in the link, meaning I did not explicitly realize the difference.
The documentation is not wholly correct.
 systemh.inc has these ifdefs:
Code: Pascal  [Select][+][-]
  1. {$ifdef CPU64}
  2.   SizeInt = Int64;
  3.   SizeUInt = QWord;
  4.   PtrInt = Int64;
  5.   PtrUInt = QWord;
  6.   ValSInt = int64;
  7.   ValUInt = qword;
  8.   CodePointer = Pointer;
  9.   CodePtrInt = PtrInt;
  10.   CodePtrUInt = PtrUInt;
  11.   TExitCode = Longint;
  12. {$endif CPU64}
  13.  
  14. {$ifdef CPU32}
  15.   SizeInt = Longint;
  16.   SizeUInt = DWord;
  17.   PtrInt = Longint;
  18.   PtrUInt = DWord;
  19.   ValSInt = Longint;
  20.   ValUInt = Cardinal;
  21.   CodePointer = Pointer;
  22.   CodePtrInt = PtrInt;
  23.   CodePtrUInt = PtrUInt;
  24.   TExitCode = Longint;
  25. {$endif CPU32}
The point PascalDragon makes is valid (if academic in this situation), and it is good to know that in most situations like this we can omit the third parameter to Copy() (except for those situations that  require it to be stated explicitly because the copy needs to be shorter than to the end of the string or array).

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Hyperlink labels
« Reply #12 on: May 19, 2021, 07:48:56 pm »
"- For SizeInt, I went to this link https://www.freepascal.org/docs-html/rtl/system/sizeint.html But all I understood is that "type SizeInt = LongInt;" As shown in the link, meaning I did not explicitly realize the difference.
The documentation is not wholly correct.
 systemh.inc has these ifdefs:
Code: Pascal  [Select][+][-]
  1. {$ifdef CPU64}
  2.   SizeInt = Int64;
  3.   SizeUInt = QWord;
  4.   PtrInt = Int64;
  5.   PtrUInt = QWord;
  6.   ValSInt = int64;
  7.   ValUInt = qword;
  8.   CodePointer = Pointer;
  9.   CodePtrInt = PtrInt;
  10.   CodePtrUInt = PtrUInt;
  11.   TExitCode = Longint;
  12. {$endif CPU64}
  13.  
  14. {$ifdef CPU32}
  15.   SizeInt = Longint;
  16.   SizeUInt = DWord;
  17.   PtrInt = Longint;
  18.   PtrUInt = DWord;
  19.   ValSInt = Longint;
  20.   ValUInt = Cardinal;
  21.   CodePointer = Pointer;
  22.   CodePtrInt = PtrInt;
  23.   CodePtrUInt = PtrUInt;
  24.   TExitCode = Longint;
  25. {$endif CPU32}
The point PascalDragon makes is valid (if academic in this situation), and it is good to know that in most situations like this we can omit the third parameter to Copy() (except for those situations that  require it to be stated explicitly because the copy needs to be shorter than to the end of the string or array).

لحظة من فضلكـ من أجل أن أُضيف إلى معلوماتي ،فما هي الـ systemh.inc فلقد جربتها كوحدة unit وكانت النتيجة رسالة خطأ ،ومن أين أتيت بهذا الكود أو كيف أتيت بهذه الشذرة من الكود والتي تبدو من الـ systemh.inc؟

google translate:

"For a moment please - to add to my information, what is systemh.inc? I tried it as a unit and the result was an error message, and where did I "you" get this code or how did I "you" get this bit (chunk) of code that looks like systemh.inc?"

howardpc

  • Hero Member
  • *****
  • Posts: 3715
Re: Hyperlink labels [SOLVED]
« Reply #13 on: May 19, 2021, 07:58:18 pm »
Probably simplest is in the Lazarus editor type:
Code: Pascal  [Select][+][-]
  1. var
  2. temp: SizeInt;
Place the cursor somewhere in SizeInt and press Alt-UpArrow.
The IDE finds and opens the relevant .inc file for you, and jumps you to the declaration.

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Hyperlink labels [SOLVED]
« Reply #14 on: May 19, 2021, 08:18:16 pm »
Probably simplest is in the Lazarus editor type:
Code: Pascal  [Select][+][-]
  1. var
  2. temp: SizeInt;
Place the cursor somewhere in SizeInt and press Alt-UpArrow.
The IDE finds and opens the relevant .inc file for you, and jumps you to the declaration.

هل تعلم الطريف في الأمر ؟! أنّي وجدت هذه الشذرة:

Code: Pascal  [Select][+][-]
  1. {$ifdef CPU16}
  2.   SizeInt = Integer;
  3.  
توقّعتُ أنْ أجد {$ifdef CPU8} :D

وهل تُوجد أجهزة إلى الآن CPU16؟!

google translate:

"Do you know the funny thing about it ?! I found this fragment (chunk):

Code: Pascal  [Select][+][-]
  1. {$ifdef CPU16}
  2.   SizeInt = Integer;
  3.  

I expected to find {$ ifdef CPU8}  :D

Are there devices yet CPU16 ?!"
« Last Edit: May 19, 2021, 08:20:14 pm by pascal111 »

 

TinyPortal © 2005-2018