Author: Fernando Martins
Sometimes it's necessary to create a visual object, like a button or a label, at
run time. Here I'll show how to do it with a simple example.
Answer:
To explain how to create a visual object at run-time, there's some simple notions
that is necessary to understood. I'll try to explain it in a simple and fast way
(so don't bother with any "less accurate" information). Simplifying things, objects
have two things: properties and methods. A visual object is not different, it has
properties, methods and can act on event handling.
A visual object's life cycle is just like any other object life cycle, it must be
created, will "live" and at the end will die. Follwoing this three basic life cycle
steps:
When creating a visual object, it's necessary to say who will be the parent, who
will have it has a child. Example: when deploying a TPanel on a TForm, the TForm is
the TPanels' parent, when deploying a TLabel on the TPanel, the TPanel is the
TLabels' parent.
To give the object some "life", it's necessary to code the answer to an event.
Example: the OnKeyUp event on a TForm tells the TForm what to do when the user
releases a key.
When the object is no longer needed, it should be removed. Example: when you have a
TLabel that you no long need, you delete it.
So, let's take a simple case to exemplefy this. Let's create a TButton and give it
some life.
Start a new project, name your main form as "Form1" and specify these private
variables:
MyButton: TButton;
IsAlive: Boolean;
Deploy a TButton on Form1, caption it as "Manage Button" and on the OnClick event
write the following code:
1 // create the button
2 MyButton := TButton.Create(Form1);
3 with MyButton do
4 begin
5 // setting the buttons' parent
6 Parent := Form1;
7 // setting the alignment and size
8 Top := 50;
9 Left := 10;
10 Width := 100;
11 // showing it
12 Caption := "&It Works!";
13 Visible := True;
14 end;
This creates the button when the "Manage Button" is pushed. Run the application and
click the "Manage Button".
This is not really usefull util the button created ar run-time actually do
anything, so let's give it life. Let's build a procedure able to respond to a
OnClick event. On your Form1 private declaration write:
procedure MyButtonOnClickEvent(Sender: TObject);
and write the following code for it:
15 procedure TForm1.MyButtonOnClickEvent(Sender: TObject);
16 begin
17 ShowMessage("Well, it really works!");
18 end;
How do we know this will work? Well, if you check the TButton help, you will see
that the OnClick event has the same signature as the MyButtonOnClickEvent that was
just coded. Whenever you need to build an event, check the documentation on that
object's event and make a procedure with the same signature.
Now let's make the MyButtonOnClickEvent the OnClick event of the button, just add
19 OnClick := MyButtonOnClickEvent;
20
21 //to the code, it should look like this now:
22
23 // create the button
24 MyButton := TButton.Create(Form1);
25 with MyButton do
26 begin
27 // setting the buttons' parent
28 Parent := Form1;
29 // setting the alignment and size
30 Top := 10;
31 Left := 10;
32 Width := 100;
33 // making it respond to the OnClick event
34 onClick := MyButtonOnClickEvent;
35 // showing it
36 Caption := "&It Works!";
37 Visible := True;
38 end;
Now run it, click the "Manage Button" and then click the "It Works!" button! ;)
Now that the button has some "life", all it needs is to "die" to have a full life
cycle. So, let's kill it by calling the standard destructor. Let's change the
"Manage Button" code to create and destroy the MyButton button:
39 if IsAlive = False then
40 begin
41 // create the button
42 MyButton := TButton.Create(Form1);
43 with MyButton do
44 begin
45 // setting the buttons' parent
46 Parent := Form1;
47 // setting the alignment and size
48 Top := 10;
49 Left := 10;
50 Width := 100;
51 // making it respond to the OnClick event
52 onClick := MyButtonOnClickEvent;
53 // showing it
54 Caption := "&It Works!";
55 Visible := True;
56 end;
57 IsAlive := True;
58 end
59 else
60 begin
61 MyButton.Free; // kills the button
62 IsAlive := False;
63 end;
64
65 //The IsAlive flag will tell us if the button is alive or not. To be positive that
66 this has no faults, the IsAlive variable should be initialized, let's fo it on
67 Form1'ow event:
68
69 IsAlive := False;
Now run the application. Click the "Manage Button", click the "It Works!" button,
click the "Manage Button" again.
Simple, isn't it ? :)
|