Lazarus

Programming => General => Topic started by: OC DelGuy on February 02, 2023, 07:30:30 pm

Title: TLabel or TStaticText? Which one and when to use it?
Post by: OC DelGuy 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?
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: Curt Carpenter 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.)
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: Handoko on February 02, 2023, 08:34:31 pm
The wiki has the information about them:
https://wiki.freepascal.org/TLabel (https://wiki.freepascal.org/TLabel)
https://wiki.freepascal.org/TStaticText (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.
 
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: OC DelGuy 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.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: OC DelGuy on February 02, 2023, 10:09:27 pm
The wiki has the information about them:
https://wiki.freepascal.org/TLabel (https://wiki.freepascal.org/TLabel)
https://wiki.freepascal.org/TStaticText (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.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: KodeZwerg 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.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: OC DelGuy 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.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: dseligo 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.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: jamie 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
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: dseligo 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.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: domasz 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.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: Handoko 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 (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.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: Curt Carpenter 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.


Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: Handoko 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.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: KodeZwerg 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.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: dsiders on February 03, 2023, 03:26:34 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.

It's TStaticText and not TStaticLabel.

I'm using Windows, and both allow transparency and display just fine over an image. Both display just fine on a Panel... even on top of an image on a panel.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: lainz on February 03, 2023, 03:36:20 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.

It's TStaticText and not TStaticLabel.

I'm using Windows, and both allow transparency and display just fine over an image. Both display just fine on a Panel... even on top of an image on a panel.

I reported a bug about that feature that transparency on TCustomControl descendants does not have transparency on Gtk2 Linux.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: KodeZwerg on February 03, 2023, 03:51:46 am
@Handoko
Another test of me that visualize the memory difference a little bit better.
I created 5000 of each this time.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: Handoko on February 03, 2023, 05:06:46 am
Thank you KodeZwerg for providing the test.

As the name suggests, TWinControl is for windowed controls. For that reason, it will provide some 'extra' things for the ability to contain other controls (like TPanel). TGraphicsControl is simply for showing things on screen. So in theory, TLabel should be more lightweight TStaticText.

I am a bit busy now, I will inspect the case later.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: OC DelGuy on February 03, 2023, 07:36:14 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.

This makes a lot of sense.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: domasz on February 03, 2023, 09:14:57 am
Both display just fine on a Panel... even on top of an image on a panel.
Not "on" TPanel, but "over". Like on my screenshot and then try to bring TLabel to front- you can't.
TStaticText seems transparent but not really- look how it renders. Part of its background should show TPanel.

Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: tetrastes on February 03, 2023, 10:13:38 am
LCL tries to follow the theming of underlying widget set. When you begin to play with colors, especially with descendants of TWinControl (Tpanel in particular), strange things happen.

PS. I suspect that the behaviour of overlapping controls is not the priority task of Lazarus team. ;)
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: 440bx on February 03, 2023, 10:31:20 am
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?
I'm going to answer your question from a Windows API point of view.

The problem with that kind of answer is that the LCL contains code that adds some features not present when using the Windows API (unless the programmer him/herself writes the code to implement them, e.g, hit testing on a non-windowed control)

The most important difference is that TLabel is NOT a windowed control whereas TStaticText is.  Whether or not the control is windowed is the crucial difference that drives the choice.

Using that crucial difference to make the determination, the choice is driven by the following:

If there is a fair amount of text in the window (or "form" if you prefer) that doesn't need to be manipulated by the user then that text should be implemented as a TLabel.  If the text needs to be "manipulated", e.g, moved around in the window by the user then using a windowed control such as TStaticText will simplify manipulating the control.    The other important consideration is that since a TStaticText is in a window, the window that contains it can be thought of as a cocoon that protects the text, any painting that occurs _outside_ its window cannot affect the text in it (presuming some class/window styles are set accordingly.)

A non-windowed control does not have that protection.  if anything is output at a location that overlaps the label text then the label text will be affected.

The LCL makes it a bit difficult to select one or the other because the LCL contains code that makes a TLabel (non-windowed control) have many of the features of the TStaticText (a windowed control) but, there is one difference the LCL (or any other code) cannot "compensate" for and that is: update speed.

