Recent

Author Topic: Loop though object names  (Read 4744 times)

bill0287

  • Full Member
  • ***
  • Posts: 146
Loop though object names
« on: May 02, 2016, 11:04:06 pm »
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][+][-]
  1. var i: integer; str3, str4, str5: String
  2.  
  3. begin
  4.   for i := 3 to 5 do begin
  5.     PictureXXX.Picture.LoadFromFile('foo.bmp');
  6.     strXXX := 'foo';
  7.  
  8.   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).
« Last Edit: May 02, 2016, 11:28:57 pm by bill0287 »

ahiggins

  • Jr. Member
  • **
  • Posts: 92
Re: Loop though object names
« Reply #1 on: May 02, 2016, 11:51:12 pm »
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

  • Hero Member
  • *****
  • Posts: 2330
Re: Loop though object names
« Reply #2 on: May 02, 2016, 11:56:12 pm »
I 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

  • Hero Member
  • *****
  • Posts: 1228
Re: Loop though object names
« Reply #3 on: May 03, 2016, 12:13:52 am »
hello,
yes findcomponent is a good solution :
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var i: integer;
  3.     MyStr: array[1..5] of String ;
  4.     ThePicture: Timage;
  5. begin
  6.   for i := 3 to 5 do begin
  7.   ThePicture := self.FindComponent('Picture'+IntToStr(i)) as Timage;
  8.   ThePicture.Picture.LoadFromFile('foo.Bmp');
  9.   MyStr[i] := 'foo';
  10.   end;
  11.  
  12. end;      

or use an array of components :
Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.   MyPictures : Array[1..5] of Timage;  
  4. //
  5. procedure TForm1.FormCreate(Sender: TObject);
  6. var i: integer;
  7. begin
  8.   for i := 1 to 5 do
  9.   begin
  10.   MyPictures[i] :=   self.FindComponent('Picture'+IntToStr(i)) as Timage;
  11.   end;
  12. end;  
  13.  

Friendly, J.P
« Last Edit: May 03, 2016, 12:20:09 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

bill0287

  • Full Member
  • ***
  • Posts: 146
Re: Loop though object names
« Reply #4 on: May 03, 2016, 12:15:40 am »
Thanks all, I did find a solution using FindComponent and an array for the variables.

 

TinyPortal © 2005-2018