Delphi IDE (89)
Windows with Delphi (280)
About this site
Links to us
How to create a datamodule/ form at runtime from a DFM
1 comments. Current rating: (1 votes). Leave comments and/ or rate it.
I want to read a DataModule.dfm and create an instance of it into my application.
You generally cannot this (easily). A form or datamodule is much more than the resource file. The resource only contains the saved properties of the objects you placed at designtime on the form/DM, it does not contain any of the form/DM classes code, for example constructors won't get executed, and without that the stuff in the resource file is usually fairly useless.
You *can* load the resource, however you have to first call RegisterClass for each and every object class used in the resource, otherwise the load will fail since the stream code will be unable to create the objects contained in the stream. This is not necessary when you create a proper instance of the saved form/Dm class, since the class itself contained the info the streaming code needs.
Look at the following example.
You don't like the formatting? Check out SourceCoder then!
Stream := TFileStream.Create(Filename, fmOpenRead);
Reader := TReader.Create(Stream, 4096);
RegisterClasses([TForm, TEdit]); NewComponent := Reader.ReadRootComponent(nil)
anonymous from Turkey