Board index » delphi » component arrays

component arrays

I know I have seen it before but can't find it now.
Can I group a bunch of components in an array and use at run time.

If I have 30-40 Timage on a page and need to reference thos like:

image[x]  etc

johan

 

Re:component arrays


Johan Lindgren schrieb:

Quote

> I know I have seen it before but can't find it now.
> Can I group a bunch of components in an array and use at run time.

> If I have 30-40 Timage on a page and need to reference thos like:

> image[x]  etc

> johan

Hi Johan,

I guess you create them at design time. You should give them the dafault
names like Image1, Image2 etc. Then, create a variable to hold
references as an array, like:

  private
    image: Array[1..40] of TImage;

Finally, at startup in the OnCreate method of the form, insert:

  for i:=1 to 40 do image[i] :=
TImage(FindComponent('Image'+IntToStr(i)));

HTH
-Michael

Re:component arrays


How about setting the tag property of each control to a unique value and
then using
(FindComponent('Image'+IntToStr(i)) as TImage)
or if you want to use it in an event method:
(FindComponent('Image'+IntToStr((Sender as TImage).tag) as TImage)

--
----------------------------------------------------------------------------
Bruce Meier Consulting CC
57 Woodley Road, Plumstead 7800 SOUTH AFRICA
Ph +27 (0)21 7626691  Cell 083 4148196
bme...@mweb.co.za

Quote
Johan Lindgren <johan.lindg...@tt.se> wrote in message

news:OflF3.4861$yU2.9234@nntpserver.swip.net...
Quote
> I know I have seen it before but can't find it now.
> Can I group a bunch of components in an array and use at run time.

> If I have 30-40 Timage on a page and need to reference thos like:

> image[x]  etc

> johan

Re:component arrays


Quote
Bruce Meier wrote:

> How about setting the tag property of each control to a unique value and
> then using
> (FindComponent('Image'+IntToStr(i)) as TImage)

This will work but has nothing to do with the Tag property. Tag must not be
set.

Quote
> or if you want to use it in an event method:
> (FindComponent('Image'+IntToStr((Sender as TImage).tag) as TImage)

If he wants to access Image[9], he is not in an event method, that's why
Sender is not valid here. If this *is* used inside an event method *of one of
the images*, it is much easier to say: (Sender as TImage). That's enough.

-Michael

Re:component arrays


In article <37E6992F.86E3E...@foni.net>, Michael Winter <win...@foni.net>
writes:

Quote
>Bruce Meier wrote:

>> How about setting the tag property of each control to a unique value and
>> then using
>> (FindComponent('Image'+IntToStr(i)) as TImage)

>This will work but has nothing to do with the Tag property. Tag must not be
>set.

Why _not_ set the tag?

Gordon
~~~~~~~~~~~~~~~~~~~~~~~~
Certifiable - definitely <g>

http://members.aol.com/mgcsoft/  (the function junction equation editor)
http://members.aol.com/delphistuf/delphstf.htm   (Delphi bits and bobs and a
few links)

MGCSoft

Re:component arrays


GWhit41980 schrieb:

Quote

> In article <37E6992F.86E3E...@foni.net>, Michael Winter <win...@foni.net>
> writes:

> >Bruce Meier wrote:

> >> How about setting the tag property of each control to a unique value and
> >> then using
> >> (FindComponent('Image'+IntToStr(i)) as TImage)

> >This will work but has nothing to do with the Tag property. Tag must not be
> >set.

> Why _not_ set the tag?

Sorry Gordon, my mistake. This should read "You do not need / It is not
neccesary to set the Tag to use this statement". (I forgot that the
negation of "must" has a comletely other meaning then the negation of
"muessen" in German language.)

-Michael

Re:component arrays


Michael Winter skrev i meddelandet <37E71FE5.BBCC8...@aquila.de>...

Quote
>> >> How about setting the tag property of each control to a unique value
and
>> >> then using
>> >> (FindComponent('Image'+IntToStr(i)) as TImage)

