RTTIFormNameCaption (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This code exemplifies the GetType, GetProperty, and GetValue functions inside the RTTI unit. The application will gather information about the names and captions of the forms that compose the entire application.

Code

procedure Tfrm1.btnGetClick(Sender: TObject);
begin
  { First, fill a TRttiType variable with type information 
    from the TForm object that is of interest. }
  RT := RC.GetType(TypeInfo(TForm));

  { Then fill in a ListBox with the 'Name' and 'Caption' properties of all the
    forms included in this example, namely 'frm1' and 'frm2'. These two forms
    are the Main form and the AboutBox form. }
  with ListBox1 do
  begin
    Items.BeginUpdate;
    Items.Add('Name: ' + RT.GetProperty('Name').GetValue(frm1).ToString +
              '; Caption: ' + RT.GetProperty('Caption').GetValue(frm1).ToString);
    Items.Add('Name: ' + RT.GetProperty('Name').GetValue(frm2).ToString +
              '; Caption: ' + RT.GetProperty('Caption').GetValue(frm2).ToString);
    Items.EndUpdate;
  end;
end;

Uses

Personal tools
Assistance