Entry
Delphi: Component: Dynamic: Create: How to dynamically create an event, once component is created?
Sep 12th, 2003 09:40
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 11 September 2003 - 10:19 am ------------------
Delphi: Component: Dynamic: Create: How to dynamically create an
event, once component is created?
Steps: Overview:
1. -Create that component e.g. dynamically
or otherwise
2. -Add your event code in the 'implementation' section
in the source code
Put there your source code which
you want to have triggered by the event
(e.g. the OnClick event, when clicking on the created button)
3. -You might have to add a dummy parameter like
( Sender: TObject ) to your event source code (because
this is by default expected for this component in Delphi),
otherwise it might not work
4. -Then inform about this event, by
assigning your event code with e.g. the OnClick method
for that created component
---
Steps: Worked out:
1. -Create that component e.g. dynamically
or otherwise
http://www.faqts.com/knowledge_base/view.phtml/aid/24287/fid/175
2. -Add your event code in the 'implementation' section
in the source code
Put there your source code which
you want to have triggered by the event
(e.g. the OnClick event, when clicking on the created button)
...
implementation
procedure TForm1.SomeButtonEventCreatedDynamicallyByMe( Sender:
TObject );
begin
// do something
ShowMessage( 'Hello World' );
end;
...
3. -You might have to add a dummy parameter like
( Sender: TObject ) to your event source code (because
this is by default expected for this component in Delphi),
otherwise it might not work
4. -Then inform about this event, by
assigning your event code with e.g. the OnClick method
for that created component
Btn.OnClick := SomeButtonEventCreatedDynamicallyByMe;
---
5. That will create alltogether the following program:
--- cut here ---------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
procedure Label1Click(Sender: TObject);
procedure SomeButtonEventCreatedDynamicallyByMe( Sender: TObject );
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.SomeButtonEventCreatedDynamicallyByMe( Sender:
TObject );
begin
// do something
ShowMessage( 'Hello World' );
end;
procedure TForm1.Label1Click(Sender: TObject);
var Btn : TButton;
begin
Btn := TButton.Create(Self);
Btn.Parent := Self;
Btn.OnClick := SomeButtonEventCreatedDynamicallyByMe;
end;
end.
--- cut here ---------------------------------------------------------
6. When you run this program, then
1. It will show the label
2. After clicking on the label, that will show the button.
3. If you then click on that button it will show you a message
box with 'Hello World':
+-------------------+ +-------------+
| | | |
+-------------------+ | |
| Hello World |
| |
+-------------------+ +-------------+
| Label1 |
+-------------------+
---
[Internet: see also: http://www.google.com,
search for 'How to dynamically create an event in Delphi':
http://community.borland.com/article/0,1410,16467,00.html]
http://www.faqts.com/knowledge_base/view.phtml/aid/24327/fid/175
----------------------------------------------------------------------