Question

Delphi - Button dimension

Asked by: danz67

Hi,            
      
I have so many buttons, about 650, I want to set a size (height and Widht) equal for all through code. Currently I do this for a button
AdvSmoothButton54.Width := Panel45.Width div 5;

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-11-03 at 23:40:15ID24870014
Topic

Delphi Programming

Participating Experts
3
Points
125
Comments
29

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Scrollable div with height=100%
    I try to make a page that will have a long table, and a title above the table, and I want the table to be scrollable WITHOUT scrolling the title. In other ways, I want a fix title and a scrollable table. the way I see it I can use either frames or scrollable DIVs. I preffer s...
  2. Table-like layout with equal DIV height for each cell?
    Ok, here are the important bits. -Ignoring the fact that Im trying to use CSS/Divs for tabular data (Requirement) -And ignoring the fact that the table_header DIVs arent the same width as the table divs (unless it honestly matters in this case) I need to find a way to make t...
  3. How to align heights of several divs
    Hi X-perts, A very simple question: how to align heights of a few divs via jscript. I have 4 divs with dynamicaly populating content. Their heights are not defined in the css. I am trying to get their actual heights first and assign the max height to all of them. here is my...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Geert_GruwezPosted on 2009-11-04 at 00:18:01ID: 25737323

is panel45 the parent ?
or what relationship does it have to the button ?

you could add a unit code with the class and new constructor to modify all with only changing the uses ...

 

by: danz67Posted on 2009-11-04 at 00:29:19ID: 25737372

is not a parent, is a measurement parameter that I need to do a calculation

 

by: MerijnBPosted on 2009-11-04 at 01:02:51ID: 25737530

Are all these button on the same form?

 

by: danz67Posted on 2009-11-04 at 01:21:34ID: 25737597

Yes

 

by: Geert_GruwezPosted on 2009-11-04 at 01:39:15ID: 25737681

please take your time to explain what you want.
the better you explain, the easier to understand what you want.

up to now, i'm not following ...

 

by: danz67Posted on 2009-11-04 at 01:55:20ID: 25737757

I have a panel inside with 5 button, when the panel changes size the button I have to grow in proportion, this is repeated for 125 panel and 625 button.
I hope I have been more clear, thanks

 

by: Geert_GruwezPosted on 2009-11-04 at 02:05:39ID: 25737793

would have been easier if you created a frame for each set
and then create the frames on the fly

i could create a sample tonight... don't have delphi here now

 

by: danz67Posted on 2009-11-04 at 02:14:01ID: 25737852

Can not create a frame, should I delete all the work done until now, we must find a solution on what I have done

 

by: Geert_GruwezPosted on 2009-11-04 at 02:16:54ID: 25737869

ok, no frames, bit more complex, but should work.
show you tonight ...

 

by: epasquierPosted on 2009-11-04 at 02:20:10ID: 25737883

I believe you need to set the width and height of your buttons, but also Left/Top position.

If you need to arrange a lot of buttons, you have to tell us how they are positioned and how you want to arrange them. Supposing you want them in row/columns you could have a procedure like this :

LayoutControls([edit1,edit2,btn1,btn2], 2, 8,8, 128, 32, 120, 24);

will but say 2 edit in columns with 2 buttons underneath

procedure LayoutControls(a:Array of TControl; NC, T,L,DX,DY:Integer;W:Integer=0;H:Integer=0);
// NC : number of columns. T/L : top/left position of first control 
// DX/DY : distance between 2 colomns or row
// W/H: Witdh/Height of every button, by default 0 (if you do not specify this parameter) means no change 
Var
 i,c,r:integer;
begin
 c:=0;
 r:=0;
 for i:=Low(a) to High(a) do With a[i] do
  begin
   Left:=L+c*DX;
   Top:=T+r*DY;
   if W>0 Then Width:=W;
   if H>0 Then Height:=H;
   inc(c);
   if c=NC Then
    begin
     c:=0;
     inc(r);
    end;
  end;
end; 

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:

Select allOpen in new window

 

by: epasquierPosted on 2009-11-04 at 02:25:02ID: 25737908

update for your specific need : change the above procedure with :

procedure LayoutControls(aParent:TComponent; NC, T,L,DX,DY:Integer;W:Integer=0;H:Integer=0);
...
 for i:=0 to aParent.ComponentCount-1 do if aParent.Components[i] is TControl Then With TControl(aParent.Components[i]) do

                                              
1:
2:
3:

Select allOpen in new window

 

by: epasquierPosted on 2009-11-04 at 02:28:50ID: 25737931

then for each panel you call :
var
 DX:integer;
begin
 DX:=(myPanel.Width-8) Div 5;
 LayoutControls(myPanel, 5, 8, 8,DX, 0, DX-8);
