[Lazarus] How to load the Form from LFM-file at run time?

Александр С. sss0991 at gmail.com
Thu Feb 21 12:04:43 CET 2013


>You don't need the units, you need to register all needed classes
lfm only adds components or sets properties. So I can get only a class
names as strings. TClass is required for RegisterComponents. I do not
know how to convert a string to TClass.

> you need to … provide a OnFindClass hook, as shown in the example.
It works for the component, but will it work for TForm? I tried this,
I made a few changes in the example, I did not succeed:

Index: mainunit.pas
===================================================================
--- mainunit.pas	(revision 40336)
+++ mainunit.pas	(working copy)
@@ -53,7 +53,7 @@
 begin
   AStream:=TMemoryStream.Create;
   try
-    WriteComponentAsBinaryToStream(AStream,AGroupBox);
+    WriteComponentAsBinaryToStream(AStream,CompStreamDemoForm);
     SaveStreamAsString(AStream);
   finally
     AStream.Free;
@@ -72,9 +72,7 @@
     ReadStreamFromString(AStream);
     NewComponent:=nil;
     ReadComponentFromBinaryStream(AStream,NewComponent,
-                                  @OnFindClass,DestinationGroupBox);
-    if NewComponent is TControl then
-      TControl(NewComponent).Parent:=DestinationGroupBox;
+                                  @OnFindClass);
   finally
     AStream.Free;
   end;
@@ -158,8 +156,8 @@
   DestinationGroupBox.
 }
 begin
-  while DestinationGroupBox.ComponentCount>0 do
-    DestinationGroupBox.Components[0].Free;
+  while CompStreamDemoForm.ComponentCount>0 do
+    CompStreamDemoForm.Components[0].Free;
 end;

 procedure TCompStreamDemoForm.OnFindClass(Reader: TReader;
@@ -167,8 +165,8 @@
 begin
   if CompareText(AClassName,'TGroupBox')=0 then
     ComponentClass:=TGroupBox
-  else if CompareText(AClassName,'TCheckBox')=0 then
-    ComponentClass:=TCheckBox;
+  else if CompareText(AClassName,'TCompStreamDemoForm')=0 then
+    ComponentClass:=TCompStreamDemoForm;
 end;

 end.




More information about the Lazarus mailing list