[SOLVED] Runtime component creation lazarus / 21-General ---------------------------------------------------------------------- pcurtis · 2022-02-02 20:08 So, we can create a runtime component as such Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  MyComp: TDBGrid; begin  MyComp:= TDBGrid.Create(Self);  ......  How can I create a runtime component when the only thing known is the classname? So, for example, I could create a TLabel, then a TScrollbox and so on. How to define MyComp? and how to create the component from a string holding the classname? All components are visual. ---------------------------------------------------------------------- pcurtis · 2022-02-03 01:16 No. Pseudo code ... var   MyComp: array of TUniversalBaseComponent;   MyComp[0] := 'TLabel'.Create(nil);   MyComp[1] := 'TMemo'.Create(nil);   MyComp[2] := 'TDBGrid'.Create(nil);   .....   .....   MyComp[0].Free;   MyComp[1].Free;   MyComp[2].Free; ---------------------------------------------------------------------- pcurtis · 2022-02-03 02:16 Close but QuoteVar   J:TClass; .....   J := Tmemo;  <-- --- in script land.  If J.ClassName = StringFromScript('Tmemo') Then myComponentArray[?] := J.Create; How can I tell "J" to be a TMemo from a string that holds 'TMemo' or a TScrollbox if the string contains 'TScrollbox'? Or do I have to do a If .. Then for each component type? There are a lot of them and there new ones all the time. ---------------------------------------------------------------------- pcurtis · 2022-02-03 02:37 Maybe this makes it a little clearer Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  ComponentNames: Array[0..10] of String;  Components: Array[0..10] of TClass;   ComponentNames[0]:= 'TLabel';  ComponentNames[1]:= 'TDBGrid';  ComponentNames[2]:= 'TSomeComponent';   for i:= 0 to 2 do    Components[i]:= ComponentNames[i].Create; <-- Here Something like StringToClass(ComponentNames).Create ---------------------------------------------------------------------- Handoko · 2022-02-03 03:25 Write a new function and use case statement: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private    function CreateClassFromName(const S: string): TControl;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } function TForm1.CreateClassFromName(const S: string): TControl;begin  case S.ToUpper of    'TLABEL':  Result := TLabel.Create(Self);    'TBUTTON': Result := TButton.Create(Self);    else      Result := nil;  end;  if Assigned(Result) then    Result.Parent := Self;  Assert(Result <> nil, 'Wrong class name for CreateClassFromName.');end; procedure TForm1.Button1Click(Sender: TObject);begin  with CreateClassFromName('TButton') do  begin    Left  := 120;    Top     := 50;    Caption := 'Button';  end;  with CreateClassFromName('TLabel') do  begin    Left    := 60;    Top     := 80;    Caption := 'A new button has been created.';  end;  Button1.Enabled := False;end; end. ~edit~ Because the result is TControl, I think it will be better to name it CreateControlFromName. ---------------------------------------------------------------------- pcurtis · 2022-02-03 06:14 Thanks Hanoko, but   case S.ToUpper of     'TLABEL':  Result := TLabel.Create(Self);     'TBUTTON': Result := TButton.Create(Self);     else       Result := nil;   end; Is what I am trying to avoid. The types of components is large, and is constantly growing. The list of cases would become unmanageable. There must be a way. When the IDE reads a *.lfm file component names are just strings. Problem is I don't see how. ---------------------------------------------------------------------- PascalDragon · 2022-02-03 07:57 Quote from: pcurtis on February 03, 2022, 07:14:44 amWhen the IDE reads a *.lfm file component names are just strings. Problem is I don't see how. As long as your components are registered correctly so that they could also be handled with a form you can retrieve them using Classes.FindClass (which will throw an exception if the class isn't found) or Classes.GetClass (which doesn't throw an exception but returns Nil). Please note that this will only work for classes that descend from TPersistent (any TComponent descendant and thus any LCL control does that). If you need to register a descendant of TPersistent that isn't yet registered you need to use Classes.RegisterClass. ---------------------------------------------------------------------- avk · 2022-02-03 10:42 It seems that, if desired, a similar system of registration can be done only for controls. ---------------------------------------------------------------------- pcurtis · 2022-02-04 09:28 Thanks all.