• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Drag&Drop a panel from a panel to another

I need to drag a panel from another panel toa 3th panel .
Erase the panel from first panel and create it in the second panel .
0
ginsonic
Asked:
ginsonic
  • 3
1 Solution
 
CalvinDayCommented:
Place Panel1, Panel2 on a form. Place Panel3 on Panel1. Set Panel3's Dragmode:=dmAutomatic. Here's the code to do the move.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    procedure Panel2DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure Panel2DragDrop(Sender, Source: TObject; X, Y: Integer);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

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

procedure TForm1.Panel2DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  Panel3.Parent:=Panel2;
end;

end.
0
 
ginsonicAuthor Commented:
I wish to simulate the drag&drop .
Your code just splash the panel in another panel . I wish a real drag&drop .
Can help me ?
0
 
CalvinDayCommented:
Your form and both panels must have this function in the OnDragOver action.

procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Panel3.Parent:=Sender as TWinControl;
  Accept:=True;
  panel3.Left:=x;
  panel3.top:=y;
end;

and the panel3 must have the following  function in the OnDragOver action. This is done so that as the mouse moves over panel3, it will pass its movements to the parent.

procedure TForm1.Panel3DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  FormDragOver(Panel3.Parent, Source, Panel3.Left+X, Panel3.Top+Y, State, Accept);
end;
0
 
CalvinDayCommented:
BTW if you want the dragging centered, change the following:

  panel3.Left:=x-panel3.Width div 2;
  panel3.top:=y-panel3.height div 2;
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now