Recent

Author Topic: TLabel: Width incorrect with AutoSize set to true  (Read 3739 times)

Scouz

  • Newbie
  • Posts: 3
TLabel: Width incorrect with AutoSize set to true
« on: September 29, 2007, 07:44:37 pm »
When I try to get the .Width property from my labels which has AutoSize = true, it returns 65 for all of them even though they vary in size.

Anyone who know why it behaves like this? I use it to organize them from left to right and when .Width is that big, they get a huge space between them. I know aligning may solve it but since I need a specific space between them and I don't want to add dummy components in between, I would rather like to fix the issue :)

Scouz

  • Newbie
  • Posts: 3
RE: TLabel: Width incorrect with AutoSize set to true
« Reply #1 on: September 29, 2007, 07:55:15 pm »
Seems like it's a problem I've experienced before... If I call the "positioning" routine a few ms after the form has been shown, it'll do the trick. A bit weird behaivour but a timer isn't a big problem :)

If anyone know if it's possible to do this without a timer, please tell me.

Rik H

  • Newbie
  • Posts: 1
Re: TLabel: Width incorrect with AutoSize set to true
« Reply #2 on: December 13, 2019, 12:23:33 pm »
Probably a decade late but I had the same problem today. Apparrently this issue has not been fixed in the last 12 years.  %)
After some searching I found the solution for calculating width and height of a label manually

Label.Width := Label.Canvas.TextWidth(Label.caption);
Label.Height := Label.Canvas.TextHeight(Label.caption);


trev

  • Sr. Member
  • ****
  • Posts: 349
  • Former Delphi 7 and Delphi 10.2 User
Re: TLabel: Width incorrect with AutoSize set to true
« Reply #3 on: December 13, 2019, 01:03:19 pm »
Thanks for posting a workaround, but you may want to post a bug to the bugtracker http://bugs.freepascal.org/set_project.php?project_id=6  with details of operating system and versions of Lazarus and FPC.
o Lazarus v2.1.0 r62452, FPC v3.3.1 r42644, macOS 10.14.6 (with sup update), Xcode 10.3
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

wp

  • Hero Member
  • *****
  • Posts: 6654
Re: TLabel: Width incorrect with AutoSize set to true
« Reply #4 on: December 13, 2019, 01:04:10 pm »
I don't know what you are doing. This is working correctly for me: Add a label, set its caption, change the font size, and add a memo. Then add this code to read the label width -- all three numbers in the memo are the same and seem to be correct when I read the label width with a screen ruler:

Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Memo1.Lines.Add(Label1.Width.ToString);
  4. end;
  5.  
  6. procedure TForm1.FormActivate(Sender: TObject);
  7. begin
  8.   Memo1.Lines.Add(Label1.Width.ToString);
  9.   Memo1.Lines.Add(Label1.Canvas.TextWidth(Label1.Caption).ToString);
  10. end;
« Last Edit: December 13, 2019, 01:06:04 pm by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10