Embarcadero Delphi Using RTTI in Delphi Basic Class Information

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

This example shows how to obtain the ancestry of a component using the ClassType and ClassParent properties. It uses a button Button1: TButton and a list box ListBox1: TListBox on a form TForm1.

When the user clicks the button, the name of the button’s class and the names of its parent classes are added to the list box.

procedure TForm1.Button1Click(Sender: TObject) ;
var
  ClassRef: TClass;
begin
   ListBox1.Clear;
   ClassRef := Sender.ClassType;
   while ClassRef <> nil do
   begin
     ListBox1.Items.Add(ClassRef.ClassName) ;
     ClassRef := ClassRef.ClassParent;
   end;
end;

The list box contains the following strings after the user clicks the button:

  • TButton
  • TButtonControl
  • TWinControl
  • TControl
  • TComponent
  • TPersistent
  • TObject


Got any Embarcadero Delphi Question?