Working with drag and drop

Hello Guys

I need to create a application that allow me to work with sentence build. This way:

I have a form and on it I have two panels,in one panel I have a sentence all scrambled, each word must be scrambled and put in small panel like a block. My second panel will get the blocks until I build my sentence that I listened to. Take a look at the graphic:


 ______________________________________________Panel1____________________________________
|wife working house mouth appeared toy big My was father's dog with in his at a a her when
|________________________________________________________________________________________

Imagine that each word is in one small panel inside the big panel1, now I have the secound panel and I want to drag and
drop the words (small panels) into the secound panel. This way I will form the sentence. I also need to come back the
panel to the first one so that I can correct the sentence.


Thanks





LVL 1
hidrauAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pierre CorneliusCommented:
Here's a simple drag-drop demo. I used TLabels instead of TPanel, but TPanels should also work.

PAS File:
======
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    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:= (Source is TLabel);
end;

procedure TForm1.Panel2DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  with (Source as TControl) do
  begin
    Left:= x;
    Top:= y;
    Parent:= (Sender as TPanel);
  end;
end;

end.


DFM File:
======
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 688
    Height = 41
    Align = alTop
    Caption = 'Panel1'
    TabOrder = 0
    object Label1: TLabel
      Left = 16
      Top = 16
      Width = 19
      Height = 13
      Caption = 'wife'
      DragMode = dmAutomatic
    end
    object Label2: TLabel
      Left = 56
      Top = 16
      Width = 37
      Height = 13
      Caption = 'working'
      DragMode = dmAutomatic
    end
    object Label3: TLabel
      Left = 104
      Top = 16
      Width = 29
      Height = 13
      Caption = 'house'
      DragMode = dmAutomatic
    end
    object Label4: TLabel
      Left = 152
      Top = 16
      Width = 29
      Height = 13
      Caption = 'mouth'
      DragMode = dmAutomatic
    end
    object Label5: TLabel
      Left = 192
      Top = 16
      Width = 45
      Height = 13
      Caption = 'appeared'
      DragMode = dmAutomatic
    end
  end
  object Panel2: TPanel
    Left = 0
    Top = 41
    Width = 688
    Height = 41
    Align = alTop
    Caption = 'Panel1'
    TabOrder = 1
    OnDragDrop = Panel2DragDrop
    OnDragOver = Panel2DragOver
  end
end


Regards
Pierre
hidrauAuthor Commented:
Could you give me this example with panels?

Thanks
Pierre CorneliusCommented:
This should sort you out. Not that it relies on reposition done based on the Align property being set to alLeft.

Pas File:
=====
unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

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

procedure TForm1.Panel2DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  with (Source as TControl) do
  begin
    Parent:= (Sender as TPanel);
    Left:= TControl(Parent).Width;
  end;
end;

procedure TForm1.Panel7DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  if (Source is TPanel) AND (Sender is TPanel) AND (Sender <> Source) then
  begin
    if (TPanel(Sender).Parent = TPanel(Source).Parent) then
    begin
      if (TPanel(Sender).Left < TPanel(Source).Left)
        then (Source as TPanel).Left:= (Sender as TPanel).Left
        else  (Source as TPanel).Left:= (Sender as TPanel).Left+1;
    end
    else begin
      TPanel(Source).parent:= nil; //align changes the position, so delay this repositioning
      TPanel(Source).Left:= TPanel(Sender).Left;
      TPanel(Source).parent:= TPanel(Sender).Parent;
    end;
  end;
end;

procedure TForm1.Panel7DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept:= ((Source is TPanel) AND (Sender is TPanel));
end;

end.

DFM File
=====

object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 688
    Height = 41
    Align = alTop
    Caption = 'Panel1'
    TabOrder = 0
    OnDragDrop = Panel2DragDrop
    OnDragOver = Panel2DragOver
    object Panel3: TPanel
      Left = 385
      Top = 1
      Width = 96
      Height = 39
      Align = alLeft
      Caption = 'appeared'
      DragMode = dmAutomatic
      TabOrder = 0
      OnDragDrop = Panel7DragDrop
      OnDragOver = Panel7DragOver
    end
    object Panel4: TPanel
      Left = 289
      Top = 1
      Width = 96
      Height = 39
      Align = alLeft
      Caption = 'mouth'
      DragMode = dmAutomatic
      TabOrder = 1
      OnDragDrop = Panel7DragDrop
      OnDragOver = Panel7DragOver
    end
    object Panel5: TPanel
      Left = 193
      Top = 1
      Width = 96
      Height = 39
      Align = alLeft
      Caption = 'house'
      DragMode = dmAutomatic
      TabOrder = 2
      OnDragDrop = Panel7DragDrop
      OnDragOver = Panel7DragOver
    end
    object Panel6: TPanel
      Left = 97
      Top = 1
      Width = 96
      Height = 39
      Align = alLeft
      Caption = 'working'
      DragMode = dmAutomatic
      TabOrder = 3
      OnDragDrop = Panel7DragDrop
      OnDragOver = Panel7DragOver
    end
    object Panel7: TPanel
      Left = 1
      Top = 1
      Width = 96
      Height = 39
      Align = alLeft
      Caption = 'wife'
      DragMode = dmAutomatic
      TabOrder = 4
      OnDragDrop = Panel7DragDrop
      OnDragOver = Panel7DragOver
    end
  end
  object Panel2: TPanel
    Left = 0
    Top = 41
    Width = 688
    Height = 41
    Align = alTop
    Caption = 'Panel1'
    TabOrder = 1
    OnDragDrop = Panel2DragDrop
    OnDragOver = Panel2DragOver
  end
end

Regards
Pierre

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.