Forum > LCL

When a component is created at runtime is not given a name how can it be found?

(1/5) > >>

vfclists:
When a component is created at runtime and was not given a name Is one automatically created for it?

I have created program that at runtime can list all the components fouond on a form so I can use the property editor to change the properties, but it seems they are not showing up on the list. Those on the lists have names like DBMemoX, PanelX etc, X being a number.

For instance I want to change a DBMemo, but after going through every memo on the list and changing a property none of them are changed, so I suspect the one created is not listed.

I've got to check the original code and get back to you.

Does Lazarus have  a way of naming components created at runtime?

KodeZwerg:

--- Quote from: vfclists on February 15, 2024, 09:31:55 am ---Does Lazarus have  a way of naming components created at runtime?

--- End quote ---
Sure:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---YourComponent := TYourComponent.Create(Self);try  YourComponent.Parent := Self;  YourComponent.Name := 'YourComponent';  // or via Format() to include numbering etc...

BlueIcaro:
Hello, I'm not sure what are you doing. But with my limited knowledge, this code works for me


--- Code: ---
procedure TForm1.FormCreate(Sender: TObject);
var
  P, P1: TPanel;
  B: TButton;
begin
  P := TPanel.Create(Self);
  P.Caption := 'RunTime';
  P.Parent := Self;

  P1 := TPanel.Create(Self);
  P1.Caption := 'RunTime1';
  P1.Parent := Self;
  P1.Left := 200;
  P1.Top := 0;


  B := TButton.Create(Self);
  B.Parent := Self;
  B.Left := 50;
  B.Top := 100;
  B.Caption := 'Test';
  B.OnClick:=@BClick;

end;

procedure TForm1.BClick(Sender: TObject);
var
  C: Integer;
begin
  C := Self.ComponentCount;
  ShowMessage('Componentes on Form1: '+IntToStr(C)); //Shows 3
end;
   

--- End code ---

Also you can put a name, as KodeZwerg says.
@KodeZwerg you are faster than me, I could not finish my answer  ;)
/BlueIcaro
/BlueIcaro

KodeZwerg:

--- Quote from: vfclists on February 15, 2024, 09:31:55 am ---For instance I want to change a DBMemo, but after going through every memo on the list and changing a property none of them are changed, so I suspect the one created is not listed.
--- End quote ---
I do suggest that you switch to a better generic way of how to dynamic create at runtime components and keep track about them.
When I do create, I most often create them in a list and to quick identify I give each a unique tag, with a unique tag you can write generic methods that just seperate the different tag handling.

Thaddy:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function CreateUniqueName(const BaseName: string; Owner: TComponent): string;var  Counter: Integer;begin  Counter := 1;  Writestr(Result,BaseName,Counter);  while Assigned(Owner.FindComponent(Result)) do  begin    Writestr(Result,BaseName,Counter);    Inc(Counter);  end;end;But IIRC there is already such a function in the LCL? Except I could not find it, but Lazarus does something similar, of course.
Compiles, but not tested since Lazarus trunk will not compile at the moment.

Navigation

[0] Message Index

[#] Next page

Go to full version