How can I add custom properties to my new component in an FMX application? What is the syntax for the Component property? How can I add a visual property to my own custom FireMonkey component that can be read or written by the Object Inspector? How can I create a property that can be edited via the Object Inspector in the C++ IDE at design time and via coding at run time? What is the __published
: section in Classes (Type Objects) in C++ Builder? What is __property
? Where can I use __property
? Let’s answer these questions.
One of the most powerful features of the C++ Builder is its components that can be used with or without visuals. Components make programming easy, you can do many operations without knowing low coding or some specific features needed. Every component that you drag into your project’s forms is an object of the base component class. For example, the TEdit is a Class Type, a Component and Edit1 is an Object from the TEdit Component.
Table of Contents
What is a Component in C++ Builder?
In C++ Builder and Delphi, every object (i.e. Edit1) inherits from TObject class (i.e. TEdit1). Objects that can appear in the Form Designer inherit from TPersistent or TComponent Controls, which appear to the user at run time, inherit from TControl. There are two types of controls, graphic controls, which inherit from TGraphicControl, and windowed controls, which inherit from TWinControl. A control like TCheckBox inherits all the functionality of TObject, TPersistent, TComponent, TControl, and TWinControl, and adds specialized capabilities of its own.
A Component (capital C) in C++ Builder and Delphi specifies the base class for all components. Components can be added to the Tool palette and manipulated at design time. Components can own other components.
The TComponent branch contains classes that descend from TComponent but not TControl. Objects in this branch are components that you can manipulate on forms at design time but which do not appear to the user at run time. They are persistent objects that can do the following:
- Appear on the Tool palette and be changed on the form.
- Own and manage other components.
- Load and save themselves.
Several methods introduced by TComponent dictate how components act during design time and what information gets saved with the component. Streaming (the saving and loading of form files, which store information about the property values of objects on a form) is introduced in this branch. Properties are persistent if they are published – and published properties are automatically streamed.
The TComponent branch also introduces the concept of ownership, which is propagated throughout the component library. Two properties support ownership: Owner and Components. Every component has an Owner property that references another component as its owner. A component may own other components. In this case, all owned components are referenced in Components property of the component.
The constructor for every component takes a parameter that specifies the owner of the new component. If the passed-in owner exists, the new component is added to Components list of that owner. Aside from using the Components list to reference owned components, this property also provides for the automatic destruction of owned components. As long as the component has an owner, it will be destroyed when the owner is destroyed. For example, since TForm is a descendant of TComponent, all components owned by a form are destroyed and their memory freed when the form is destroyed. (Assuming, of course, that the components have properly designed destructors that clean them up correctly.)
If a property type is a TComponent or a descendant, the streaming system creates an instance of that type when reading it in. If a property type is TPersistent but not TComponent, the streaming system uses the existing instance available through the property and reads values for the properties of that instance.
Components that do not need a visual interface can be derived directly from TComponent. To make a tool such as a TTimer device, you can derive from TComponent. This type of component resides on the Tool Palette but performs internal functions that are accessed through code rather than appearing in the user interface at run time.
How to set Properties of a Component in C++ Builder?
Adding new properties to components is easy in C++ Builder. In modern C++, classes have private:, public: and protected: sections, and in addition to these C++ Builder has __published: section that allows user to add visual properties that can be read and write via Object Inspector panel on design or as a property in run time.
One of the most powerful features of the C++ Builder is its own Components that can be used with or without visuals. Components make programming easy, you can do many operations easily without knowing low coding or some specific features needed. Every component that you drag into is an object of that component class. For example, TEdit is a Class Type, a Component and Edit1 is an Object from the TEdit Component.
RAD Studio runs in 32bits that means all components should be 32bits. You can compile 64bits VCL Component but you can NOT install a 64bits VCL Component on to 32bits RAD Studio. On the other side, you can create 32bits VCL component that runs with with your 64bbits applications. That means while the RADS IDE is 32bits you can create 64bits applications with your 64bits components. Now let’s see how we can do this.
What is a Component in C++ Builder ?
In C++ Builder and Delphi, every object (i.e. Edit1) inherits from TObject class (i.e. TEdit1). Objects that can appear in the Form Designer inherit from TPersistent or TComponent Controls, which appear to the user at run time, inherit from TControl. There are two types of controls, graphic controls, which inherit from TGraphicControl, and windowed controls, which inherit from TWinControl. A control like TCheckBox inherits all the functionality of TObject, TPersistent, TComponent, TControl, and TWinControl, and adds specialized capabilities of its own.
A Component in C++ Builder and Delphi specifies the base class for all components. Components can be added to the Tool palette and manipulated at design time. Components can own other components. Com
The TComponent branch contains classes that descend from TComponent but not TControl. Objects in this branch are components that you can manipulate on forms at design time but which do not appear to the user at run time. They are persistent objects that can do the following:
- Appear on the Tool palette and be changed on the form.
- Own and manage other components.
- Load and save themselves.
Several methods introduced by TComponent dictate how components act during design time and what information gets saved with the component. Streaming (the saving and loading of form files, which store information about the property values of objects on a form) is introduced in this branch. Properties are persistent if they are published and published properties are automatically streamed.
The TComponent branch also introduces the concept of ownership, which is propagated throughout the component library. Two properties support ownership: Owner and Components. Every component has an Owner property that references another component as its owner. A component may own other components. In this case, all owned components are referenced in Components property of the component.
The constructor for every component takes a parameter that specifies the owner of the new component. If the passed-in owner exists, the new component is added to Components list of that owner. Aside from using the Components list to reference owned components, this property also provides for the automatic destruction of owned components. As long as the component has an owner, it will be destroyed when the owner is destroyed. For example, since TForm is a descendant of TComponent, all components owned by a form are destroyed and their memory freed when the form is destroyed. (Assuming, of course, that the components have properly designed destructors that clean them up correctly.)
If a property type is a TComponent or a descendant, the streaming system creates an instance of that type when reading it in. If a property type is TPersistent but not TComponent, the streaming system uses the existing instance available through the property and reads values for the properties of that instance.
Components that do not need a visual interface can be derived directly from TComponent. To make a tool such as a TTimer device, you can derive from TComponent. This type of component resides on the Tool Palette but performs internal functions that are accessed through code rather than appearing in the user interface at run time.
How to define Properties of a FMX Component in C++ Builder?
Adding new properties to components are really easy in C++ Builder. In modern C++ Classes has private:, public: and protected: sections, and in addition to these C++ Builder has __published: section that allows user to add visual properties that can be read and write via Object Inspector panel on design or as a property in run time.
Go to header of your component unit and list your own properties under __published: section.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// --------------------------------------------------------------------------- #ifndef MyCompoUnit1H #define MyCompoUnit1H // --------------------------------------------------------------------------- #include <System.SysUtils.hpp> #include <System.Classes.hpp> #include <FMX.Controls.hpp> #include <FMX.Objects.hpp> #include <FMX.Types.hpp> #include <FMX.Colors.hpp> // --------------------------------------------------------------------------- class PACKAGE TMyCompo : public Fmx::Controls::TControl { typedef Fmx::Controls::TControl inherited; private: protected: public: __fastcall TMyCompo(TComponent* Owner); __fastcall ~TMyCompo(); __published: // Add __property ... here }; // --------------------------------------------------------------------------- #endif |
Syntax for a __property
can be written as follows,
Syntax:
1 2 3 |
__property <Data Type> <VisualPropertyName> = {read = <ReadName>, write = <WriteName>}; |
Here are the some __property examples to basic data types in C++ and C++ Builder,
How to add String property to a new component?
1 2 3 4 5 6 |
private: String title; __published: __property String MyTitle = {read = title, write = title}; |
How to add Integer property to a new component?
1 2 3 4 5 6 7 |
private: int myi; __published: __property int MyInt = {read = myi, write = myi}; |
How to add Float property to a new component?
1 2 3 4 5 6 |
private: float myf; __published: __property float MyFloat = {read = myf, write = myf}; |
How to add TStringList property to a new component?
1 2 3 4 5 6 |
private: TStringList *slist; __published: __property TStringList *MyList = {read = slist, write = slist}; |
How to add TPointF property to a new component?
1 2 3 4 5 6 |
private: TPointF point; __published: __property TPointF MyPoint = {read = point, write = point}; |
Full Header Example for a New Component
Here is a example with custom user defined custom __property‘ies,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
// --------------------------------------------------------------------------- #ifndef MyCompoUnit1H #define MyCompoUnit1H // --------------------------------------------------------------------------- #include <System.SysUtils.hpp> #include <System.Classes.hpp> #include <FMX.Controls.hpp> #include <FMX.Objects.hpp> #include <FMX.Types.hpp> #include <FMX.Colors.hpp> // --------------------------------------------------------------------------- class PACKAGE TMyCompo : public Fmx::Controls::TControl { typedef Fmx::Controls::TControl inherited; private: String title; int vx, vy, vz, count; TStringList *slist; protected: public: __fastcall TMyCompo(TComponent* Owner); __fastcall ~TMyCompo(); __published: __property String Title = {read = title, write = title}; __property int VX = {read = vx, write = vx}; __property int VY = {read = vy, write = vy}; __property int VZ = {read = vz, write = vz}; __property int Count = {read = count, write = count}; ; __property TStringList *List = {read = slist, write = slist}; }; // --------------------------------------------------------------------------- #endif |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition