Recent

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

dsiders

  • Hero Member
  • *****
  • Posts: 1238
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #15 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.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

lainz

  • Hero Member
  • *****
  • Posts: 4593
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #16 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.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #17 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.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Handoko

  • Hero Member
  • *****
  • Posts: 5290
  • My goal: build my own game engine using Lazarus
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #18 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.

OC DelGuy

  • Full Member
  • ***
  • Posts: 121
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #19 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.
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

domasz

  • Hero Member
  • *****
  • Posts: 529
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #20 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.


tetrastes

  • Hero Member
  • *****
  • Posts: 526
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #21 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. ;)
« Last Edit: February 03, 2023, 10:24:11 am by tetrastes »

440bx

  • Hero Member
  • *****
  • Posts: 4535
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #22 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.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Nicole

  • Hero Member
  • *****
  • Posts: 1009
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #23 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.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1408
    • Lebeau Software
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #24 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.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #25 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.

tetrastes

  • Hero Member
  • *****
  • Posts: 526
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #26 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.

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #27 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.

tetrastes

  • Hero Member
  • *****
  • Posts: 526
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #28 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.
« Last Edit: February 05, 2023, 01:42:08 pm by tetrastes »

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: TLabel or TStaticText? Which one and when to use it?
« Reply #29 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.

 

TinyPortal © 2005-2018