A windowed control is always slower to repaint than a non-windowed control.  Often, due to how Windows goes about repainting windows, the difference in speed can be quite noticeable.  Even on fast machines, windows that contain many windowed control can be "elastic" when they are resized and, the reason is because of the large number of child windows the main window contains (e.g, MS Visual Studio.)

When text is implemented as a non windowed control, repainting that text will be quite a bit faster than repainting the same text in a windowed control.  This makes updating/repainting the window quite a bit faster and makes the program feel more responsive.

The answer to your question is as follows: _in general_ use TLabel because repainting will be faster and it will consume less resources (it's not windowed), use TStaticText when that control offers some capability/characteristic TLabel does not offer that you really need.

HTH.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: Nicole on February 03, 2023, 02:50:01 pm
by the way, case you should change your mind:

Lazarus offers a way to "change class". You can right-click and change from one to the other. As they do not have the same properties, you will lose properties, which the destination class does not have. You are warned before and have to confirm that.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: Remy Lebeau on February 04, 2023, 09:15:07 pm
You can put TStaticLabel over TPanel but TLabel will always be behind TPanel.

That is incorrect, you an put a TLabel on top of a TPanel just fine.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: eljo on February 04, 2023, 09:32:24 pm
That is incorrect, you an put a TLabel on top of a TPanel just fine.
Not on top, you can put it inside a tpanel just fine. On top assumes the same parent, at least in my book.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: tetrastes on February 04, 2023, 11:31:17 pm
That is incorrect, you an put a TLabel on top of a TPanel just fine.
Not on top, you can put it inside a tpanel just fine. On top assumes the same parent, at least in my book.

You can, if Tpanel property ParentColor is True, as you can see in Reply #21.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: eljo on February 04, 2023, 11:59:21 pm
That is incorrect, you an put a TLabel on top of a TPanel just fine.
Not on top, you can put it inside a tpanel just fine. On top assumes the same parent, at least in my book.

You can, if Tpanel property ParentColor is True, as you can see in Reply #21.
That's not on top, that's panel transparency of sorts. The tlabel is bellow the tpanel.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: tetrastes on February 05, 2023, 01:40:24 pm
That is incorrect, you an put a TLabel on top of a TPanel just fine.
Not on top, you can put it inside a tpanel just fine. On top assumes the same parent, at least in my book.

You can, if Tpanel property ParentColor is True, as you can see in Reply #21.
That's not on top, that's panel transparency of sorts. The tlabel is bellow the tpanel.

What do you mean by "on top" and "below"? I guess now that you mean Z-order "Front"/"Forward" and "Back", not what you see on the screen. In this case I agree with you, Z-order does not affect the behaviour of TLabel.
But you confused me with your terminology. %) For me Label is "on top" at the first picture, and "below" at the second.
EDIT: pictures are at Reply #21.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: eljo on February 05, 2023, 01:48:27 pm
What do you mean by "on top" and "below"? I guess now that you mean Z-order "Front"/"Forward" and "Back", not what you see on the screen. In this case I agree with you, Z-order does not affect the behaviour of TLabel.
But you confused me with your terminology. %) For me Label is "on top" at the first picture, and "below" at the second.
Yes with "on top" and "bellow" I'm talking about the z order and no its not the same, which is evident by the frame line on top of the tlabel in your screenshot.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: tetrastes on February 05, 2023, 02:07:01 pm
What do you mean by "on top" and "below"? I guess now that you mean Z-order "Front"/"Forward" and "Back", not what you see on the screen. In this case I agree with you, Z-order does not affect the behaviour of TLabel.
But you confused me with your terminology. %) For me Label is "on top" at the first picture, and "below" at the second.
Yes with "on top" and "bellow" I'm talking about the z order and no its not the same, which is evident by the frame line on top of the tlabel in your screenshot.