Thanks for all help and suggestions.
Michaels idea worked perfectly. I have now 50 timage and 50 tlabel that I
can reference through image[x] and label[x], place and fill with the info
when the page is updated.
I am using the tag property to keep track of the id of the picture
displayed.
So if I display a certain picture in an image and the user double clicks on
that image I can through the tag property find out the id of the actual
picture. Regardless of which position that image has in the image-array.

johan

Re:component arrays


Quote
In article <37E71FE5.BBCC8...@aquila.de>, Michael Winter <m...@aquila.de> writes:
>Sorry Gordon, my mistake. This should read "You do not need / It is not
>neccesary to set the Tag to use this statement". (I forgot that the
>negation of "must" has a comletely other meaning then the negation of
>"muessen" in German language.)

>-Michael

No problem, you had me worried for a bit though!, if my German had been a
quarter as good as your English I'd probably have known that :)

Gordon
~~~~~~~~~~~~~~~~~~~~~~~~
Certifiable - definitely <g>

http://members.aol.com/mgcsoft/  (the function junction equation editor)
http://members.aol.com/delphistuf/delphstf.htm   (Delphi bits and bobs and a
few links)

MGCSoft

Re:component arrays


In article <19990920191934.07848.00000...@ngol03.aol.com>, gwhit41...@aol.com

Quote
(GWhit41980) writes:
>>Bruce Meier wrote:

>>This will work but has nothing to do with the Tag property. Tag must not be
>>set.

>Why _not_ set the tag?

I know these comments arose originally because of a language translation bug
<g> but setting Tags is in general _not_ a good Idea because it makes the code
opaque. It is the same as using "magic numbers". It makes you refer to other
(or hidden) areas of your application code before you can understand what the
particular element of code is doing.

If you absolutely _have_ to do it then either put a chunk of comments by the
code identifying which tag is which component, or set a load of constants which
make it clear (in this case the constants might have a name descriptive of the
image), or set the Tag values in the FormCreate event handler.

My strictures, of course, are not valid if you are writing some form of
meta-handler (ie a generic handler which is applied to any or all objects, and
not dependant on the particular object).

Alan Lloyd
alangll...@aol.com

Re:component arrays


In article <19990921160856.07765.00000...@ngol06.aol.com>, alangll...@aol.com

Quote
(AlanGLLoyd) writes:
>I know these comments arose originally because of a language translation bug
><g> but setting Tags is in general _not_ a good Idea because it makes the
>code
>opaque. It is the same as using "magic numbers". It makes you refer to other
>(or hidden) areas of your application code before you can understand what the
>particular element of code is doing.

Fair comment.

Quote

>If you absolutely _have_ to do it then either put a chunk of comments by the
>code identifying which tag is which component, or set a load of constants
>which
>make it clear (in this case the constants might have a name descriptive of
>the
>image), or set the Tag values in the FormCreate event handler.

I doubt whether there are any instances where you _absolutely_have_to_ but in
this instance I can't think of an easier way than

  for var:= 0 to high(Images) do
    begin
        Images[var]:= TImage.Create(self);
        ...
        ...
        Images[var].tag:= var;
    end;

Very easy and IMO very readable.

[snip]

Gordon.

~~~~~~~~~~~~~~~~~~~~~~~~
Certifiable - definitely <g>

http://members.aol.com/mgcsoft/  (the function junction equation editor)
http://members.aol.com/delphistuf/delphstf.htm   (Delphi bits and bobs and a
few links)

MGCSoft

Re:component arrays


GWhit41980 skrev i meddelandet >I doubt whether there are any instances
where you _absolutely_have_to_ but in

Quote
>this instance I can't think of an easier way than
>  for var:= 0 to high(Images) do
>    begin
>        Images[var]:= TImage.Create(self);
>        ...
>        ...
>        Images[var].tag:= var;
>    end;

