Solved

Iterating through controls within a control.

Posted on 2007-03-19
5
428 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
  • 3
5 Comments
 
LVL 17

Accepted Solution

by:
mokule earned 300 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 200 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 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

20 Experts available now in Live!

Get 1:1 Help Now