Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 256
Marus Gradinaru Avatar asked Mar 18 '23 18:03

Marus Gradinaru


1 Answers

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.

like image 193
David Heffernan Avatar answered Apr 26 '23 01:04

David Heffernan