Solved

Iterating through controls within a control.

Posted on 2007-03-19
5
433 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 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi - replicating a form 8 108
can't find the executable in Simulator 1 125
Dev express lookupcombo 3 60
Firemonkey BASS_Init into a thread 17 76
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

731 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