Bryan
Bryan

Reputation: 61

Delphi Procedure/Function Creation help

I'm trying to make a function to clean up a bit of my code...

instead of having to use a crap ton of FOR loops to do this....

Form1.Image1.Picture.LoadFromFile();
Form1.Image2.Picture.LoadFromFile();
Form1.Image3.Picture.LoadFromFile();
Form1.Image4.Picture.LoadFromFile();

is theres a way to do something like this

procedure UpdateCardImage(Value,Value2,ImageX : Integer);
var
i,i2 : integer;
begin

for i := 2 to Value do
begin
for i2 := 98 to Value2 do
begin
if Value2 = 99 then
begin
Form1.Image[ImageX].Picture.LoadFromFile('images\' + IntToStr( i + 30 - 5) + '.JPG');
end;
if Value2 = 100 then
begin
Form1.Image[ImageX].Picture.LoadFromFile('images\' + IntToStr( 13 -1 + i ) + '.JPG');
end;
if Value2 = 104 then
begin
Form1.Image[ImageX].Picture.LoadFromFile('images\' + IntToStr(i -1 ) + '.JPG');
end;
if Value2 = 115 then
begin
Form1.Image[ImageX].Picture.LoadFromFile('images\' + IntToStr(i + 40 -2) + '.JPG');
end;
end;
end;

also

delphi gives a error saying 'Undefined Identifier : 'Image';

Upvotes: 0

Views: 809

Answers (5)

philnext
philnext

Reputation: 3402

OK Bryan ... I didn't really see the problem.

The best way seems to be :

   if bFile then
   begin
     Form1.TImage(FindComponent(ImageX)).Picture.LoadFromFile('images\' +sFileName+ '.JPG');
   end;

I suppose that Form.Image[x] is a TImage, tou need to add the Tcomponent Library in the code. See the help for FindCompoent.

Upvotes: 0

Bryan
Bryan

Reputation: 85

Ok well I was up for 29 or so hours straight when i posted this...Sorry

I found my own solution with a bit of Philnext's help

procedure UpdateCardImage(Value,Value2 : Integer; ImageX : String);
var
i,i2 : Integer;
sFileName : string;
bFile : boolean;
begin
 for i := 2 to Value Do
  for i2 := 98 to Value2 Do
    Begin
      bFile := true;
Case Value2 of
  99 : sFileName := IntToStr( i + 30 - 5);
  100 : sFileName := IntToStr(13 -1 + i );
  104 : sFileName := IntToStr( i -1 );
  115 : sFileName := IntToStr(i + 40 -2);
   else bFile := False;
   end;
   if bFile then
   begin
    if ImageX = 'Image1' then
    Form1.Image1.Picture.LoadFromFile('images\' +sFileName+ '.JPG');
    if ImageX = 'Image2' then
    Form1.Image2.Picture.LoadFromFile('images\' +sFileName+ '.JPG');
    if ImageX = 'Image3' then
    Form1.Image3.Picture.LoadFromFile('images\' +sFileName+ '.JPG');
    if ImageX = 'Image4' then
    Form1.Image4.Picture.LoadFromFile('images\' +sFileName+ '.JPG');
    if ImageX = 'Image5' then
    Form1.Image5.Picture.LoadFromFile('images\' +sFileName+ '.JPG');
    if ImageX = 'Image6' then
    Form1.Image6.Picture.LoadFromFile('images\' +sFileName+ '.JPG');
    if ImageX = 'Image7' then
    Form1.Image7.Picture.LoadFromFile('images\' +sFileName+ '.JPG');
  end;
 end;
end;

Abelisto had a interesting answer that would look like it would work but didn't...Props for the suggestion Abelisto.

Upvotes: 0

Abelisto
Abelisto

Reputation: 15614

Form1.Image1 is not equivalent for Form1.Image[1] In your case you must use something like (Form1.FindChildControl('Image' + IntToStr(ImageX)) as TImage).Picture.LoadFromFile();

Upvotes: 4

philnext
philnext

Reputation: 3402

May be a code like :

procedure UpdateCardImage(Value,Value2,ImageX : Integer);
var
i,i2 : integer;
bFile : bolean;
sFileName : string;
begin
  for i := 2 to Value do
    for i2 := 98 to Value2 do
    Begin
      bFile := true;
      Case Value2 of 
        99  : sFileName := IntToStr( i + 30 - 5);
        100 : sFileName := IntToStr( 13 -1 + i );
        115 : sFileName := IntToStr(i + 40 -2);
      else bFile := False;
      end;
      if bFile then Form1.Image[ImageX].Picture.LoadFromFile('images\' +sFileName+ '.JPG');
    end;
end;

Upvotes: 0

Мסž
Мסž

Reputation:

The if statement is the problem. Try to make a function that calculates or returns the filename, so you can just go

for i:=0 to 100 do
  Form1.Image[ImageX].Picture.LoadFromFile('images\' + GetFilename(i));

Even if you have to make an array of filenames and index into it that's better than a loop with if/case statement.

const cFilenames : array[0..2] of string = ('one','two','three')
for i:=low(cFilenames) to high(cFilenames) do
  Form1.Image[ImageX].Picture.LoadFromFile('images\' + cFilenames[i]);

Upvotes: 1

Related Questions