• Revista PROGRAMAR: Já está disponível a edição #53 da revista programar. Faz já o download aqui!

M6

Criar objectos visuais em run-time.

1 mensagem neste tópico

Antes de mais quero pedir desculpa por este artigo estar em inglês, prometo (palavra de escuteiro que é coisa que nunca fui :P) que o traduzo para a língua de Camões quando tiver tempo, mas já escrevi isto há uns anos num fórum de Delphi.

Este artigo surgiu no seguimento do tópico Como Criar uma Form dentro de outra!!


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.

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:

1. 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.

2. 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.

3. 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:

  // create the button
  MyButton := TButton.Create(Form1);
  With MyButton Do
  Begin
    // setting the buttons' parent
    Parent := Form1;  
    // setting the alignment and size
    Top := 50;  
    Left := 10;
    Width := 100;
    // showing it
    Caption := "&It Works!";
    Visible := True;
  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:

Procedure TForm1.MyButtonOnClickEvent(Sender: TObject);
Begin
   ShowMessage("Well, it really works!");
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

OnClick := MyButtonOnClickEvent;

to the code, it should look like this now:

  // create the button
  MyButton := TButton.Create(Form1);
  With MyButton Do
  Begin
    // setting the buttons' parent
    Parent := Form1;  
    // setting the alignment and size
    Top := 10;  
    Left := 10;
    Width := 100;
    // making it respond to the OnClick event
    onClick := MyButtonOnClickEvent;
    // showing it
    Caption := "&It Works!";
    Visible := True;
  End;

Now run it, click the "Manage Button" and then click the "It Works!" button! :P

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:

If IsAlive = False Then
Begin
   // create the button
   MyButton := TButton.Create(Form1);
   With MyButton Do
   Begin
    // setting the buttons' parent
    Parent := Form1;  
    // setting the alignment and size
    Top := 10;  
    Left := 10;
    Width := 100;
    // making it respond to the OnClick event
    onClick := MyButtonOnClickEvent;
    // showing it
    Caption := "&It Works!";
    Visible := True;
   End;
   IsAlive := True;
End
Else
Begin
    MyButton.Free;  // kills the button
    IsAlive := False;
End;

The IsAlive flag will tell us if the button is alive or not.

To be positive that this has no faults, the IsAlive variable should be initialized, let's fo it on Form1's OnShow event:

  IsAlive := False;

Now run the application. Click the "Manage Button", click the "It Works!" button, click the "Manage Button" again.

Simple, isn't it ? :D

Hope this will be helpful.

0

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

Crie uma conta ou ligue-se para comentar

Só membros podem comentar

Criar nova conta

Registe para ter uma conta na nossa comunidade. É fácil!


Registar nova conta

Entra

Já tem conta? Inicie sessão aqui.


Entrar Agora