Back

Drag/Drop Using Color Example

Object: Changing the TMemo background using a Drag/Drop with TShapes Brush Color.

TShape as the source of the drop and Memo will accept.

Add a TShape(s) Component to your form.(Or 16 of the TShapes, one for each color, or you can add even more  using color codes.) Change the TShape's individual Brush Colors to colors like clRed, clBlue, clAqua, etc.

Next: Set each TShape's Drag property to "AUTOMATIC"

Add a TMemo Component to your form.
Click on the Memo and add this code for the event handler
OnDragOver in the Object Inspector.

Accept := Source Is TShape;
Next: Making sure your are still focused on the Memo, back in the Object Inspector click on the event handler OnDragDrop and add this:

   Memo1.Color := (Source As TShape).Brush.Color;

   {Don't forget the period after (Source As Tshape)}

(Your code should now look like this.)

procedure TForm1.Memo1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
   Accept := Source Is TShape;
end;

procedure TForm1.Memo1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
   Memo1.Color := (Source As TShape).Brush.Color;
end;

That's it! After you have compiled your program, run it, grab a TShape with your mouse, then drag it and drop it on the Memo. The Memo's background will change to the color of the TShape's color.

This example came from Clayton Turner


Back