Loop though object names lazarus / 61-Beginners ---------------------------------------------------------------------- bill0287 · 2016-05-02 21:04 How do I setup a for loop so I can substitute the integer value in the object name or variable name? Something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var i: integer; str3, str4, str5: String begin  for i := 3 to 5 do begin    PictureXXX.Picture.LoadFromFile('foo.bmp');    strXXX := 'foo';   end; I have 3 images on the form, named Picture3, Picture4, and Picture5 and I want to substitute the value of i for XXX in the indentifier. (It's actually a boat load more than 3, but you get the idea). ---------------------------------------------------------------------- ahiggins · 2016-05-02 21:51 I might be well of the mark but you could try something like this (not tested) var i: integer; For i :=0 to Form1.ComponentCount -1 do      begin           If Components is Timage then           If TImage(Components).Name = 'Picture'+inttostr(I) Then      begin           TImage(Components).LoadFromFile('foo.bmp');           TImage(Components).Name:='foo';      end; ---------------------------------------------------------------------- molly · 2016-05-02 21:56 Quote from: ahiggins on May 02, 2016, 11:51:12 pmI might be well of the mark but you could try something like this (not tested) Nah, not really. but it might be more comfortable using method findcomponent() Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)     Button1: TButton;     Button2: TButton;     Button3: TButton;     Button4: TButton;     Button5: TButton;     Button6: TButton;     Memo1: TMemo;     procedure ButtonClick(Sender: TObject);   private     { private declarations }   public     { public declarations }   end; var   Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.ButtonClick(Sender: TObject); var   i : integer;   C : TComponent; begin   For i := 0 to Pred(Form1.ComponentCount) do   begin     Memo1.Append(Form1.Components[i].Name);   end;   For i := 3 to 5 do   begin     C := Form1.FindComponent('Button' + IntToStr(i));     if C is TButton then with C as TButton do     begin       Caption := 'Number' + IntToStr(i);     end;   end; end; end. ---------------------------------------------------------------------- Jurassic Pork · 2016-05-02 22:13 hello, yes findcomponent is a good solution : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var i: integer;    MyStr: array[1..5] of String ;    ThePicture: Timage;begin  for i := 3 to 5 do begin  ThePicture := self.FindComponent('Picture'+IntToStr(i)) as Timage;  ThePicture.Picture.LoadFromFile('foo.Bmp');  MyStr[i] := 'foo';  end; end;       or use an array of components : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  Form1: TForm1;  MyPictures : Array[1..5] of Timage;   //procedure TForm1.FormCreate(Sender: TObject);var i: integer;begin  for i := 1 to 5 do  begin  MyPictures[i] :=   self.FindComponent('Picture'+IntToStr(i)) as Timage;  end;end; Friendly, J.P ---------------------------------------------------------------------- bill0287 · 2016-05-02 22:15 Thanks all, I did find a solution using FindComponent and an array for the variables.