Author Topic: howto get the array index of a passed TObject  (Read 7853 times)

jw

  • Full Member
  • ***
  • Posts: 126
howto get the array index of a passed TObject
« on: January 22, 2011, 10:35:46 pm »
I've created an array of timages see code.
my quaetion has to do with howto determin the objects array index once it's been clicked again see code below
procedure tileclick.  My method does work it just seems wrong so I'm asking if there's a better way to do it.

Code: [Select]
images: array of TImage;



procedure TForm1.FormCreate(Sender: TObject);
var
arraycount:integer;

begin

for arraycount:=0 to 15 do
        begin

             images[arraycount]:= TImage.Create(Self);
             
           

             TImage(images[arraycount]).Parent := Self;


             //make images clickable
             TImage(images[arraycount]).OnClick:= @tileclick;




      TImage(images[arraycount]).Width := 40;
              TImage(images[arraycount]).Height := 40;


              TImage(images[arraycount]).visible := true;
              TImage(images[arraycount]).stretch := true;


             

              TImage(images[arraycount]).top := 40;
              TImage(images[arraycount]).left := arraycount*40;
                                                   




        end;                                 
end;   




procedure TForm1.tileClick(Sender: TObject);
var
tile:integer;

begin

//this is how I figure out the array index of the clicked object
//this seems like a really stupid way to do it and I'm asking for
//the right way or better way

     tile:=-1;
     repeat
           tile:=tile+1;
           Application.ProcessMessages;
     until Sender=images[tile];

end;                             



ik

  • Jr. Member
  • **
  • Posts: 88
  • ik
    • LINESIP
Re: howto get the array index of a passed TObject
« Reply #1 on: January 22, 2011, 10:55:42 pm »
I will use the Tag property to store the index of the array, and do something like this:

Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
var
arraycount:integer;

begin

for arraycount:=0 to 15 do
        begin
...
         TImage(images[arraycount]).Tag := arraycount;
...
end;
         
procedure TForm1.tileClick(Sender: TObject);
begin
  TImage(Sender).tag
end;

That way I know the position in the array, but not always you need to know this if you can use th actual object, so it depends also on your needs.

Dibo

  • Hero Member
  • *****
  • Posts: 1061
Re: howto get the array index of a passed TObject
« Reply #2 on: January 22, 2011, 11:05:15 pm »
There is many ways. Your code is good, but you can simplify this:

1. TImage have Tag property, so you can use this to store index:
Code: Pascal  [Select][+][-]
  1. TImage(images[arraycount]).Tag := arraycount
  2. .....
  3. procedure TForm1.tileClick(Sender: TObject);
  4. var
  5.         tile:integer;
  6. begin
  7.   if Sender is TImage then
  8.     tile := TImage(Sender).Tag;
  9.   .........
  10. end;
  11.  

2. You can use TList object instead of array:
Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.     procedure FormCreate(Sender: TObject);
  3.   private
  4.     FImageList: TList;
  5.     { private declarations }
  6.   public
  7.     { public declarations }
  8.   end;  
  9.  
  10.  
  11. procedure TForm1.FormCreate(Sender: TObject);
  12. var
  13.   i: Integer;
  14.   tmp: TImage;
  15. begin
  16.   FImageList := TList.Create;
  17.  
  18.   for i := 0 to 15 do
  19.   begin
  20.     tmp := TImage.Create(Self);
  21.     FImageList.Add(tmp);
  22.     with tmp do begin
  23.       Parent := Self;
  24.     //make images clickable
  25.       OnClick:= @tileclick;
  26.       Width := 40;
  27.       Height := 40;
  28.       visible := true;
  29.       stretch := true;            
  30.       top := 40;
  31.       left := arraycount*40;
  32.     end;
  33.   end;
  34. end;  
  35.  
  36. procedure TForm1.FormDestroy(Sender: TObject);
  37. var
  38.   i: Integer;
  39. begin
  40.   // If you assign owner to TImage, then you don't need this free below.
  41.   // You can also use TObjectList which have auto-free instead of TList
  42.   for i:=0 to FImageList.Count-1 do
  43.     TImage(FImageList.Items[i]).Free;
  44.  
  45.   FImageList.Free;
  46. end;
  47.  
  48. procedure TForm1.tileClick(Sender: TObject);
  49. var
  50.   tile:integer;
  51. begin
  52.   tile := FImageList.IndexOf(Sender);
  53. end;
  54.  
« Last Edit: January 23, 2011, 12:02:10 am by Dibo »

jw

  • Full Member
  • ***
  • Posts: 126
Re: howto get the array index of a passed TObject
« Reply #3 on: January 22, 2011, 11:43:23 pm »
thank you ik and Dibo very much!! :D


I've seen some threads marked solved, how do I do this.

Dibo

  • Hero Member
  • *****
  • Posts: 1061
Re: howto get the array index of a passed TObject
« Reply #4 on: January 22, 2011, 11:59:39 pm »
I always do this by editing first post and changing subject with "[SOLVED]" prefix

starmate

  • Newbie
  • Posts: 2
Re: howto get the array index of a passed TObject
« Reply #5 on: February 10, 2011, 03:30:38 pm »
Hello everyone,

I found this thread and I am very glad because this is just what I was looking for  :)

However, I have a little issue.

Code: [Select]
FImageList.Count
always return 0

Is someone experiencing this problem as well?

Best regards,

Jon

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: howto get the array index of a passed TObject
« Reply #6 on: February 10, 2011, 04:37:29 pm »
Code: [Select]
images: array of TImage;

procedure TForm1.FormCreate(Sender: TObject);
var
arraycount:integer;
begin

for arraycount:=0 to 15 do
        begin
             images[arraycount]:= TImage.Create(Self);

I didn't read further because what you are doing, is accessing memory space that is not defined at all.

Either
Code: [Select]
images: array[0..15] of TImage;
or

Code: [Select]
images: array of TImage;

SetLength(images,16);

To the question, i guess the Tag property from TImage is indeed easiest way. Set it to same as index when putting images in array.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: howto get the array index of a passed TObject
« Reply #7 on: February 10, 2011, 04:42:28 pm »
Code: [Select]
FImageList.Count always return 0
Most likely you aren't adding new items to the list correctly. You are using Add or Insert functions? May need to see more code for better help.

 

TinyPortal © 2005-2018