end;

 

by: epasquierPosted on 2009-11-04 at 02:31:18ID: 25737945

if all your panels have the same parent, then you can loop all of them using the parent ComponentCount and Components properties with a test with TPanel, like I did with TControl

procedure LayoutAllPanels(aParent:TComponent)
var
 DX:integer;
begin
 for i:=0 to aParent.ComponentCount-1 do if aParent.Components[i] is TPanel Then 
  begin
   DX:=(TPanel(aParent.Components[i]).Width-8) Div 5;
   LayoutControls(aParent.Components[i], 5, 8, 8,DX, 0, DX-8);
  end;
end;

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

Select allOpen in new window

 

by: epasquierPosted on 2009-11-04 at 02:31:56ID: 25737947

I forgot the i:integer; declaration above

 

by: danz67Posted on 2009-11-04 at 02:40:17ID: 25737990

I made an example at the beginning:
AdvSmoothButton54.Width: Panel45.Width div = 5;
Suppose AdvSmoothButton54.Width = 150

buttons are called by AdvSmoothButton1 to AdvSmoothButton625.
What I need is to know just how to set the width of each button the same for all the buttons, this is difficult??

 

by: Geert_GruwezPosted on 2009-11-04 at 02:41:33ID: 25737997

>>epasquier
there is a solution with 1 unit and add 1 uses to alter all buttons on 1 form ...

 

by: epasquierPosted on 2009-11-04 at 02:52:32ID: 25738040

>> What I need is to know just how to set the width of each button the same for all the buttons
No, if you only change the size, they will stay in their former position and overlap or have more space than necessary between them.

>> this is difficult??
no it's not, you just have to copy/paste the code provided and alter to suit your needs like space between buttons

if all your buttons must take the full width of the panel and no space between them, then alter the LayoutAllPanels like below, and call

LayoutAllPanels( Parent_Of_All_Your_Panels );

 
procedure LayoutAllPanels(aParent:TComponent)
var
 DX:integer;
begin
 for i:=0 to aParent.ComponentCount-1 do if aParent.Components[i] is TPanel Then 
  begin
   DX:=TPanel(aParent.Components[i]).Width Div 5;
   LayoutControls(aParent.Components[i], 5, 8, 8,DX, 0, DX);
  end;
end;
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen in new window

 

by: epasquierPosted on 2009-11-04 at 02:53:35ID: 25738044

>> Geert_Gruwez: there is a solution with 1 unit and add 1 uses to alter all buttons on 1 form ...
don't keep us waiting, please tell us your solution ;)

 

by: danz67Posted on 2009-11-04 at 02:59:22ID: 25738072

     
I forgot to say that each button contained within the panel is left-aligned, so I need to assign the width of the buttons (which are 5 for each panel) with exactly the value of panle.widht div 5, all repeated each button

 

by: danz67Posted on 2009-11-04 at 04:03:01ID: 25738443

Probably my English is not very clear, further evidence to explain better, in my program I had written this

 AdvSmoothButton54.Width := Panel45.Width div 5;
 AdvSmoothButton246.Width := Panel45.Width div 5;
 AdvSmoothButton247.Width := Panel45.Width div 5;
 AdvSmoothButton248.Width := Panel45.Width div 5;
 AdvSmoothButton250.Width := Panel45.Width div 5;

 AdvSmoothButton236.Width := Panel45.Width div 5;
 AdvSmoothButton237.Width := Panel45.Width div 5;
 AdvSmoothButton238.Width := Panel45.Width div 5;
 AdvSmoothButton239.Width := Panel45.Width div 5;
 AdvSmoothButton240.Width := Panel45.Width div 5;

 AdvSmoothButton231.Width := Panel45.Width div 5;
 AdvSmoothButton232.Width := Panel45.Width div 5;
 AdvSmoothButton233.Width := Panel45.Width div 5;
 AdvSmoothButton234.Width := Panel45.Width div 5;
 AdvSmoothButton235.Width := Panel45.Width div 5;

 AdvSmoothButton241.Width := Panel45.Width div 5;
 AdvSmoothButton242.Width := Panel45.Width div 5;
 AdvSmoothButton243.Width := Panel45.Width div 5;
 AdvSmoothButton244.Width := Panel45.Width div 5;
 AdvSmoothButton245.Width := Panel45.Width div 5;

 AdvSmoothButton226.Width := Panel45.Width div 5;
 AdvSmoothButton227.Width := Panel45.Width div 5;
 AdvSmoothButton228.Width := Panel45.Width div 5;
 AdvSmoothButton229.Width := Panel45.Width div 5;
 AdvSmoothButton230.Width := Panel45.Width div 5;

How can I simplify the code?

 

by: epasquierPosted on 2009-11-04 at 04:13:46ID: 25738512

