Solved

Reposition components proportionally on a form

Posted on 2004-10-07
12
484 Views
Last Modified: 2010-04-05
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!
0
Comment
Question by:devilduck
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +4
12 Comments
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12250847
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...
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12250858
Oh, don't forget... I changed their positions only horizontally. It's just an example. ;-)
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12251006
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;

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12251033
Sorry Workshop_Alex I was posting at the same time as you, you beat me to it.
0
 
LVL 1

Expert Comment

by:Bart_Thomas
ID: 12251627
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
0
 

Author Comment

by:devilduck
ID: 12252244
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
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12252672
download an example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21159755.zip
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12254637
@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.

0
 

Expert Comment

by:joncmora
ID: 12255619
>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.
0
 

Author Comment

by:devilduck
ID: 12293955
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
0
 

Accepted Solution

by:
RomMod earned 0 total points
ID: 12349153
The question has been PAQ'd and the 250 points have been refunded.
RomMod
Community Support Moderator
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month6 days, 4 hours left to enroll

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question