Recent

Author Topic: TLabel or TStaticText? Which one and when to use it?  (Read 4112 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 121
TLabel or TStaticText? Which one and when to use it?
« on: February 02, 2023, 07:30:30 pm »
What's the difference with these two boxes?

Under what circumstances do I need to use a TLabel?

Under what circumstances do I need to use a TStaticText?

When choosing, what do I need to ask myself in order to make the right decision and place the correct control?
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

Curt Carpenter

  • Sr. Member
  • ****
  • Posts: 396
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #1 on: February 02, 2023, 08:27:29 pm »
"Google" and "Search" are your friends :)

As you're climbing the learning curve, I'd suggest sticking with TLabel.  I can remember only one time when I needed TStaticText, and it's been so long ago that I don't remember the circumstance.  (Lazarus is such a rich programming environment that you'll often have multiple ways of accomplishing things, with sometimes subtle differences in how they work.  Because I'm old, I have devolved into picking one alternative and sticking with it, brain cells being at a premium anymore.)

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #2 on: February 02, 2023, 08:34:31 pm »
The wiki has the information about them:
https://wiki.freepascal.org/TLabel
https://wiki.freepascal.org/TStaticText

If after reading the wiki still have no idea, you haven't really read it. It mentions TGraphicControl and TWinControl. What are the differences? Keep reading. All the information you need is already documented. Sooner or later, you will find it.  :D

As already suggested, for beginners ... stick with TLabel.
 
« Last Edit: February 02, 2023, 08:37:52 pm by Handoko »

OC DelGuy

  • Full Member
  • ***
  • Posts: 121
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #3 on: February 02, 2023, 09:56:14 pm »
"Google" and "Search" are your friends :)

As you're climbing the learning curve, I'd suggest sticking with TLabel.  I can remember only one time when I needed TStaticText, and it's been so long ago that I don't remember the circumstance.  (Lazarus is such a rich programming environment that you'll often have multiple ways of accomplishing things, with sometimes subtle differences in how they work.  Because I'm old, I have devolved into picking one alternative and sticking with it, brain cells being at a premium anymore.)

You know, I was expecting stuff that's not in the reference guide.  Like given the exact same text, this one uses more memory than that one.
Or maybe This one is much faster than that one.  Again, given the exact same text, if you have 100 of these they will print much faster than 100 of those.
You know, stuff that only experienced programmers would know.
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

OC DelGuy

  • Full Member
  • ***
  • Posts: 121
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #4 on: February 02, 2023, 10:09:27 pm »
The wiki has the information about them:
https://wiki.freepascal.org/TLabel
https://wiki.freepascal.org/TStaticText

If after reading the wiki still have no idea, you haven't really read it. It mentions TGraphicControl and TWinControl. What are the differences? Keep reading. All the information you need is already documented. Sooner or later, you will find it.  :D

As already suggested, for beginners ... stick with TLabel.

Yes, Handoko.  I read the docs.  After reading them, They appear to be the same.  But I would imagine that one is better suited during these types of situations.  I want to know what those situations are!  One might be faster to display on the screen than the other.  Which one?  Or the speed might be so close that it's negligible.  I don't know and I'm looking for advice on when to use one over the other!  One is a decendant of TGraphicsControl.  Does that mean that if I'm writing code that is graphics intensive, that I should choose the TLabel?  Is that even what that means?

I need all that kind of info.  Reference docs don't give that info.
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #5 on: February 02, 2023, 10:20:16 pm »
More memory saving TStaticText, more flexible TLabel. More speed, you will feel no difference I guess. TLabel can scale with DPI, about TStaticText I am unaware.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

OC DelGuy

  • Full Member
  • ***
  • Posts: 121
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #6 on: February 02, 2023, 10:29:04 pm »
More memory saving TStaticText, more flexible TLabel. More speed, you will feel no difference I guess. TLabel can scale with DPI, about TStaticText I am unaware.

Yep!  That's the stuff!  A good dose of experience!  Thanks Zwerg.
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #7 on: February 02, 2023, 11:07:34 pm »
I use it sometimes when I want to visually highlight results - with StaticText it's easy to set border.

In screenshot I attached, I just set Alignment to taRightJustify and BorderStyle to sbsSunken.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #8 on: February 02, 2023, 11:14:54 pm »
and since it has a window handle, external programs can scan your app and get the info! :D
The only true wisdom is knowing you know nothing

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #9 on: February 03, 2023, 12:27:13 am »
and since it has a window handle, external programs can scan your app and get the info! :D

Yet another advantage! 8-)

Seriously: IMHO, external programs can do a lot of things, so if it isn't some ultra secret software, it is better tactic to protect computer from such a programs than to think about this. They can also take screenshot and see all labels and statictexts.

domasz

  • Sr. Member
  • ****
  • Posts: 423
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #10 on: February 03, 2023, 01:06:05 am »
If you put TStaticLabel over TImage then this label's transparency won't work properly and it will have a solid background.
TLabel will have a proper transparent background.

You can put TStaticLabel over TPanel but TLabel will always be behind TPanel.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #11 on: February 03, 2023, 02:32:38 am »
... I need all that kind of info.  Reference docs don't give that info.

