When, the code that create the components on the form and the code that set their properties, is called?

952 views Asked by At

If I put a component on the form I don't see any code like MyComp:=TMyComponent.Create in unit code. I think the component is created automatically, but when ? And the same happends with the properties that I configured after I put the component on the form. When they are applied at runtime ?

2

There are 2 answers

0
David Heffernan On BEST ANSWER

The properties for a form and all the design time components that live on it are streamed in by the framework during the construction of the form. That process is triggered from the form's constructor, in TCustomForm.Create. The pertinent code in there looks like this:

Include(FFormState, fsCreating);
try
  if not InitInheritedComponent(Self, TForm) then
    raise EResNotFound.CreateFmt(SResNotFound, [ClassName]);
finally
  Exclude(FFormState, fsCreating);
end;

The key is the call to InitInheritedComponent. That is a function defined in the Classes unit that does the heavy lifting. In a very broad overview it does the following:

  1. Finds the name of the form class and looks for an RT_RCDATA resource of that name. That resource is the .dfm file.
  2. Once the .dfm resource has been located it is parsed.
  3. The .dfm parsing handles the assignment of properties that you set at design-time in the Object Inspector. For instance the parsing might encounter a line like this: Caption = 'My main form' and it turns that into an assignment of the string 'My main form' to the form's property Caption.
  4. The .dfm file is hierarchical. It contains the properties for the various components and controls that are defined at design-time.
  5. As well as setting the properties of the form's design-time components, the .dfm streaming process also instantiates those components.

In order for all of this to work, the streaming framework relies on RTTI. It knows nothing at all at compile time of your classes and components. Hence the need for RTTI. The streaming framework uses the old style RTTI and in fact that is the reason for the existence of old style RTTI. If ever you wonder about why old style RTTI is the way it is, try to view it from the perspective of having been designed to support streaming.

0
Dalija Prasnikar On

Information about controls and components as well as their properties that you edit while designing in IDE will be stored in your form .dfm file. Creation of that form in run-time will trigger the process of automatic loading of that .dfm file and all controls and components will be initialized at that time.

This is rather simple explanation of what exactly happens, you can start debugger at form creation line and follow what is happening there, but it is quite longish process and you can easily get lost if you are still learning.

You can find form creation code that Delphi automatically writes in .dpr file of your project.

  Application.CreateForm(TForm1, Form1);