ok, I didn't knew that AdvSmoothButton has an align property. It's not the case for standard buttons.
Then do the same as explained above but simpler. I just wanted to provide a sample that could work for everything, with options.

It could even be simpler by removing some tests but it's safer this way

procedure LayoutAllPanels(aParent:TComponent)
var
 DX:integer;
 i,j:integer;
begin
 for i:=0 to aParent.ComponentCount-1 do 
  if aParent.Components[i] is TPanel Then 
    With TPanel(aParent.Components[i]) do
  begin
   DX:=Width Div 5;
   for j:=0 to ComponentCount-1 do if Components[j] is TControl Then 
    With TControl(Components[j]) do Width:=DX;
  end;
end;
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:

Select allOpen in new window

 

by: danz67Posted on 2009-11-04 at 04:24:44ID: 25738583

     
This procedure as the appeal? okay one button to try?

procedure TForm1.Button1Click (Sender: TObject);
begin

end;

 

by: Geert_GruwezPosted on 2009-11-04 at 06:04:41ID: 25739367

you made it even simpler ...

above your form paste this

type
  TAdvSmoothButton = class(unitname.TAdvSmoothButton)
  public
    constructor Create(AOwner: TComponent); override;
  end;

  TYourForm = class(TForm)
  ...
  public
    function ButtonWidth: integer;
  end;

constructor TAdvSmoothButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  DoInitWidth;
end;
 
procedure TAdvSmoothButton.DoInitWidth;
begin
  Width := TYourForm(Owner).ButtonWidth;
end;

function TYourForm.ButtonWidth: integer;
begin
  Result := 50;
  if Assigned(Panel45) then
    Result := Panel45.Width div 5;
end;

 

by: Geert_GruwezPosted on 2009-11-04 at 06:05:40ID: 25739374

a few typos ... sorry ...

type
  TAdvSmoothButton = class(unitname.TAdvSmoothButton)
  public
    constructor Create(AOwner: TComponent); override;
    procedure DoInitWidth;
  end;
 
  TYourForm = class(TForm)
  ...
  public
    function ButtonWidth: integer;
  end;
 
constructor TAdvSmoothButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  DoInitWidth;
end;
 
procedure TAdvSmoothButton.DoInitWidth;
begin
  Width := TYourForm(Owner).ButtonWidth;
end;
 
function TYourForm.ButtonWidth: integer;
begin
  Result := 50;
  if Assigned(Panel45) then
    Result := Panel45.Width div 5;
end;
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:

Select allOpen in new window

 

by: epasquierPosted on 2009-11-04 at 06:14:42ID: 25739444

Geert, you missed a point : The buttons need resize when the panel are resized. And if you want to go that way then you'd better simply state that the width of a button := TPanel(Owner).Width Div 5;

So danz67, assuming that all your panels are in the same panel (or other) parent, here is what you have to do :
add an onResize event for this parent container :

procedure TForm1.PanelResize(Sender: TObject);
begin
 LayoutAllPanels(TComponent(Sender));
end;

 

by: Geert_GruwezPosted on 2009-11-04 at 06:21:14ID: 25739491

i know, haven't got delphi here, only a browser ...
i was still gonna come back on that

 

by: danz67Posted on 2009-11-04 at 06:35:33ID: 25739648

greatly appreciate the work you've done and I must say that if I try this code buttons spread out perfectly

procedure TForm1.Button1Click(Sender: TObject);
var
 DX:integer;
 i,j:integer;
 aParent:TComponent;
begin
 DX:=Width Div 5;
 for j:=0 to ComponentCount-1 do if Components[j] is TControl Then
   With TControl(Components[j]) do Width:=DX;
end;

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

Select allOpen in new window

 

by: danz67Posted on 2009-11-04 at 06:37:34ID: 25739670

but if I use the procedure and the first reference from the panel resize does not get the result on each panel

procedure LayoutAllPanels(aParent:TComponent);
var
 DX:integer;
 i,j:integer;
begin
 for i:=0 to aParent.ComponentCount-1 do
  if aParent.Components[i] is TPanel Then
    With TPanel(aParent.Components[i]) do
  begin
   DX:=Width Div 5;
   for j:=0 to ComponentCount-1 do if Components[j] is TControl Then
    With TControl(Components[j]) do Width:=DX;
  end;
end;
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:

Select allOpen in new window

 

by: epasquierPosted on 2009-11-04 at 06:41:48ID: 25739717

Yep, that's the simplest form of it, requiring that you only have buttons in your form that needs this kind of resize (all will be affected) and that they have all the same width. All you have to do is then to put this code in the formResize event, and call this formResize event in the onFormCreate (or after your buttons are created if dynamically) because otherwise they won't be correctly sized until you resize your forms once.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...