Link to home
Start Free TrialLog in
Avatar of devilduck
devilduck

asked on

Reposition components proportionally on a form

Noob question:

I am sure this is a simple question, but I haven't found an answer that works, yet.

As an example of what I need, let's say that I have a form with two rows of five buttons on it.  The buttons in each row are placed next to each other horizontally, spaced equally.  The two rows are spaced equally apart vertically.  When the user expands or contracts the form, I want the buttons to reposition themselves, keeping their proportional, equal spacing on the form.

Preferably, I would be able to do this with anchor, alignment magic as I my real project has many controls on several group boxes in a client aligned page control/tab sheet.

If I can't do this easily (anchor, alignment magic) then, I will up the points to 500 for the person who provides a function that will:
1. Go through the controls on the tab sheet and re-size the group boxes proportionally on the form.
2. Reposition the controls on each group box proportionally to the size of the group box.


Thanks!
Avatar of Wim ten Brink
Wim ten Brink
Flag of Netherlands image

Use the Form.OnResize event to calculate the new positions of your buttons. It's a bit much code to write but it's the easiest trick.

procedure TForm1.FormResize( Sender: TObject );
const
  Margin = 8;
  Space = 16;
var
  BtnWidth: Integer;
begin
  // These buttons at the same height as Button1
  Button2.Top := Button1.Top;
  Button3.Top := Button1.Top;
  Button4.Top := Button1.Top;
  Button5.Top := Button1.Top;
  // These buttons at the same height as Button6
  Button7.Top := Button6.Top;
  Button8.Top := Button6.Top;
  Button9.Top := Button6.Top;
  Button10.Top := Button6.Top;
  // Change the buttons widths first.
  BtnWidth := ( Button1.Parent.ClientWidth - 2 * Margin - 4 * Space ) div 5;
  // Set all buttons to the same width.
  Button1.Width := BtnWidth;
  Button2.Width := BtnWidth;
  Button3.Width := BtnWidth;
  Button4.Width := BtnWidth;
  Button5.Width := BtnWidth;
  Button6.Width := BtnWidth;
  Button7.Width := BtnWidth;
  Button8.Width := BtnWidth;
  Button9.Width := BtnWidth;
  Button10.Width := BtnWidth;
  // Now position all buttons.
  Button1.Left := Margin;
  Button2.Left := Button1.Left + Button1.Width + Space;
  Button3.Left := Button2.Left + Button2.Width + Space;
  Button4.Left := Button3.Left + Button3.Width + Space;
  Button5.Left := Button4.Left + Button4.Width + Space;
  Button6.Left := Button1.Left;
  Button7.Left := Button6.Left + Button6.Width + Space;
  Button8.Left := Button7.Left + Button7.Width + Space;
  Button9.Left := Button8.Left + Button8.Width + Space;
  Button10.Left := Button9.Left + Button9.Width + Space;
end;

I know, it's a lot of code. It would be easier if you had the buttons created dynamically and stored in arrays but hey, it works!

There's no simple alignment method for this, unfortunately...
Oh, don't forget... I changed their positions only horizontally. It's just an example. ;-)
The way I would do this, using the simplest tools, is to put all the calculations in the Form Onresize event.

procedure TForm1.FormResize(Sender: TObject);
var
  buttonwidth,buttonheight:integer;
begin
buttonwidth:=form1.width div 5;
buttonheight:=form1.height div 2;

button1.left:=0;
button1.width:=buttonwidth;
button1.top:=0;
button1.height:=buttonheight;

button2.left:=buttonwidth*1;
button2.width:=buttonwidth;

etc.
end;

Sorry Workshop_Alex I was posting at the same time as you, you beat me to it.
Avatar of Bart_Thomas
Bart_Thomas

Maybe the OnResize-event can help:

FOldSize is a Integer.

procedure TForm1.FormResize(Sender: TObject);
begin
  ScaleBy(Width,FOldSize);
  FOldSize := Width;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FOldSize := Width;
end;

Regards,
Bart Thomas
Avatar of devilduck

ASKER

Thanks to all who replied with a function.  I found the answer for this problem using anchors in Mastering Delphi 6.  

If you put the two rows of buttons on a form and set all of their anchors to false, it does exaclty what I wanted.  The buttons will reposition proportionally to the form as the form expands and contracts.

Further, if you add a group box and add the two rows of buttons to the group box, setting the top, left and right anchors of the group box to true will get the group box to resize with the form.  Setting the anchors of the buttons to false will make the buttons reposition properly to the group box which is resizing with the form.

Anchor magic!

Thanks again,

DD
download an example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21159755.zip
@moorhouselondon, lol. Doesn't matter. I was afraid something like that could happen and was amazed I was the first... :-)

@devilduck:
> If you put the two rows of buttons on a form and set all of their anchors to false, it does exaclty what I wanted.
I've done that too once. But Delphi's anchors don't always seem to work correctly. I've had some bad experience in the past with them, which is why I just recalculate their positions on the Resize events. Basically, I hare anchors since they never do exactly what I want.

>If you put the two rows of buttons on a form and set all of their
>anchors to false, it does exaclty what I wanted.  The buttons
>will reposition proportionally to the form as the form expands
>and contracts.

Exactly what you should do! That's what I do in my projects. I have this idea of not coding when I can do something just by setting properties at design time.
So...  How do I close this question out?  While I answered my own question, I also appreciate how much work went into all of your answers.

Do I ask for a refund?  (Not my prefernce.  I don't need the points and you all worked pretty hard).

Do I accept the first answer?

Do I accept the largest answer?

Do I accept the answer from esoftbg because he made a zip file?

Do I spread the points around?


I am truly sorry, but I don't have time to test all the answers and find the best.  Mine works great for me.

Any help would be appreciated.

Thanks!

DD
ASKER CERTIFIED SOLUTION
Avatar of RomMod
RomMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial