Entry
Delphi: Component: Dynamic: Simple: How to dynamically create add/move/delete/drag/drop a button?
Jun 18th, 2005 12:27
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 05 September 2004 - 07:29 am ------------------
Delphi: Component: Dynamic: Simple: How to dynamically create
add/move/delete/drag/drop a button?
---
Steps: Overview:
1. -Put 3 buttons on a form
1. -Double click on button1
and name the caption 'Create'
Put the following code in it:
--- cut here: begin --------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
MyBtn1 := TButton.Create( Self );
MyBtn1.Parent := Self;
end;
--- cut here: end ----------------------------------------------------
2. -Double click on button2
and name caption 'Delete'
Put the following code in it:
--- cut here: begin --------------------------------------------------
procedure TForm1.Button2Click(Sender: TObject);
begin
TObject( MyBtn1 ).Free();
end;
--- cut here: end ----------------------------------------------------
3. -Double click on button3
and name the caption 'Move'
Put the following code in it:
--- cut here: begin --------------------------------------------------
procedure TForm1.Button3Click(Sender: TObject);
begin
MyBtn1.Left := Mouse.CursorPos.X;
MyBtn1.Top := Mouse.CursorPos.Y;
end;
--- cut here: end ----------------------------------------------------
4. Because that button must be known in all
the other buttons here, make it a global variable
--- cut here: begin --------------------------------------------------
var
Form1: TForm1;
MyBtn1 : TButton;
--- cut here: end ----------------------------------------------------
5. So all together it becomes (after also adding
some extra 'try .. except' code in it for handling exceptions)
--- cut here: begin --------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MyBtn1 : TButton; // use an array if more buttons
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
// var Btn : TButton;
begin
try
MyBtn1 := TButton.Create( Self );
MyBtn1.Parent := Self;
except
ShowMessage( 'could not create a button' );
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
try
TObject( MyBtn1 ).Free();
except
ShowMessage( 'could not delete the button (possibly still not
created)' );
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
try
MyBtn1.Left := Mouse.CursorPos.X;
MyBtn1.Top := Mouse.CursorPos.Y;
except
ShowMessage( 'could not move the button (already deleted or still
not created)' );
end;
end;
end.
--- cut here: end ----------------------------------------------------
6. To run it, do the following in this order:
1. first create a button by clicking button1,
2. then possibly move it by clicking button2
3. then possibly delete it by clicking button3
---
---
7. To add drag and drop to it in an easy way, use e.g. the
OnDragDrop and OnDrapOver event from your form.
1. Double click on the 'Events' tab, then 'OnDragDrop' of the
form and add the following code
--- cut here: begin --------------------------------------------------
procedure TForm1.FormDragDrop( Sender, Source: TObject; X, Y:
Integer );
begin
if Source is TButton then
begin
TButton( Source ).Left := X;
TButton( Source ).Top := Y;
end;
end;
--- cut here: end ----------------------------------------------------
2. Double click on the 'Events' tab, then 'OnDragOver' of the
form and add the following code
--- cut here: begin --------------------------------------------------
procedure TForm1.FormDragOver( Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := ( Source is TButton );
end;
--- cut here: end ----------------------------------------------------
8. So all together it becomes (after also adding
some extra 'try .. except' code in it for handling exceptions)
--- cut here: begin --------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormDragDrop(Sender, Source: TObject; X, Y: Integer);
procedure FormDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MyBtn1 : TButton; // use an array if more buttons
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
try
MyBtn1 := TButton.Create( Self );
MyBtn1.Parent := Self;
MyBtn1.DragMode := dmAutomatic;
except
ShowMessage( 'could not create a button' );
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
try
TObject( MyBtn1 ).Free();
except
ShowMessage( 'could not delete the button (possibly still not
created)' );
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
try
MyBtn1.Left := Mouse.CursorPos.X;
MyBtn1.Top := Mouse.CursorPos.Y;
except
ShowMessage( 'could not move the button (already deleted or still
not created)' );
end;
end;
procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
begin
if Source is TButton then
begin
TButton( Source ).Left := X;
TButton( Source ).Top := Y;
end;
end;
procedure TForm1.FormDragOver( Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := ( Source is TButton );
end;
end.
--- cut here: end ----------------------------------------------------
9. To run it, do the following in this order:
1. first create a button by clicking button1,
2. then possibly move it by clicking button2
3. possibly drag and drop it by clicking on the just created
button and dragging it around
4. then possibly delete it by clicking button3
figure:
3 buttons on the form
+-----------------------------------------------------------+
| |
| |
| |
| +----------+ |
| | Create | |
| +----------+ |
| |
| +----------+ |
| | Move | |
| +----------+ |
| |
| +----------+ |
| | Delete | |
| +----------+ |
| |
| |
+-----------------------------------------------------------+
figure:
after clicking the 'Create' button, a new button is created
in the upper left corner
+-----------------------------------------------------------+
|+----------+ |
|| | |
|+----------+ |
| +----------+ |
| | Create | |
| +----------+ |
| |
| +----------+ |
| | Move | |
| +----------+ |
| |
| +----------+ |
| | Delete | |
| +----------+ |
| |
| |
+-----------------------------------------------------------+
figure:
after clicking the 'Move' button, the new button moves
to another position (and will move a little bit, every
time you click the 'Move' button on a different position
on it)
+-----------------------------------------------------------+
| |
| |
| |
| +----------+ |
| | Create | |
| +----------+ |
| |
| +----------+ |
| | Move | |
| +----------+ |
| |
| +----------+ |
| | Delete | |
| +----------+ +----------+ |
| | | |
| +----------+ |
+-----------------------------------------------------------+
figure:
If you click on the new button, you can drag it around
and drop it on a new position with your mouse
+-----------------------------------------------------------+
| |
| |
| +----------+ |
| +----------+ | | |
| | Create | +----------+ |
| +----------+ ^ |
| | your |
| +----------+ mousepointer |
| | Move | |
| +----------+ |
| |
| +----------+ |
| | Delete | |
| +----------+ |
| |
| |
+-----------------------------------------------------------+
figure:
If you click on the 'Delete' button, your newly created
button will be removed.
+-----------------------------------------------------------+
| |
| |
| |
| +----------+ |
| | Create | |
| +----------+ |
| |
| +----------+ |
| | Move | |
| +----------+ |
| |
| +----------+ |
| | Delete | |
| +----------+ |
| |
| |
+-----------------------------------------------------------+
---
---
Successfully run using Microsoft Windows XP Professional and Delphi 7.
---
---
Internet: see also:
---
Delphi: Component: Dynamic: Create: How to create dynamically a button
component (e.g. when clicking on another component)?
http://www.faqts.com/knowledge_base/view.phtml/aid/24287/fid/175
---
[Internet: see also: http://www.google.com search for 'how to
dynamically drag in Delphi':
http://delphi.about.com/cs/adptips2001/a/bltip0801_4.htm]
---
[Internet: see also: http://www.google.com search for 'how to
dynamically drag in Delphi':
http://delphi.about.com/library/weekly/aa080399.htm]
---
Delphi: Drag/drop: Link: Can you give an overview of links?
http://www.faqts.com/knowledge_base/view.phtml/aid/36803/fid/175
----------------------------------------------------------------------