I tried this but I did not get the images to show up in my application. But
maybe I was doing something else wrong.
If you use this approach, do you need to free the images also?

johan

Re:component arrays


You'll have to Set Image's Parent property (and Visible, too, if they
don't show up).

var
  Image: TImage;
.....
  for var:= 0 to high(Images) do
    begin
       Image:=TImage.Create(self);
       Image.parent:=[Some   control...]
       Images[var]:= Image;
   end;

...and yes, you'll have to free them...

--

Bjoerge

Johan Lindgren skrev i meldingen
<5k%F3.870$wH4.1...@nntpserver.swip.net>...

Quote

>GWhit41980 skrev i meddelandet >I doubt whether there are any instances
>where you _absolutely_have_to_ but in
>>this instance I can't think of an easier way than
>>  for var:= 0 to high(Images) do
>>    begin
>>        Images[var]:= TImage.Create(self);
>>        ...
>>        ...
>>        Images[var].tag:= var;
>>    end;

>I tried this but I did not get the images to show up in my application.
But
>maybe I was doing something else wrong.
>If you use this approach, do you need to free the images also?

>johan

Re:component arrays


In article <19990922023737.08086.00000...@ngol02.aol.com>, gwhit41...@aol.com

Quote
(GWhit41980) writes:
>I doubt whether there are any instances where you _absolutely_have_to_ but in
>this instance I can't think of an easier way than

>  for var:= 0 to high(Images) do
>    begin
>        Images[var]:= TImage.Create(self);
>        ...
>        ...
>        Images[var].tag:= var;
>    end;

>Very easy and IMO very readable.

Agreed, but my last para exonerates this sort of coding from my comments.  I
would count this as a "meta-handler" where the particular identifier does not
matter - you are just handling them all, and the particular image is readily
identifiable in the code as Images[var].

And in answer to Johann's query, Images must be displayed on a Canvas, which is
a property of some WinControl. So somewhere you have to code :-

Images[var].Canvas := "some WinControl".Canvas

There are many WinControls which do have a canvas.

And yes you do need to free the images.

Alan Lloyd
allangll...@aol.com

Re:component arrays


Quote
> .....
>   for var:= 0 to high(Images) do
>     begin
>        Image:=TImage.Create(self);
>        Image.parent:=[Some   control...]
>        Images[var]:= Image;
>    end;

> ...and yes, you'll have to free them...

Doesn't the "self" passed to the constructor handle the tImge releases?
Quote

> --

> Bjoerge

> Johan Lindgren skrev i meldingen
> <5k%F3.870$wH4.1...@nntpserver.swip.net>...

> >GWhit41980 skrev i meddelandet >I doubt whether there are any instances
> >where you _absolutely_have_to_ but in
> >>this instance I can't think of an easier way than
> >>  for var:= 0 to high(Images) do
> >>    begin
> >>        Images[var]:= TImage.Create(self);
> >>        ...
> >>        ...
> >>        Images[var].tag:= var;
> >>    end;

> >I tried this but I did not get the images to show up in my application.
> But
> >maybe I was doing something else wrong.
> >If you use this approach, do you need to free the images also?

> >johan

Re:component arrays


In article <19990922144112.11196.00000...@ngol03.aol.com>, alangll...@aol.com

Quote
(AlanGLLoyd) writes:
>Agreed, but my last para exonerates this sort of coding from my comments.  I
>would count this as a "meta-handler" where the particular identifier does not
>matter - you are just handling them all, and the particular image is readily
>identifiable in the code as Images[var].

Sorry, I interpreted it as _all_objects_.

Gordon.
~~~~~~~~~~~~~~~~~~~~~~~~
Certifiable - definitely <g>

http://members.aol.com/mgcsoft/  (the function junction equation editor)
http://members.aol.com/delphistuf/delphstf.htm   (Delphi bits and bobs and a
few links)

MGCSoft

Other Threads