Recent

Author Topic: align centre in window, doesn't !  (Read 1603 times)

lazer

  • Sr. Member
  • ****
  • Posts: 269
align centre in window, doesn't !
« on: October 13, 2022, 12:27:56 pm »
Hi,

I find it very frustrating trying to align buttons and various components using the align dialogue.

For example : Horizontal , align , centre in window    does not the centre the button, so it is not centred at all. 

I really can't imagine in what situation this is what anyone would want to do. Why isn't there a logical option to centre in the parent object like Delphi and just about everything else does?

It's actually quite hard to work out what it is doing. 

If I put a Tbutton in a Tpanal on Tform the button drags around with the panel. Fine.  Now if I  do align to centre it not aligned to either the panel or the form and can actually disappear from view since it is all or partially outside of the panel.

Can someone comment on this and explain what it thinks it is doing so I can understand how to use it?

Is there any way other than programming an event handler to get this to work ?

TIA.

wp

  • Hero Member
  • *****
  • Posts: 13352
Re: align centre in window, doesn't !
« Reply #1 on: October 13, 2022, 01:08:22 pm »
I don't know what you are talking about...

The "Align" property of many components? Hmmm, can't be, because there is no setting for "center in window".

The "Align" in the context menu? Could be because now there is a "center in window" in the "horizontal" and "vertical" boxes. I tested it - it seems to work. BUT: This is valid only for the size of the form at designtime. When you later resize the form at runtime (or even at designtime) the button will stick to its old position. And this is the same as in Delphi. So, I'd say this is rather useless...

The best way to center a control inside its container is using the anchor editor: https://wiki.freepascal.org/Anchor_Sides#Via_the_Anchor_Editor. Select the button that you want to center, open the anchor editor (menu "View"). First go to the "Left anchoring" box, select the form in the combobox, and click the center button below the combobox - this centers the button horizontally. You can resize the form, and the button will follow. Repeat with the "Top anchoring" settings, and the button is also centered vertically. (BUT: now you cannot drag the button with the mouse any more since the requirement for centering and mouse interaction are mutually excluding).
« Last Edit: October 13, 2022, 01:11:12 pm by wp »

lazer

  • Sr. Member
  • ****
  • Posts: 269
Re: align centre in window, doesn't !
« Reply #2 on: October 13, 2022, 05:10:33 pm »
Quote
The "Align" property of many components? Hmmm, can't be, because there is no setting for "center in window".
Where did I say anything about "Align" property of many components?

 I did say "using the align dialogue", which indeed can be accessed via the right-mouse click context menu.

Quote
When you later resize the form at runtime (or even at designtime) the button will stick to its old position.
Nonsense, if you resize it the parent, it will set controls to a difference place next time you do the align dlg. It is NOT setting an alignment "property" it is adjusting , left and top. So if you resize you parent object you will need to redo the align dlg.

Quote
And this is the same as in Delphi.
Wrong again. It is many years since I used Delphi and most recent was D4. But this always worked in a sensible and useful way. That is where I have my expectation of what this does.


lazer

  • Sr. Member
  • ****
  • Posts: 269
Re: align centre in window, doesn't !
« Reply #3 on: October 13, 2022, 05:23:50 pm »
The link you posted provides a solution. This test code with a button on a form locks it to the centre in both directions.


Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   button1.AnchorVerticalCenterTo(Form1);
  4.   button1.AnchorHorizontalCenterTo(Form1);
  5. end;
  6.  

Thanks for the tip.

dseligo

  • Hero Member
  • *****
  • Posts: 1653
Re: align centre in window, doesn't !
« Reply #4 on: October 13, 2022, 09:38:55 pm »
The link you posted provides a solution. This test code with a button on a form locks it to the centre in both directions.


Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   button1.AnchorVerticalCenterTo(Form1);
  4.   button1.AnchorHorizontalCenterTo(Form1);
  5. end;
  6.  

Thanks for the tip.

I am not sure, but I think you missed wp's suggestion to use anchor editor. You can set anchors in your code, but you can also do it via anchor editor as wp said.

lazer

  • Sr. Member
  • ****
  • Posts: 269
Re: align centre in window, doesn't !
« Reply #5 on: October 13, 2022, 10:39:26 pm »
Yes, thanks, I found that , it's available through the Anchor (set)  propertied in property editor too.

Like the procedure calls, it seems to set something else too, which I haven't found.

There is the alignment of the anchor and the "sibling".  Are these invisible properties which are not available programmatically ?

wp

  • Hero Member
  • *****
  • Posts: 13352
Re: align centre in window, doesn't !
« Reply #6 on: October 13, 2022, 11:03:51 pm »
The anchor editor is just a visual tool to set the Anchorsides[], Anchors and BorderSpacing properties. Please read also https://wiki.freepascal.org/Autosize_/_Layout#Anchor_Sides which I forgot to mention in the above post.

Quote
When you later resize the form at runtime (or even at designtime) the button will stick to its old position.
Nonsense, if you resize it the parent, it will set controls to a difference place next time you do the align dlg. It is NOT setting an alignment "property" it is adjusting , left and top. So if you resize you parent object you will need to redo the align dlg.
I add a button to the form somewhere, right-click on the button, select "Align..." from the context menu and "Center in windows" in the "Horizontal" and "Vertical" boxes. --> This moves the button into the center of the form. But when I resize the form the position of the button is not recalculated (like it is with anchoring), it stays at is position. Call this statement "non-sense" or whatever, I checked it now for the second time. And I also checked it with Delphi 7 and XE 10.3 - same behaviour as with Lazarus. Please test it yourself, do not rely on your 25 year-old memories of Delphi 4. But maybe you are doing something else here. What is your Lazarus/FPC version? Your OS? My observations are on Win 11 with Laz 2.3/FPC 3.2.2

 

TinyPortal © 2005-2018