4.03.2013 Clone your form into many !!!

This is a fun code example to try. When you open up a new Application project, you get a form. To have fun, we can create hundreds of "copies" of this form! On runtime. Here's how...

Concept 

Do you know what class is? Class is a collection of students, right? No, in the programing world? If you know what class is, then understanding this example will be easier for you. If you don't, then I can simply say that a class is a variable which has its own variables and functions. You can ask a class to do something and it will do it. A class has its own life - like a person. It can answer to your calls and remember things.

For example, when you create a new Application project, you see a TForm1 class declaration at the top. And a variable declaration of Form1: TForm1. Here, Form1 is a variable which holds the TForm1 class. It means that we have made a copy of the "TForm1" class as "Form1". If you say to the Form1 that hey buddy, please close yourself (Form1.close) then it closes itself. Thus you can command a variable! And this is class.

The beauty of class is that you can make as many "copies" of them you like. For example, you can create a class named "human" and you have two variables like Name and Age. So you can create new a new "human"s and name it! ...and age it!

Human1.Name := 'John';

Human1.Age := 25;



Human2.Name := 'Kate';

Human2.Age := 21;

And you can create as many "human"s as you like!

(I found a good introduction of classes in the php manual, but can't find it now. It had a shopping cart class example which was very easy to understand.)

Quick Tutorial

Create an Application project. Project->New Project->Application->OK.


Click the TButton from the toolbar and drag an area in the form to create a button. Double click it. Then write:

var
  frm: TForm1;

begin
  frm:=TForm1.Create(Form1);
  frm.Show;

end;

Run it!


Press F9. Click the button and a new form will appear on top each time you click the button. The problem is that new forms appear at one position. So it is difficult to understand whether a new form has appeared. Close the forms. Do the following...

With the form selected, go to Object Inspector and set Position to poDefaultPosOnly. This will position every new form  in cascading style (one lower to another).


Now again press F9. Now you will see pure fun! Forms and forms everywhere! Keep copying until your heart desires.


Sample code zip

Download sample source zip here: http://db.tt/4DKsykdh
or here: http://bit.ly/179TjPq
Size: 670 KB

6 comments:

Anonymous said...
This comment has been removed by the author.
Anonymous said...

hei, how to clone Tbutton? thanks before. sample please

Adnan Shameem said...

A, Zia Urrahman,

Cloning a component is easy. It is similar to the above code. Just remember to set the parent property.

Create a new Application Project. Drop a TButton. Double click it then enter:


procedure TForm1.Button1Click(Sender: TObject);
var
btn: TButton;
begin
btn := TButton.Create(Button1);
btn.parent := Form1;

// set properties for new button
btn.top := (random(300));
btn.Left := (random(300));
btn.Caption := inttostr(random(9999));
btn.OnClick := @Button1Click;
end;


You can set properties to the new button one by one.

Then run it and keep clicking the button to get a button everytime.
Enjoy!

Adnan Shameem said...

I forgot to add the reference topic link: http://forum.lazarus.freepascal.org/index.php?topic=8912.0

Carmelo Privitera said...

hi and compliment for your tutorials!!
i have try to create an desktop note application and i want to duplicate form for create more desktop note. I have tried to apply your method but does not work. Have u any advice for me?
regards
Carme

Adnan Shameem said...

@Carme
It is a great idea!

The above code should work... for the cloning part at least. for saving the notes I would recommend using a unique id number for each new note created with random(999999). You could save them in an INI file with the section name with the note's ID. For example:

[note835739]
note="This is a note of mine which I need to remember"
posx=123
posy=678

[note274850]
note="This is another note of mine which I will forget"
posx=136
posy=456

So this way you will have the note text plus the position of the forms saved in an INI file.

When the first form is loaded, you can then iterate between the sections and create a form for each note.

Good luck!

Ref:
INI files: http://wiki.freepascal.org/Using_INI_Files
Random number: http://lazplanet.blogspot.com/2013/05/how-to-generate-random-number.html

 
Copyright 2013 LazPlanet
Carbon 12 Blogger template by Blogger Bits. Supported by Bloggermint