I don't understand what do you mean again. What is "not the same"? I changed Z-order of Label1 regarding Panel1 in all ways, and the picture is the same.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: lainz on February 05, 2023, 03:31:15 pm
What do you mean by "on top" and "below"? I guess now that you mean Z-order "Front"/"Forward" and "Back", not what you see on the screen. In this case I agree with you, Z-order does not affect the behaviour of TLabel.
But you confused me with your terminology. %) For me Label is "on top" at the first picture, and "below" at the second.
Yes with "on top" and "bellow" I'm talking about the z order and no its not the same, which is evident by the frame line on top of the tlabel in your screenshot.

I don't understand what do you mean again. What is "not the same"? I changed Z-order of Label1 regarding Panel1 in all ways, and the picture is the same.

is not the same.

tlabel is drawn on the canvas of the parent control. tstatictext has his own canvas than can be placed half in the form and half in the tpanel...
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: tetrastes on February 05, 2023, 08:11:25 pm
is not the same.

tlabel is drawn on the canvas of the parent control. tstatictext has his own canvas than can be placed half in the form and half in the tpanel...

Sure they are not the same. But I did not mention TStaticText in my posts. I wrote only about TLabel.

And correct your post, please, as it looks like I wrote your words.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: domasz on February 05, 2023, 08:53:27 pm
I don't understand what do you mean again. What is "not the same"? I changed Z-order of Label1 regarding Panel1 in all ways, and the picture is the same.
The picture is the same because changing z-order doesn't work for TLabel. It's always behind TPanel.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: tetrastes on February 05, 2023, 09:27:00 pm
I don't understand what do you mean again. What is "not the same"? I changed Z-order of Label1 regarding Panel1 in all ways, and the picture is the same.
The picture is the same because changing z-order doesn't work for TLabel. It's always behind TPanel.

That's what I have written. I have not understood, what is "not the same".
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: eljo on February 05, 2023, 09:35:16 pm
I don't understand what do you mean again. What is "not the same"? I changed Z-order of Label1 regarding Panel1 in all ways, and the picture is the same.
The picture is the same because changing z-order doesn't work for TLabel. It's always behind TPanel.

That's what I have written. I have not understood, what is "not the same".
Zoom the image see the white line on top of the tlabel and bellow the tstatictext. Transparent tpanel and on top of the tpanel is not the same.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: tetrastes on February 05, 2023, 09:47:36 pm
I don't understand what do you mean again. What is "not the same"? I changed Z-order of Label1 regarding Panel1 in all ways, and the picture is the same.
The picture is the same because changing z-order doesn't work for TLabel. It's always behind TPanel.

That's what I have written. I have not understood, what is "not the same".
Zoom the image see the white line on top of the tlabel and bellow the tstatictext. Transparent tpanel and on top of the tpanel is not the same.

I have already written, that I agree with you. There's no need to convince me further. I have explained that I understood "on top" not in the sense of Z-order. If you were more clear in your terminology, there was no confusing.

PS. And "no its not the same" and "Transparent tpanel and on top of the tpanel is not the same" is not the same.  :D
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: KodeZwerg on February 05, 2023, 10:07:43 pm
i do not understand your strange discussion.
whoever told first that a tlabel is always behind a tpanel is wrong - at least on windows

but probably i misunderstood something
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: tetrastes on February 05, 2023, 10:47:43 pm
Oh, I am not alone.  :D

@eljo
See, what your ambiguous terminology leads to.
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: eljo on February 05, 2023, 11:17:18 pm
i do not understand your strange discussion.
whoever told first that a tlabel is always behind a tpanel is wrong - at least on windows

but probably i misunderstood something
your image makes my point. Observe the white line on top of tlabel and read my messages again.

Sorry but I'll not repeat my self again, it's awkward typing on a smart phone.
P
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: KodeZwerg on February 05, 2023, 11:26:47 pm
Than probably don't overlap, why should you do at all and what has it to do with topic "TLabel or TStaticText" ?
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: lainz on February 06, 2023, 01:23:51 am
And correct your post, please, as it looks like I wrote your words.

Sorry I was writing from my phone.. Didn't notice.  :)
Title: Re: TLabel or TStaticText? Which one and when to use it?
Post by: tetrastes on February 06, 2023, 08:46:12 am
what has it to do with topic "TLabel or TStaticText" ?

It is about that Z-order does not work for TLabel, but works for TStaticText.
TinyPortal © 2005-2018