Forum > Other

TLabel: Width incorrect with AutoSize set to true

(1/2) > >>

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 :)

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:
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);

Thanks for posting a workaround, but you may want to post a bug to the bugtracker  with details of operating system and versions of Lazarus and FPC.

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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormCreate(Sender: TObject);begin  Memo1.Lines.Add(Label1.Width.ToString);end; procedure TForm1.FormActivate(Sender: TObject);begin  Memo1.Lines.Add(Label1.Width.ToString);  Memo1.Lines.Add(Label1.Canvas.TextWidth(Label1.Caption).ToString);end;


[0] Message Index

[#] Next page

Go to full version