How can I add custom properties to my new component in a VCL application? What is the syntax for the component property? How can I add a visual property to my VCL 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 at design time and via code 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 own Components that can be used in your C++ app with or without visuals. Components make programming easy, you can do many operations easily without knowing techniques like low coding or some specific functionality – you just let the component do the hard work for you. Every component that you drag into an app’s forms is an object of the TComponent class. For example TEdit is a Class Type, a Component and Edit1 is an Object from the TEdit Component.
What is a Component in C++ ?
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 do I define properties of a VCL Component in C++ Builder?
Adding new properties to components is really 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.
Go to header of your component unit and list your own properties under __puplished 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 |
//--------------------------------------------------------------------------- #ifndef MyVCLCompo1H #define MyVCLCompo1H //--------------------------------------------------------------------------- #include <System.SysUtils.hpp> #include <System.Classes.hpp> #include <Vcl.Controls.hpp> #include <Vcl.ExtCtrls.hpp> //--------------------------------------------------------------------------- class PACKAGE TMyVCLComponent : public TImage { private: // add private definitions here related with visual __property definitions protected: public: __fastcall TMyVCLComponent(TComponent* Owner); __fastcall ~TMyVCLComponent(); __published: // Add __property definitions 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}; |
Is there a full Header example for setting properties of a new Component?
Here is a example with custom user defined custom properties,
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 40 41 |
//--------------------------------------------------------------------------- #ifndef MyVCLCompo1H #define MyVCLCompo1H //--------------------------------------------------------------------------- #include <System.SysUtils.hpp> #include <System.Classes.hpp> #include <Vcl.Controls.hpp> #include <Vcl.ExtCtrls.hpp> //--------------------------------------------------------------------------- class PACKAGE TMyVCLComponent : public TImage { private: String title; int vx, vy, vz, count; TStringList *slist; TPointF point; protected: public: __fastcall TMyVCLComponent(TComponent* Owner); __fastcall ~TMyVCLComponent(); __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 TPointF MyPoint = {read = point, write = point}; __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.