Recent

Author Topic: Runtime TLabel not shown as child  (Read 2115 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Runtime TLabel not shown as child
« on: August 31, 2017, 06:13:37 pm »
hi All,
i am creating a label during runtime as per the below but i cannot see it listed as child of self although i have created it with self as parent.
please advise what i am missing.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: integer;
  4.  
  5. begin
  6.   l:= TLabel.Create(self);
  7.   l.Caption:= 'xxxxxxxxxxxxxxxxxxx';
  8.   l.Visible:= true;
  9.   l.Parent:= self;
  10.  
  11.   for i:= 0 to self.ControlCount - 1 do begin
  12.     Memo1.Append(self.Controls[i].Name);
  13.   end;
  14. end;  

thank you

Lazarus 1.8RC4 64b
Lazarus 2.0.2 64b on Debian LXDE 10

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Runtime TLabel not shown as child
« Reply #1 on: August 31, 2017, 06:33:52 pm »
Actually it is there, you just can't see it because its name is nothing/empty.

Try to add:
Code: Pascal  [Select][+][-]
  1.   l.Name := 'Hello';

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Runtime TLabel not shown as child
« Reply #2 on: August 31, 2017, 06:44:11 pm »
thank you Handoko.

would you know who is actually doing the magic when you create the same label from the IDE?
i thought there is some inheritance like some other attributes, example autosize in the constructor for the runtime label also.
Lazarus 2.0.2 64b on Debian LXDE 10

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Runtime TLabel not shown as child
« Reply #3 on: August 31, 2017, 07:16:59 pm »
Newly created components (usually) do not have name, but if you use the form editor it will automatically generate a name for them.

One great thing about open source project is you can study the code. I looked into the IDE and I found the code that is responsible for the name creation. It is in CustomFormEditor.pas line #2263:

Code: Pascal  [Select][+][-]
  1. function TCustomFormEditor.CreateUniqueComponentName(const AClassName: string;
  2.   OwnerComponent: TComponent): string;
  3. var
  4.   i, j: integer;
  5. begin
  6.   Result:=AClassName;
  7.   if (OwnerComponent=nil) or (Result='') then exit;
  8.   i:=1;
  9.   while true do begin
  10.     j:=OwnerComponent.ComponentCount-1;
  11.     Result:=ClassNameToComponentName(AClassName);
  12.     if Result[length(Result)] in ['0'..'9'] then
  13.       Result:=Result+'_';
  14.     Result:=Result+IntToStr(i);
  15.     while (j>=0)
  16.     and (CompareText(Result,OwnerComponent.Components[j].Name)<>0) do
  17.       dec(j);
  18.     if j<0 then exit;
  19.     inc(i);
  20.   end;
  21. end;

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Runtime TLabel not shown as child
« Reply #4 on: August 31, 2017, 09:15:33 pm »
Code: Pascal  [Select][+][-]
  1. Procedure TForm1.FormClick(Sender: TObject);
  2.   Var
  3.    lab: TLabel;
  4.  Begin
  5.   lab        := TLabel.Create(Self);
  6.   lab.Caption:= 'Hello World...';
  7.   lab.Parent := Self;
  8.  End;
  9.  
  10. Procedure TForm1.Button1Click(Sender: TObject);
  11.   Var
  12.    i, iCounter: Integer;
  13.  Begin
  14.   iCounter:= 1;
  15.  
  16.   For i:= 0 To ControlCount-1
  17.   Do
  18.    Begin
  19.     If Controls[i] Is TLabel
  20.     Then
  21.      Begin
  22.       If TLabel(Controls[i]).Name = ''
  23.       Then
  24.        Begin
  25.         Memo1.Lines.Add('Label'+IntToStr(iCounter));
  26.         Inc(iCounter);
  27.        End
  28.       Else Memo1.Lines.Add(TLabel(Controls[i]).Name);
  29.      End;
  30.    End;
  31.  End;  
  32.  

...just kidding....  :D :D :D
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

 

TinyPortal © 2005-2018