Solved

Iterating through controls within a control.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

929 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

15 Experts available now in Live!

Get 1:1 Help Now