[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Iterating through controls within a control.

Posted on 2007-03-19
5
Medium Priority
?
442 Views
Last Modified: 2010-04-05
When iterating through the controls on a form for resetting its font size, how do I get to any controls within a control.(For example any labels in a groupbox). The following is how my code looks like:

Type
TFooClass = class(TControl); { needed to get at protected }
                               { font property }

procedure TSLCCOTForm.FormShow(Sender: TObject);

var
DesiredSize, i : integer;

begin
  if PixelsPerInch <> 96 then
  begin
  for i := ControlCount -1 downto 0 do    
  begin
  DesiredSize := 8;
  TFooClass(Controls[i]).Font.Name := 'Arial';
  TFooClass(Controls[i]).Font.Size := MulDiv(DesiredSize,96,PixelsPerInch);
  end;
  end;
end;
0
Comment
Question by:QurbanDurrani
[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
5 Comments
 
LVL 17

Accepted Solution

by:
mokule earned 1200 total points
ID: 18752496
procedure TForm1.Button2Click(Sender: TObject);
begin
  Iterate(Form1);
end;

procedure TForm1.Iterate(Contr: TWinControl);
var
  i: integer;
begin
  for i := 0 to Contr.ControlCount-1 do
    begin
// Your operations here
    if Contr.Controls[i] is TWinControl then
      Iterate(Contr.Controls[i] as TWinControl);
    end;
end;
0
 

Author Comment

by:QurbanDurrani
ID: 18753381
Mokule,
what am I doing wrong? It does not get to the controls inside the controls. Here is the code:

procedure TSLCCOTForm.FormShow(Sender: TObject);
begin
  if PixelsPerInch <> 96 then
  Iterate(SLCCOTForm);
end;

procedure TSLCCOTForm.Iterate(Contr: TWinControl);
var
i, j, DesiredSize : integer;
begin
  for i := 0 to Contr.ControlCount -1 do
    begin
        for j := ControlCount -1 downto 0 do
          begin
            DesiredSize := 8;
            TFooClass(Controls[j]).Font.Name := 'Arial';
            TFooClass(Controls[j]).Font.Size := MulDiv(DesiredSize,96,PixelsPerInch);
          end;
    if Contr.Controls[i] is TWinControl then
      Iterate(Contr.Controls[i] as TWinControl);
    end;    
end;

0
 
LVL 17

Assisted Solution

by:TheRealLoki
TheRealLoki earned 800 total points
ID: 18753472
small change needed

procedure TForm1.Iterate(Contr: TWinControl);
var
  i: integer;
begin
  for i := 0 to Contr.ControlCount-1 do
    begin
      memo1.Lines.Add(Contr.Controls[i].Name);
      if Contr.Controls[i] is TWinControl then
        Iterate(Contr.Controls[i] as TWinControl);
      if (Contr.Controls[i] is TLabel) then
      begin
        DesiredSize := 8;
        TLabel(Contr.Controls[i]).Font.Name := 'Arial';
        TLabel(Contr.Controls[i]).Font.Size := MulDiv(DesiredSize,96,PixelsPerInch);
      end;
    end;
end;
0
 

Author Comment

by:QurbanDurrani
ID: 18759046
TheRealLoki,
Thanks for your help. The above code after removing the line with memo would work only on labels. I would want it to work on any controls within any control. For example a pagecontrol containing  tabsheets which inturn contains groupbox, labels, buttons etc. or a form containing groupboxes which inturn contains labels and buttons.
Sorry for any confusion my initial question might have created.
0
 

Author Comment

by:QurbanDurrani
ID: 18767440
Mokule,
Applying the code exactly the way you first suggested as shown below gives 'List Undex out of bounds(6)' error.
The code is shown below:

procedure TSLCCOTForm.FormShow(Sender: TObject);
begin
  if PixelsPerInch <> 96 then
  Iterate(SLCCOTForm);
end;

procedure TSLCCOTForm.Iterate(Contr: TWinControl);
var
i, DesiredSize : integer;
begin
  for i := 0 to Contr.ControlCount -1 do
    begin
            DesiredSize := 8;
            TFooClass(Controls[i]).Font.Name := 'Arial';
            TFooClass(Controls[i]).Font.Size := MulDiv(DesiredSize,96,PixelsPerInch);
    if Contr.Controls[i] is TWinControl then
    Iterate(Contr.Controls[i] as TWinControl);
    end;    
end;


0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

650 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