Search code examples
delphic++builder

Keep OK/Cancel button centered on resize automatically?


What method is built-in to C++ Builder to keep two buttons centered on a Form when it's resized? I was close using a TRelativePanel but the best I could do is center one of the buttons and the other going to the left or right of that one. If I had 3 buttons that method would work, but I'm sure something so common they must have it built-in. How do I set that up in the C++ Builder world? If I have to use an OnResize() event, I know how to do that, but seems to me they would have something built-in.

Although I am surprised they don't have a min/max size setting for a forms property so maybe I'm expecting too much.

TIA!!


Solution

  • If you remove both akLeft and akRight from the Anchors setting of the button, they will remain at their relative position (ie. if they are 10% from the left border, they will remain 10% from the left border when you resize the form).

    But that also means that if you place two buttons in the center of a form (with the left one just left of the center, and the right one just right of the center), then they will move apart as you extend the form and overlap as you shrink it.

    The only way to keep both buttons at the center of the form, regardless of how you resize it, is to place some code in the FormResize event that adjusts their position by calculation.

    Edit: From Tom Brunberg's comment, you can also have a TPanel with no Left/Right anchors, but with a Bottom anchor, the panel being just the size of the two buttons combined and any spacing between them, placed at the bottom of the form, with the two buttons inside this panel. If you remove any Bevel from this panel:

        P.BevelKind:=TBevelKind.bkNone;
        P.BevelInner:=TBevelCut.bvNone; 
        P.BevelEdges:=[];
        P.Anchors:=[akBottom];
    

    then it will be invisible to the user, and it'll float along the bottom edge, but keeping it centered.

    If you by "min/max size setting for a forms property" mean a way to limit the resize of a form to be within certain limits, take a look at the Constraints property of the form.