Recent

Author Topic: TLabel: Width incorrect with AutoSize set to true  (Read 6330 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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 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.

wp

  • Hero Member
  • *****
  • Posts: 12069
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 »

vladbfg

  • Newbie
  • Posts: 4
Re: TLabel: Width incorrect with AutoSize set to true
« Reply #5 on: June 03, 2024, 09:02:19 am »
Hi all, sorry for necropost, but I have same problem in 2024 - property ".width" does not refresh (but when form invisible)

Started parameters:
Label.autosize = true
FormMain.visible = false

When I change Label.Caption then the ".width"  not changes
(I convert old delphi-project, there width changed)

If FormMain.visible = true then width updates.

Thx for trick with Label.width:=Label.Canvas.TextWidth(...), it works
« Last Edit: June 03, 2024, 09:16:43 am by vladbfg »

 

TinyPortal © 2005-2018