Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DELETE a specific TImage in runtime?? 2

Status
Not open for further replies.

luistsousa

Technical User
May 12, 2003
66
PT
Hello

I created a variable number of TImages in runtime, every with a different name. How can I delete a specific image, in runtime?


Many Thanks
Luis
 
u can hide, or totally destroy:

Image1->Hide() for hiding

or: Image1->~TImage() to destroy
 
Hi

The problem is that I create a number variable of

imagem =new TImage(this);



Best Regards

 
click on the image and use dynamic cast for getting the handle so to speak.

TObject *REdit;
if(dynamic_cast<TRichEdit *>(REdit) != 0)
do something;

I have declared a global REdit pointer.

I used this on a texteditor that has several open richedits.
they all share a common function where I can determine the richedit that has been selected. The dynamic cast will appoint the REdit pointer to the object that called the function. I dont have to know the index or name, only that at this time the REdit pointer is pointing to the richedit that is calling the funtion. usually by the onclick or onkeypress. I

tomcruz.net
 
try this. I havent done anything like this before so I used it as a learning exercise. thanks for the opportunity.

#include <vcl.h>
#pragma hdrstop

#include &quot;Unit1.h&quot;

#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;

int x;
TImage *image;

__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
x = 1;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
image = new TImage(this);
image->Parent = Form1;
image->Left = image->Width * x;
image->Picture->LoadFromFile (&quot;key.bmp&quot;);
image->OnClick = imageClick;
x++;
}

// declare this as public in the form1 header
void __fastcall TForm1::imageClick(TObject *Sender)
{
if(dynamic_cast<TImage *>(Sender) != 0)
{
ShowMessage (&quot;test Message one&quot;);
image = dynamic_cast<TImage *>(Sender);
}
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
delete image;
}

tomcruz.net

 
Hi Tom

Many thanks for Your tip. I use
image = dynamic_cast<TImage *>(Sender);
delete image;

and works. The problem is sometimes appear a error message with indication of &quot;acess violation ar address 400.... in module rtl60.bpl .......&quot;

How can I fix this?

Best regards

Luis
 
You got to check for sender = NULL

image = dynamic_cast<TImage *>(Sender);
if(image)
delete image;

that would be my first guess.

tomcruz.net

 
it may not be the timage stuff. look at the rest of your code. I wish I could help further. good luck

tomcruz.net
 
dont give up yet.

I will expect to see a new thread soon titled


error when deleting timage
&quot;acess violation ar address 400.... in module rtl60.bpl .......&quot;


and show some code. ;-)

tomcruz.net
 
It would be simplest to use a TList to keep track of the images.

TList *MyImageList = new TList();
TImage *NewImage = new TImage(this);
NewImage->Parent = this;

MyImageList->Add(NewImage);

Later, to delete the image...

TImage *ImageToDelete = (TImage*) MyImageList->Items[Index];
delete ImageToDelete;
MyImageList->Delete(Index);

Chris
 
Hi Tom and Chris

Many thanks for Your contribution.

I have test the method :

image = dynamic_cast<TImage *>(Sender);
delete image;

with a very simple code like in down. The error message continue to appear.


void __fastcall TForm1::Button2Click(TObject *Sender)
{
imagem =new TImage(this);
imagem->Height=20;
imagem->Width=34;
imagem->Top=34;
imagem->Left=345;
imagem->Parent=this;
imagem->AutoSize = false;
imagem->Stretch=true;
imagem->Picture->LoadFromFile(&quot;j:/1750.jpg&quot;);
imagem->Visible=true;
imagem->OnClick=click;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::click (TObject *Sender){
imagem=dynamic_cast<TImage *>(Sender);
if(imagem)
delete imagem;
}

 
I also tested the method of Chris. Is unebelived but the error message appear again.
I had reinstaled the builder 6, but nothing...


Regards
Luis
 
did you try the complete code above.

the code in the fifth posting works.

try it as complete and as is.
 
void __fastcall TForm1::click (TObject *Sender){
imagem=dynamic_cast<TImage *>(Sender);
if(imagem)
delete imagem;
}


you are in the timage object. if you delete it now your pulling the rug out from under you. you have to assign the image to a pointer and then later delete it. thats why I used the second button to delete the image. you could use a popup menu/dialog/form to delete from there maybe.

I think you will have to do something other than delete the image directly from an image click.

tomcruz.net
 
Definitely can't delete the image in an OnClick routine for that image. I didn't notice that earlier. Your &quot;click&quot; function is returning to an invalid address since you just deleted it's return. There are many work arounds. Delete it with a popup, different button, put it in a queue to be deleted, etc. Just as long as you aren't executing any code inside the code-space of that image.

Chris
 
Many thanks

Finally I had success with this operation.

Best Regards
Luis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top