Recent

Author Topic: Creating "TEdit" component dynamically  (Read 3103 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Creating "TEdit" component dynamically
« on: May 05, 2021, 01:48:27 pm »
I'm trying to create "TEdit" component dynamically, but I see no result.


Code: Pascal  [Select][+][-]
  1.  
  2. var
  3.   Form1: TForm1;
  4.   e1:tedit;
  5.  
  6. procedure TForm1.FormCreate(Sender: TObject);
  7. begin
  8.  
  9.   e1:=tedit.Create(self);
  10.  
  11.   e1.Height:=28;
  12.   e1.Width:=127;
  13.   e1.Left:=95;
  14.   e1.Top:=56;
  15.  
  16.   e1.Text:='Hello World!';
  17.  
  18. end;
  19.  
  20. procedure TForm1.FormDestroy(Sender: TObject);
  21. begin
  22.   e1.Free;
  23. end;
  24.  
  25.  
La chose par la chose est rappelé.

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: Creating "TEdit" component dynamically
« Reply #1 on: May 05, 2021, 01:51:30 pm »
You didn't set parent:
Code: Pascal  [Select][+][-]
  1. e1.Parent:=Self;

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Creating "TEdit" component dynamically
« Reply #2 on: May 05, 2021, 02:11:33 pm »
It works!
Thanks!
La chose par la chose est rappelé.

alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: Creating "TEdit" component dynamically
« Reply #3 on: May 05, 2021, 02:19:50 pm »
One more thing, since you gave an Owner to the TEdit by:

Code: Pascal  [Select][+][-]
  1. e1:=tedit.Create(self);

You shall not free it at FormDestroy(). It will be freed by the Owner at the right time.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Creating "TEdit" component dynamically
« Reply #4 on: May 05, 2021, 02:23:01 pm »
I got it! thanks!
La chose par la chose est rappelé.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Creating "TEdit" component dynamically
« Reply #5 on: May 05, 2021, 02:24:33 pm »
By the way, is there a problem if I said:

Code: Pascal  [Select][+][-]
  1.  
  2. e1.create(self); // rather than     e1:=tedit.Create(self);
  3.  
  4.  
La chose par la chose est rappelé.

alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: Creating "TEdit" component dynamically
« Reply #6 on: May 05, 2021, 02:39:15 pm »
By the way, is there a problem if I said:

Code: Pascal  [Select][+][-]
  1. e1.create(self); // rather than     e1:=tedit.Create(self);
  2.  
It is not the same. The constructor is part of the class(TEdit), not the instance (e1). e1 is just a pointer internally.

"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Creating "TEdit" component dynamically
« Reply #7 on: May 05, 2021, 02:55:40 pm »
By the way, is there a problem if I said:
Code: Pascal  [Select][+][-]
  1. e1.create(self); // rather than     e1:=tedit.Create(self);

When you do that what you're doing is basically just calling a method of the instance, rather than invoking the constructor of the class. Two very different concepts.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Creating "TEdit" component dynamically
« Reply #8 on: May 05, 2021, 02:59:06 pm »
I see now. I think the different between the two concepts can be used in advanced uses of components.
La chose par la chose est rappelé.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Creating "TEdit" component dynamically
« Reply #9 on: May 05, 2021, 03:08:25 pm »
Beyond the instance creation "magic", the constructor is sometimes used to initialize some fields, set some default properties, etc. If you're going to call it as a method to "reset" it to the default state it's usually better to include all those initializations to their own method (SetDefaults() or whatever) and call that instead.

That way it's less confusing and there is no doubt about whether you're constructing the object or just "resetting" it.

IMHO, of course :-\
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Creating "TEdit" component dynamically
« Reply #10 on: May 05, 2021, 03:16:50 pm »
It seems Lazarus freePascal is strong language in event driven programming, it cares in details, small things of OOP and Visual Programming and different techniques.
La chose par la chose est rappelé.

alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: Creating "TEdit" component dynamically
« Reply #11 on: May 05, 2021, 06:38:00 pm »
I see now. I think the different between the two concepts can be used in advanced uses of components.

It is quite simple actually, you may think of objects as of instances of a certain class, and for that class as of the only instance of some hypothetical meta-class.
Therefore, all that is valid for the object can be applied to the class - the class also have its methods and data fields (static ones).
So, the constructors are actually methods of the class - they construct an instance of the object and return them back as a reference:
Code: Pascal  [Select][+][-]
  1.   e1 := TEdit.Create(Self);
  2.  
Means "call the method Create on (sole) instance of Meta-TEdit named TEdit and assign the returned reference to the e1".  As lucamar said, the:
Code: Pascal  [Select][+][-]
  1.   e1.Create(Self);
  2.  
means "call the method Create on instance of TEdit named e1", but e1 doesn't exist yet and also does not have such a method.

It's simple.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Creating "TEdit" component dynamically
« Reply #12 on: May 05, 2021, 06:42:52 pm »
It has logical sense @y.ivanov. I understand.
La chose par la chose est rappelé.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: Creating "TEdit" component dynamically
« Reply #13 on: May 06, 2021, 12:30:13 am »
One more thing, since you gave an Owner to the TEdit by:

Code: Pascal  [Select][+][-]
  1. e1:=tedit.Create(self);

You shall not free it at FormDestroy(). It will be freed by the Owner at the right time.

While it is true that an Owner will free the component automatically, it should be noted that it is perfectly safe to manually Free() a component that has an Owner assigned.  The component will simply remove itself from the Owner's internal list so that the Owner will not try to free the component again when the Owner is freed.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: Creating "TEdit" component dynamically
« Reply #14 on: May 06, 2021, 02:43:28 am »
*snip*
While it is true that an Owner will free the component automatically, it should be noted that it is perfectly safe to manually Free() a component that has an Owner assigned.  The component will simply remove itself from the Owner's internal list so that the Owner will not try to free the component again when the Owner is freed.

Formally true. My wording must be read "You may not free it" instead of "You shall not free it" then.

On the other hand, giving a non-nil owner to a component on its constructor means that we're transferring the life-cycle management to that owner. If we intend to manage the component life-cycle by ourselves, it is much better to create it without owner. Mixing the two approaches will result in UAF sooner or later.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018