Recent

Author Topic: Labels won't align as specified  (Read 3278 times)

TyneBridges

  • Full Member
  • ***
  • Posts: 150
    • Personal blog
Labels won't align as specified
« on: July 07, 2020, 12:55:28 pm »
Something that should be simple - anchoring controls relative to a parent - has me in knots in Lazarus because it just isn't working for me. The screen shot shows the problem, with several label controls that don't appear where I want them.

My parent window is called SS. DetailsBox is the control at the top left of the window in the screen shot. You can see that the anchors are set to akLeft and akBottom and yet it displays at the top left. This should be relative to the parent window - no siblings are defined.

I've removed all occurrences of DetailsBox.Left:= and DetailsBox.Top:= from my Pascal code and have recompiled: no difference. I tried adding DetailsBox.Parent:= SS to my code to see if this made a difference but it didn't.

TitleBox is the control showing the text "Computer Service" and this should currently be at the top left (Alignment taLeftJustify, Anchors akTop, akLeft) but is also misplaced. The client window has also expanded beyond its specified size and I'm baffled as to what's happening.

I don't understand why each label control has a property called Align in addition to Alignment (which seems to be the text alignment within the control). I can't find a clear explanation of the properties in the documentation, so can someone tell me what "Align" does? (Changing it from alNone to alClient also seems to make no difference.)

I thought that uploading all of my code would be overkill here and wondered if there's something simple that I've missed that would stop my controls aligning as specified?

Thanks.
« Last Edit: July 07, 2020, 01:15:35 pm by TyneBridges »
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

TyneBridges

  • Full Member
  • ***
  • Posts: 150
    • Personal blog
Re: Labels won't align as specified
« Reply #1 on: July 07, 2020, 01:12:46 pm »
Even my attempt at debugging by adding a ShowMessage seems to show nonsense. Can anybody tell me what I've missed or misunderstood here?
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Labels won't align as specified
« Reply #2 on: July 07, 2020, 01:27:25 pm »
Set align to none if u r using anchors because align overrides the anchors.
Also u may want to uncheck autosize.
The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Labels won't align as specified
« Reply #3 on: July 07, 2020, 01:28:28 pm »
Yes, what jamie says. Set Align to alNone and use Anchors and AnchorEditor.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Labels won't align as specified
« Reply #4 on: July 07, 2020, 03:13:00 pm »
DetailsBox is the control at the top left of the window in the screen shot. You can see that the anchors are set to akLeft and akBottom and yet it displays at the top left. This should be relative to the parent window - no siblings are defined.
This description is not clear: "DetailsBox" is this the "SPTINT 11..." or the "Computer Service" label? Both are at the top left... But anyway: Why do you anchor also to the bottom?

