Recent

Author Topic: Multiple frames on single form  (Read 1378 times)

hakelm

  • Full Member
  • ***
  • Posts: 137
Multiple frames on single form
« on: January 29, 2022, 11:44:54 am »
I have a frame, Ttpropertiesframe and try to place several different and independent instances on the same form with:
Code: Pascal  [Select][+][-]
  1.   properties1:=Ttpropertiesframe.Create(self);
  2.   properties1.Parent:=self;
  3.  
  4.   properties2:=Ttpropertiesframe.Create(self);
  5.   properties2.Parent:=self;
  6.   properties2.Left:=properties1.Left+properties1.Width;
  7.  

this compiles nicely but when run I get the exception:

Project imgcat raised exception class 'EComponentError' with message:
Duplicate name: A component named "tpropertiesframe" already exists

Why is this so, aren't the different frames just variables of the same type derived from TComponent?
H

MarkMLl

  • Hero Member
  • *****
  • Posts: 4163
Re: Multiple frames on single form
« Reply #1 on: January 29, 2022, 12:05:35 pm »
Why are you trying to do it manually rather than using the IDE?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

hakelm

  • Full Member
  • ***
  • Posts: 137
Re: Multiple frames on single form
« Reply #2 on: January 29, 2022, 12:15:31 pm »
Because I would like to have a little procedure that with a click places around 10 of these frames similar but equipped with different data on a form
H

wp

  • Hero Member
  • *****
  • Posts: 9578
Re: Multiple frames on single form
« Reply #3 on: January 29, 2022, 01:06:56 pm »
Yes. The TAChart demo "charteditor" makes massive use of frames, and I saw the same issue when writing this. The problem is that a frame inserted at runtime does get a Name value equal to the class name by default (I consider this to be a bug...). Without the automatic name generation of the Lazarus designtime mode, therefore, two frames of the same type have a name collision, and this must not happen.

So, jamie's idea to reset the Name to an empty string after creation of each frame is the correct solution.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

hakelm

  • Full Member
  • ***
  • Posts: 137
Re: Multiple frames on single form
« Reply #4 on: January 29, 2022, 01:31:00 pm »
Thanks, explanations are as far as I can understand correct, but I can't set the owner since it is read only.
I made this little workaround that seems to be working, at least are the subcomponents destructors called on destruction:
Code: Pascal  [Select][+][-]
  1.   c:tcomponent;
  2. ................
  3.  
  4.   c:=tcomponent.Create(self);
  5.   properties1:=Ttpropertiesframe.Create(c);
  6.   properties1.Parent:=self;
  7.  
  8.   c:=tcomponent.Create(self);
  9.   properties2:=Ttpropertiesframe.Create(c);
  10.   properties2.Parent:=self;
  11.   properties2.Left:=properties1.Left+properties1.Width;
  12.  
  13.  
H

 

Jorg3000

  • New Member
  • *
  • Posts: 20
Re: Multiple frames on single form
« Reply #5 on: January 29, 2022, 01:59:50 pm »
You don't need to do something with the Owner.
Simply set .Name:=''; after the Create.
That will do it.
So there will be no name collision at the next create.

hakelm

  • Full Member
  • ***
  • Posts: 137
Re: Multiple frames on single form
« Reply #6 on: January 29, 2022, 02:19:57 pm »
Thanks, you are right.
H

 

TinyPortal © 2005-2018