Yes it does. If you really read it, you will find:

Quote
It supports simple lightweight controls that do not need the ability to accept keyboard input or contain other controls. Since lightweight controls do not wrap GUI widgets, they use fewer resources than controls based on TWinControl.
TGraphicControl provides a Canvas property for access to the control's drawing surface and a virtual Paint method and an OnPaint handler, called in response to paint requests received by the parent control.
Override the Paint method or supply your own OnPaint handler, to do the actual drawing of the control.
Source: https://wiki.freepascal.org/TGraphicControl

Basically, that means:
- More lightweight than TWinControl descendants
- Can't accept keyboard input
- Can't contains other controls
- No GUI widget wrapping
- Use fewer resources
- Provide OnPaint handler

Use TLabel whenever you can. Use TStaticText only if TLabel does not provide the features you need, like keyboard input events or widget wrapping, like the things suggested by dselingo.

More memory saving TStaticText, more flexible TLabel.

Are you sure? Can you prove it? The documentation says differently.
« Last Edit: February 03, 2023, 03:11:47 am by Handoko »

Curt Carpenter

  • Sr. Member
  • ****
  • Posts: 396
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #12 on: February 03, 2023, 02:56:43 am »
If you use the "search" capability of the forum here, you will find years of experience regarding the use of things like statictext and label.   And Google will expand that knowledge base further in just about every case.    But...

As a teacher, I'd encourage you to prioritize functionality rather than optimization at this point on your way up the learning curve.  You'll make faster progress toward real proficiency, and have more fun along the way, by making things work first.  It's like writing a good paper:  get your first draft down on paper -- then revise and refine.  Different people have different learning styles of course, but that's the one that seems the best fit to a majority of us in my experience.



Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #13 on: February 03, 2023, 03:06:22 am »
@Curt Carpenter
+1

The forum really provides many useful information one needs to know. Spending sometime doing searching, one should find it.

Lazarus documentation is detailed and massive. With the power of Google, it can help finding the information easily.

Performance optimization is important, but that should be prioritized after functionality. Premature optimization is bad, very bad.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #14 on: February 03, 2023, 03:25:08 am »
My reply to:
More memory saving TStaticText, more flexible TLabel.

Are you sure?
On Windows, yes.

Can you prove it?
On Windows, yes. See Attachment.

The documentation says differently.
I did not wrote.

My simple testcode:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes , SysUtils , Forms , Controls , Graphics , Dialogs , StdCtrls ,
  9.   ExtCtrls;
  10.  
  11. type
  12.   TLabels = array of TLabel;
  13.   TStatics = array of TStaticText;
  14.  
  15. type
  16.  
  17.   { TForm1 }
  18.  
  19.   TForm1 = class(TForm)
  20.     Button1: TButton;
  21.     Panel1: TPanel;
  22.     RadioGroup1: TRadioGroup;
  23.     procedure Button1Click(Sender: TObject);
  24.     procedure FormCreate(Sender: TObject);
  25.   strict private
  26.     FLabels: TLabels;
  27.     FStatics: TStatics;
  28.   private
  29.     procedure GenerateLabels;
  30.     procedure GenerateStatics;
  31.   public
  32.  
  33.   end;
  34.  
  35. var
  36.   Form1: TForm1;
  37.  
  38. implementation
  39.  
  40. {$R *.lfm}
  41.  
  42. { TForm1 }
  43.  
  44. procedure TForm1.GenerateLabels;
  45. var
  46.   i: Integer;
  47. begin
  48.   SetLength(FLabels, 500);
  49.   for i := 0 to Pred(Length(FLabels)) do
  50.     begin
  51.       FLabels[i] := TLabel.Create(Self);
  52.       try
  53.         FLabels[i].Parent := Panel1;
  54.         FLabels[i].Visible := False;
  55.         FLabels[i].Caption := IntToStr(i);
  56.         FLabels[i].Align := alClient;
  57.       finally
  58.         FLabels[i].Visible := True;
  59.       end;
  60.     end;
  61. end;
  62.  
  63. procedure TForm1.GenerateStatics;
  64. var
  65.   i: Integer;
  66. begin
  67.   SetLength(FStatics, 500);
  68.   for i := 0 to Pred(Length(FStatics)) do
  69.     begin
  70.       FStatics[i] := TStaticText.Create(Self);
  71.       try
  72.         FStatics[i].Parent := Panel1;
  73.         FStatics[i].Visible := False;
  74.         FStatics[i].Caption := IntToStr(i);
  75.         FStatics[i].Align := alClient;
  76.       finally
  77.         FStatics[i].Visible := True;
  78.       end;
  79.     end;
  80. end;
  81.  
  82. procedure TForm1.Button1Click(Sender: TObject);
  83. begin
  84.   case RadioGroup1.ItemIndex of
  85.     0: GenerateLabels;
  86.     1: GenerateStatics;
  87.   end;
  88. end;
  89.  
  90. procedure TForm1.FormCreate(Sender: TObject);
  91. begin
  92.  
  93. end;
  94.  
  95. end.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018