Solved

Iterating through controls within a control.

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 303
Path  to current project in Delphi. 2 79
Run video youtube webbrowse 10 61
Delphi: barcode reading on android platform 1 29
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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