C++ComponentsLanguage FeatureLearn C++

How To Set Properties Of A New Windows VCL Component In C++

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 TObjectTPersistentTComponentTControl, 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 noTControl. 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.

Syntax for a __property can be written as follows,

Syntax:

Here are the some __property examples to basic data types in C++ and C++ Builder,

How to add String property to a new component?

How to add Integer property to a new component?

How to add Float property to a new component?

How to add TStringList property to a new component?

How to add TPointF property to a new component?

Is there a full Header example for setting properties of a new Component?

Here is a example with custom user defined custom properties,

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?