How can I center a control within its parent? And how to keep it centered when the parent resizes?
I know I could write a Parent.OnResize
event handler to move the control manually, but not all container components have an OnResize
event, and I would like to do this automatically.
Is there an akCenter
anchor?
The anchor akCenter
does not exist. But you can mimic akRelative
by setting the anchors property empty which results in relative movement of the control when the parent resizes. Combining that with centered placement will result in a centered-alike "anchor".
To center horizontally at designtime:
Anchors.akLeft := False
, Anchors.akRight := False
in the object inspector,akHorzCenter
.The same goes for vertical, in which case you set akTop
and akBottom
false. To center in both directions: Anchors := []
.
Disclaimer: I have not found documentation of this anchors usage and I do not know whether this is the intended functioning, but it works very nice in D7 as well as in XE2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With