Board index » delphi » Can't free randomly placed TImage objects

Can't free randomly placed TImage objects

Hi there,

I am trying to Free TImage objects which I create during run-time and
then randomly place on a form.

I am maintaining a list of the objects together with a unique name by
using a TStringList object.

I am associating a popupmenu with each TImage object I create during
run-time.
A right click of the mouse button on any of the objects brings up that
pop-up menu which has a delete item on it.

Clicking the delete item should Free the object and make it disappear.
Problem is that I can't figure out how.

I appreciate any help anybode is able to offer.

Regards,
Danny.

 

Re:Can't free randomly placed TImage objects


Hi Danny,

I think you need to look at the PopupComponent property of TPopupMenu so
that you can get a reference to the correct TImage. You could then use
the IndexOfObject function of TStringList to find the correct place in
the TStringList in order to delete the object.

Hope this helps. Let me know if you need more details.

Cheers,
Carl

Quote
Danny & Julia wrote:

> Hi there,

> I am trying to Free TImage objects which I create during run-time and
> then randomly place on a form.

> I am maintaining a list of the objects together with a unique name by
> using a TStringList object.

> I am associating a popupmenu with each TImage object I create during
> run-time.
> A right click of the mouse button on any of the objects brings up that
> pop-up menu which has a delete item on it.

> Clicking the delete item should Free the object and make it disappear.
> Problem is that I can't figure out how.

> I appreciate any help anybode is able to offer.

> Regards,
> Danny.

Re:Can't free randomly placed TImage objects


That will delete the entry from the string list, but I think he's having a
problem
removing and freeing the image object itself.  You can store the name of
the image object (not the file loaded into the TImage, rather, the name of
the TImage
itself - as found in the object inspector), but attempting to free it
directly
will probably give you an error.  For example, if your program has created
3 images, say, Image1, Image2, and Image3, your string list will contain
Image1, Image2, and Image3.  If you were to retrieve the name of the first
image
from the string list, store it in a string variable like strImageToFree,
then attempt to do a
strImageToFree.Free, you'd get a 'Record, Object, or Class Type required'
error.
Same as if you typed 'Image1'.Free.

What you'd need to do is to use the FindComponent method to "Find" the
component
to free based on its name, then cast it to a TImage. Like so:

    TImage(FindComponent('Image1')).Free;

Plug your image name inside the parenthesis for FindComponent.

Hope this helps.
Luck,
DL

Quote
Carl Caulkett wrote in message <367E2D57.8347B...@dircon.co.uk>...
>Hi Danny,

>I think you need to look at the PopupComponent property of TPopupMenu so
>that you can get a reference to the correct TImage. You could then use
>the IndexOfObject function of TStringList to find the correct place in
>the TStringList in order to delete the object.

>Hope this helps. Let me know if you need more details.

>Cheers,
>Carl

>Danny & Julia wrote:

>> Hi there,

>> I am trying to Free TImage objects which I create during run-time and
>> then randomly place on a form.

>> I am maintaining a list of the objects together with a unique name by
>> using a TStringList object.

>> I am associating a popupmenu with each TImage object I create during
>> run-time.
>> A right click of the mouse button on any of the objects brings up that
>> pop-up menu which has a delete item on it.

>> Clicking the delete item should Free the object and make it disappear.
>> Problem is that I can't figure out how.

>> I appreciate any help anybode is able to offer.

>> Regards,
>> Danny.

Other Threads