Solved

Reposition components proportionally on a form

Posted on 2004-10-07
12
474 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
  • 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now