Forum > Beginners
Loop though object names
(1/1)
bill0287:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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:
--- Quote from: ahiggins on May 02, 2016, 11:51:12 pm ---I might be well of the mark but you could try something like this (not tested)
--- End quote ---
Nah, not really. but it might be more comfortable using method findcomponent()
--- Code: ---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.
--- End code ---
Jurassic Pork:
hello,
yes findcomponent is a good solution :
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
Thanks all, I did find a solution using FindComponent and an array for the variables.
Navigation
[0] Message Index