RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TObject::ClassType Method

Returns the class reference for the object's class.

Pascal
function ClassType: TClass;
C++
__fastcall TClass ClassType();

Note: System::TObject::ClassType dynamically determines the type of an object and returns its class reference, or metaclass.
Avoid using System::TObject::ClassType in application code.
Note: In Delphi code, use the is or as operators instead of System::TObject::ClassType.
Note: In C++ code, use a dynamic cast or the System::TObject::InheritsFrom method instead of System::TObject::ClassType.
 

C++ Examples: 

 

/*
This example shows how to obtain the ancestry of a component
using the ClassType and ClassParent properties. It uses a
button and a list box on a form. 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.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TClass ClassRef;
  ListBox1->Clear();
  ClassRef = Sender->ClassType();
  while (ClassRef != NULL)
  {
    ListBox1->Items->Add(ClassName(ClassRef));
    ClassRef = ClassParent(ClassRef);
  };
}
/*
The list box contains the following strings after the user clicks the button:
    TButton
    TButtonControl
    TWinControl or TWinControl
    TControl
    TComponent
    TPersistent
    TObject 
*/
/*
For the following example, add a button, a status bar, and a
list box to the form. Set the SimplePanel property of the
status bar to true, using the object inspector. Also, populate
the OnMouseUp event handler for the list box.  The following
code fills a list box with the names of all components on the
form when the user clicks the button. References to the
components themselves are inserted along with the names. The
components are all inserted at the front of the list, so that
the last component added to the form is the first component
in the list.  When the user right-clicks the name of an
object in the list, the component’s coordinates are displayed
on the status bar. Note that because we are using the right-
click, the item need not be selected.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  for (int i = 0; i < ComponentCount; i++)
    ListBox1->Items->InsertObject(0,
                                  Components[i]->Name, 
                                  (TObject *)Components[i]);
}

void __fastcall TForm1::ListBox1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
  if (Button == mbRight)
  {
    TClass ClassRef;
    int Index = ListBox1->ItemAtPos(Point(X,Y), true);
    // only components that are controls have a position
    // make sure the component is a control
    for (ClassRef = ListBox1->Items->Objects[Index]->ClassType();
         ClassRef != NULL;
         ClassRef = ClassRef->ClassParent())
      if (String(ClassRef->ClassName()) == "TControl")
      {
        TControl *TheObject = (TControl *)ListBox1->Items->Objects[Index];
        StatusBar1->SimpleText =
          TheObject->Name + " is at (" +
          IntToStr(TheObject->Left) + ", " +
          IntToStr(TheObject->Top) + ")";
         break;
      }
    if (ClassRef == NULL) // if it wasn't a control
      MessageBeep(0);
  }
}
/*
To run this example, add the example code to a new project.
The example code dynamically creates a TCoolbar and three
TCoolBand objects populated with a windowed control in each
TCoolBand.
*/
void AddBands(TCoolBar *CoolBar, TList *Objects)
{
  TControl *pCurrent;
  for (int i = 0; i < Objects->Count; i++)
  {
    pCurrent = (TControl *)Objects->Items[i];
    // only add windowed controls to the coolbar
    if (dynamic_cast<TWinControl *>(pCurrent) != NULL)
    {
      // Placing the control onto the CoolBar
      // causes the TCoolBar object to create a TCoolBand 
      // and place the control within the band.
      pCurrent->Parent = CoolBar;   // This statement increments CoolBar->Bands->Count.
      // Get the reference of the TCoolBand just created and assign text.
      String S = pCurrent->ClassName(pCurrent->ClassType());
      CoolBar->Bands->Items[CoolBar->Bands->Count - 1]->Text = S;
    }
  }
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  TCoolBar *CoolBar = new TCoolBar(this);
  TList *List;
  CoolBar->Parent = this;
  CoolBar->Align = alTop;
  List = new TList();
  try
  {
    TControl *Control;
    Control = new TButton(CoolBar);
    List->Add(Control);
    Control = new TCheckBox(CoolBar);
    List->Add(Control);
    Control = new TEdit(CoolBar);
    List->Add(Control);
    AddBands(CoolBar, List);
  }
  catch (...)
  {
    ShowMessage("Some objects could not be added to Coolband");
  }
  delete List;
}

 

Delphi Examples: 

{
This example shows how to obtain the ancestry of a component
using the ClassType and ClassParent properties. It uses a
button and a list box on a form. 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 or TWinControl
    TControl
    TComponent
    TPersistent
    TObject 
}

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!