Destrying Objects on Drag And Drop

Giganews Newsgroups
Subject: Destrying Objects on Drag And Drop
Posted by:  √Čric Fleming Bonilha (er…@digifort.com.br)
Date: Sun, 26 Mar 2006

Hello,

I would like to know how is the right way to destroy objects using drag and
drop.

My application is for camera surveillance, so, I have an scrollbox on the
screen that holds all the showing cameras. I want to let the user to pick up
a camera and remove it from the screen using Drag And Drop out from the
scrollbox.

If I free the camera object on the OnDragDrop event handle for the form,
than, when it gets out from the event handler, it raises an Exception.

I have made a test, I have made a simple form application with 4 labels on
it and I wrote the following code (For Testing purpose only):

procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin

  Accept := TRUE;

end;

procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
begin

  Source.Free;

end;

procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin

  TLabel(Sender).BeginDrag(TRUE);

end;

What it happens is that if I drag and drop the Label4 on the form, it gets
an Access Violation.

To outcome this on my application, I have wrote a code that uses PostMessage
to send a message to destroy the object, so, this message will be processed
after the event handler´s heap order.

This is the right thing to do? How can I make it more professionally?

Thanks

Éric

Replies