[Lazarus] Foreach?

Sven Barth pascaldragon at googlemail.com
Sat Jun 8 09:49:33 CEST 2013


On 08.06.2013 09:32, Timothy Groves wrote:
> So I've got this code:
>
>      frmMain.btnMacro1.Caption := MacroList [0].Name;
>      frmMain.btnMacro2.Caption := MacroList [1].Name;
>      frmMain.btnMacro3.Caption := MacroList [2].Name;
>      frmMain.btnMacro4.Caption := MacroList [3].Name;
>      frmMain.btnMacro5.Caption := MacroList [4].Name;
>      frmMain.btnMacro6.Caption := MacroList [5].Name;
>      frmMain.btnMacro7.Caption := MacroList [6].Name;
>      frmMain.btnMacro8.Caption := MacroList [7].Name;
>      frmMain.btnMacro9.Caption := MacroList [8].Name;
>      frmMain.btnMacro10.Caption := MacroList [9].Name;
>
> There's gotta be some kind of construction that would let me foreach
> this.  Any suggestions?

Untested code, but somehow like this it should work (maybe 
"ControlCount" and "Controls" are named differently...):

=== code begin ===

c := 0;
for i := 0 to frmMain.ControlCount - 1 do
   if (frmMain.Controls[i] is TButton) and (Pos('btnMacro', 
frmMain.Controls[i].Name) = 1) then begin
     frmMain.Controls[i].Caption := MacroList[c].Name;
     Inc(c);
   end;

=== code end ===

Regards,
Sven




More information about the Lazarus mailing list