Entry
Delphi: Component:Dynamic: Use: More:How to dynamically use 2 events, once 2 components are created?
Sep 12th, 2003 09:38
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 12 September 2003 - 05:15 pm ------------------
Delphi: Component:Dynamic: Use: More:How to dynamically use 2 events,
once 2 components are created?
Steps: Overview:
1. -Create that 2 or more components e.g. dynamically
or otherwise
2. -Add your event codes in the 'implementation' section
in the source code
Put there your source codes which
you want to have triggered by the events
(e.g. the OnClick event, when clicking on the created button
or memo)
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 this already existing event code with e.g. the OnClick
method for that created component
---
Steps: Worked out:
1. -Create that 2 or more components e.g. dynamically
or otherwise
http://www.faqts.com/knowledge_base/view.phtml/aid/24287/fid/175
In this example, 2 components are dynamically created
(a button and a memo).
And some actions (e.g. changing the caption of the button,
and adding some text to the memo component) are done with it
afterwards.
Note:
Dynamically creating also this event source code itself (for
example the 'SomeButtonEventCreatedDynamicallyByMe' method), so
basically changing the Delphi program you are running (so e.g. not
having it already written and supplied in the 'implementation'
section yourself before) would mean that you somehow dynamically
change the Pascal program source code itself.
One general method, also applicable to other programming languages
(e.g. C#, C++, Java, ...) would be to:
1. load the source code file (e.g. 'unit1.pas')
2. add your wanted source code (e.g. the source code for your
event)
3. compile this changed source code (e.g. with the Delphi
command line compiler, and your new source code filename as a
parameter)
4. then exit your current Delphi program
5. then rerun your current Delphi program
In a language like LISP it is possible to change your currently
running program source code on the fly, without having to stop the
program temporarily. But as far as I know until now, there are
possibly no direct provisions for this in Delphi. So the above
general method might be a possible solution.
So in general your source code itself is not dynamically
generated, while running your program. So what you better can do
is to write some rather general component handling procedures
(e.g. for your newly created memo component), and put this code
in your program, which you then pass that memo component as a
parameter.
Internet: see:
http://www.faqts.com/knowledge_base/view.phtml/aid/24330/fid/175
So if you want to trigger events in the newly created components,
it should be done and inserted before in your source code, after
this components are created (and within their scope, so you could
consider to make your newly created button global and known to all
the methods within that unit.pas file, possibly put the
declaration (e.g. 'var Btn : TButton' in the 'public' section of
that unit file).
2. -Add your event codes in the 'implementation' section
in the source code
Put there your source codes which
you want to have triggered by the events
(e.g. the OnClick event, when clicking on the created button
or memo)
...
implementation
procedure TForm1.SomeButtonEventCreatedDynamicallyByMe( Sender:
TObject );
begin
// do something
ShowMessage( 'Hello World (via the button)' );
end;
procedure TForm1.SomeMemoEventCreatedDynamicallyByMe( Sender:
TObject );
begin
// do something
ShowMessage( 'Hello World (via the memo)' );
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 this already existing event code with e.g. the OnClick
method for that created component
Btn.OnClick := SomeButtonEventCreatedDynamicallyByMe;
...
Mmo.OnClick := SomeMemoEventCreatedDynamicallyByMe;
---
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 );
procedure SomeMemoEventCreatedDynamicallyByMe( 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 (via the button)' );
end;
procedure TForm1.SomeMemoEventCreatedDynamicallyByMe( Sender:
TObject );
begin
// do something
ShowMessage( 'Hello World (via the memo)' );
end;
procedure TForm1.Label1Click(Sender: TObject);
var Btn : TButton;
var Mmo : TMemo;
begin
// Create a button dynamically
Btn := TButton.Create( Self );
Btn.Parent := Self;
Btn.OnClick := SomeButtonEventCreatedDynamicallyByMe;
// Create a memo dynamically
Mmo := TMemo.Create( Self );
Mmo.Parent := Self;
Mmo.OnClick := SomeMemoEventCreatedDynamicallyByMe;
// Do something with that button
Btn.Left := 25; // make sure you see the button
Btn.Top := 200;
Btn.Caption := 'Click me';
// Do something with that memo
Mmo.Text := 'Hello World (in the memo)';
Mmo.Lines.Add( 'Line Added' );
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 and the memo
3. If you then click on that button or the memo it will show you a
message
box with 'Hello World ...':
+-------------------+ +-------------+
| | | |
+-------------------+ | |
| Hello World |
| |
+-------------------+ +-------------+
| Label1 |
+-------------------+
+--------------------+
| |
| |
| |
| |
+--------------------+
---
[Internet: see also:
http://www.faqts.com/knowledge_base/view.phtml/aid/24289/fid/175
----------------------------------------------------------------------