Since I don't fully understand your issue I am attaching "my" version of the layout. Study the anchors in the anchor editor:
- "SPRINT 11..." is anchored with akTop and akLeft to the Form1 (using Form as sibling is not absolutely required, but it is helpful if you'd once decide to activate Form.AutoSize (which is very useful when making cross-platform applications!). Since I don't like controls sitting too close to the window edge I used 6 pixels top and left border spacing.
- The date label is anchored to the top and right sides of the form, again with 6 pixels borderspacing.
- The "Computer Service" label is anchored to the left of the form and to the bottom of the "SPRINT" label, with 6 pixels top border spacing.

FTurtle

  • Sr. Member
  • ****
  • Posts: 292
Re: Labels won't align as specified
« Reply #5 on: July 07, 2020, 04:02:41 pm »
(using Form as sibling is not absolutely required, but it is helpful if you'd once decide to activate Form.AutoSize (which is very useful when making cross-platform applications!).

It is absolutely different things. If sibling is not mentioned then used Delphi mode of anchoring (you can move control in designer by mouse). If sibling is mentioned then used Lazarus mode (control cannot be moved by mouse). See hint for "Sibling" field in Anchor Editor.

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Labels won't align as specified
« Reply #6 on: July 07, 2020, 04:16:02 pm »
Yes, correct. I wanted to say "using Form as sibling is not absolutely required to anchor the label"

TyneBridges

  • Full Member
  • ***
  • Posts: 150
    • Personal blog
Re: Labels won't align as specified
« Reply #7 on: July 07, 2020, 07:56:12 pm »
Thanks to all for their help. Setting Align to none has helped but my labels are still not correctly aligned. The attached image shows the window now. The title Computer Service should be at the top left (actually I'll align it centrally but only after I fix the other problems.) The status should be at the bottom left and the date and time at the bottom right. All three are set in the Anchor Editor to have a border space of 10 on each of the two anchored sides but, as you see, none of the three elements is correctly positioned.

Is this because my window, label text and label size are all adjusted at form creation to match the screen dimensions? I don't understand why this would be incompatible with aligning the controls correctly. As an experiment I tried switching off autosize for the three labels. The text was then obscured because it didn't fit between their boundaries but they still appeared misaligned and I don't know why. My code positions the parent window (SS) but does not resize or position its child controls.

Code: Pascal  [Select][+][-]
  1. procedure TSS.FormCreate(Sender: TObject);
  2. var OurLeft, OurWidth: Integer;
  3. begin
  4.      Started:= False;
  5.      LoadConfig;
  6.      // Banner width should be 3/4 of screen width
  7.      OurWidth:= (Screen.Width div 4) * 3;
  8.      OurLeft:= (Screen.Width - OurWidth) div 2;
  9.      SS.Width:= OurWidth;
  10.      SS.Left:= OurLeft;
  11.      SS.Top:= 0;
  12.      TitleBox.Parent:= SS;
  13.      TitleBox.Color:= clNone;
  14.      TitleBox.Font.Size:= OurWidth div 40;
  15.  
  16.      // DetailsBox should anchor to bottom of window !
  17.      DetailsBox.Parent:= SS;
  18.      DetailsBox.Font.Size:= OurWidth div 80;
  19.      DetailsBox.Font.Color:= RGB(230, 230, 245);
  20.      DetailsBox.Color:= clNone;
  21.      DateTimeBox.Color:= clNone;
  22.  
  23.      DateTimeBox.Parent:= SS;
  24.      DateTimeBox.Font.Size:= OurWidth div 80;
  25.      DateTimeBox.Font.Color:= RGB(230, 230, 245);
  26.  
  27.      //ShowMessage(IntToStr(TitleBox.Font.Size));
  28.      Ct:= 0; TC:= 0;
  29.      AssignFile(L, LFileName);
  30.      Rewrite(L);
  31.      Writeln(L, 'Started Bookit screen saver: ' +  FormatDateTime('dddd d mmmm yyyy tt', Now));
  32.      // Check status in MySQL
  33.      ConnectRemote;
  34.      // Register PC if not found
  35.  
  36.      Started:= True;
  37.      Tmr.Enabled:= True;
  38.      GoingDown:= True;
  39. end;  
« Last Edit: July 07, 2020, 08:09:54 pm by TyneBridges »
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

TyneBridges

  • Full Member
  • ***
  • Posts: 150
    • Personal blog
Re: Labels won't align as specified
« Reply #8 on: July 07, 2020, 08:03:53 pm »
Here is my project. I've omitted libmysql.dll because it was so large but I guess it will still run without it, despite giving a database error (that isn't relevant to the window layout).

It's not great coding anyway but if anyone can tell me what I've done wrong I would be very grateful.
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Labels won't align as specified
« Reply #9 on: July 07, 2020, 08:51:21 pm »
For all labels: set AutoSize to True and set Sibling of active anchors to form SS (in Anchor Editor).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

TyneBridges

  • Full Member
  • ***
  • Posts: 150
    • Personal blog
Re: Labels won't align as specified
« Reply #10 on: July 07, 2020, 09:30:55 pm »
For all labels: set AutoSize to True and set Sibling of active anchors to form SS (in Anchor Editor).

Thanks for the suggestion but that didn't work either. It aligned the top label (title) correctly but the other two labels disappeared. I don't understand the logic there - how a parent control can also be a sibling.
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Labels won't align as specified
« Reply #11 on: July 07, 2020, 10:16:48 pm »
I modified your example such that it looks correct to me now. You must use the anchor editor. I made all modifications in source code, however, so that you can see what's changed. Also, use AutoSize for the labels, otherwise you risk truncation on systems different from yours.

I agree that the word "sibling" is not correct when anchoring to the sides of the parent.
« Last Edit: July 07, 2020, 10:18:40 pm by wp »

TyneBridges

  • Full Member
  • ***
  • Posts: 150
    • Personal blog
Re: Labels won't align as specified
« Reply #12 on: July 10, 2020, 08:49:21 pm »
Thanks for helping! I haven't had a chance to do anything with the application this week but will get back to it soon. I'm grateful for your time and attention.
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

TyneBridges

  • Full Member
  • ***
  • Posts: 150
    • Personal blog
Re: Labels won't align as specified
« Reply #13 on: July 13, 2020, 04:25:58 pm »
Many thanks for fixing it, wp. Your version works fine. I know I'm being dim but I still can't see what I did wrong.

I've tried comparing the text version of my main form with yours. The only difference I can see apart from a couple of the dimensions is the extra property AnchorSideBottom for the label controls in your version (e.g. AnchorSideBottom.Side = asrBottom) that doesn't seem to be in mine. I can't see where this is edited - is it set only by the anchor editor following other parameters? It looks as if setting anchors in Lazarus is much harder than it was in Delphi and I'm trying to avoid getting into the same knots again in future.

My reason for using a form's name inside its own code was to try to aid clarity. I accept that TitleBox.Parent := self; is correct, but what does it mean? Is it equivalent to TitleBox.Parent:= TitleBox rather than TitleBox.Parent:= SS (which is what I had written)? If the former is the case then, once again, I don't follow the logic. Possibly the statement isn't needed at all: I added it after things started to go wrong in case things weren't clear to the compiler.

Thanks.
« Last Edit: July 13, 2020, 04:45:34 pm by TyneBridges »
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Labels won't align as specified
« Reply #14 on: July 13, 2020, 06:04:29 pm »
My reason for using a form's name inside its own code was to try to aid clarity. I accept that TitleBox.Parent := self; is correct, but what does it mean? Is it equivalent to TitleBox.Parent:= TitleBox rather than TitleBox.Parent:= SS (which is what I had written)? If the former is the case then, once again, I don't follow the logic. Possibly the statement isn't needed at all: I added it after things started to go wrong in case things weren't clear to the compiler.

When you use Self you refer to the actual object instance, whatever its name (as a variable) instead of to a specific instance SS which might or might not be the current one.

In this specific example TitleBox.Parent := Self; is equivalent to your TitleBox.Parent:= SS but, what if you needed somewhere else a duplicate form declared as, say, TempSS: TSS? When the OnCreate handler were called it would add a TitleBox to SS again, instead of to the new TempSS; using Self as Parent prevents this and makes TitleBox a "child" of the current object being created, in this case the correct TempSS instance.

Since most forms are meant as single-instance objects it's easy to forget that when builiding a form in Lazarus what one is really doing is programming a class, not a specific object instance. One should always try to remember this (at less until it becomes automatic ;))

HTH!
« Last Edit: July 13, 2020, 06:13:38 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018