Link to home
Start Free TrialLog in
Avatar of blitz051697
blitz051697

asked on

Moving Controls at Runtime: Example Please

I'm writting an app that will allow the user to drag/drop controls onto a container and then hook them up via a line.  The user can move the items a runtime.  Are there any examples out there?
1.  Moving controls at runtime
2.  creating this type of container object.
3.  linking controls with a line (that moves when the object is moved by user).

Avatar of BoRiS
BoRiS

blitz

I can give you code as to how to move the controls at run time but dropping them on to a container i guess would have to be done by allowing the container to accept these objects and then link them...

if you want to code let me know.... by comment

remember it's only the code to move object at run time...

Later

BoRiS
Hi,

Here is doing it in one way. Do the following:

1 - Create and empty form
2 - Place a Panel and Button on the form. Those should be named as Panel1 and Button1
3 - Cut and paste the following code for your unit as follows:

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    procedure Panel1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure Panel1DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure FormDragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure FormDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Button1.DragMode := dmAutomatic;
  Panel1.DragMode := dmAutomatic;
end;

procedure TForm1.Panel1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := (Source is TControl) and (Source <> Sender);
end;

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

procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  with (Source as TControl) do
  begin
    Left := X;
    Top := Y;
  end;
end;

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

end.


Link events from object inspector to the above events. For example click on Panel and press F11 to show the object ispector. Switch to events tab and from the drop down list of OnDragOver event select Panel1DragOver etc...

Then run the project. Drag the button over Panel and drop. Then drag the panel over the form and drop. Is that what you want?

Regards,
Igor
Hi BoRis,

Sorry, I do not see you commenting while I prepare the answer.

Igor
hi inter

not to worry, lets see what blitz has to say

Later

BoRiS
Hi there,

excuse me, when you drag button back to the form it does not work so modify the following please:

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

Regards,
Igor
Avatar of blitz051697

ASKER

boris, you say that you can move controls at runtime.  But does your code also allow drawing a graphical line to link them?  Will the line move as the control moves?  
blitz

let me see what i can do...

I'll let you know by comment

later

BoRiS
Hi there,

would you please explain 'drawing a graphical line to link them' more. In addition you may reject my answer so that BoRiS can be able to supply one.

Regards,
Igor
Two controls that are linked would have a line that is drawn between them.   This would be similar to a program like Visio or flowcharting tools. As I move the control (or box or whatever) the line continues to stay linked to any other attached controls.


Yo blitz,
I could offer you a bunch of code again. Some time ago I designed some basic flowcharting like classes with connector classes to graphically display a network environment. The classes are extendable, the base connector class supports 1<->1 connections, but any element supports an unlimited number of connections (and, yes, you can drag them around however you want).
Classes with sample app for 500 pts ? Your turn.

Slash/d003303
Ok Slash, What do ya got?

Blitz
ASKER CERTIFIED SOLUTION
Avatar of d003303
d003303

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial