How do I group component properties in the Object Inspector?

I want my component not to visualize its published properties under a category not at the top level of the Object Inspector.

Take the example below:

enter image description here

type TMyComponent = class(TComponent) protected function GetSomeValue: string; function GetSomeValueExt: string; published property SomeValue: string read GetSomeValue; property SomeValueExt: string read GetSomeValueExt; end; procedure Register; implementation procedure Register; begin RegisterComponents('My Component', [TMyComponent]); end; function TMyComponent.GetSomeValue: string; begin Result := 'test'; end; function TMyComponent.GetSomeValueExt: string; begin Result := 'extended..'; end; 

How do I get my component to register with the Object Inspector with SomeValue and SomeValueExt under a category named something like MyProperties?

Illustration:

enter image description here

My component could potentially have many published properties, and I would prefer that they fall under their own subcategory of the Object Inspector level of the object, to remove it from common properties such as Name and Tag.

Thanks:)

+7
source share
1 answer

Create a class that has these properties, and then give your component one property of this class type. The property class must be a descendant of TPersistent :

 type TComponentProperties = class(TPersistent) private function GetSomeValue: string; function GetSomeValueExt: string; published property SomeValue: string read GetSomeValue; property SomeValueExt: string read GetSomeValueExt; end; TMyComponent = class(TComponent) private FProperties: TComponentProperties; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; published property Properties: TComponentProperties read FProperties; end; 

The component has its own property object, so it must be created and destroyed:

 constructor TMyComponent.Create; begin inherited; FProperties := TComponentProperties.Create; end; destructor TMyComponent.Destroy; begin FProperties.Free; inherited; end; 

With this code, the properties of the component should now be listed in the Properties element. This is not a category. Categories are something else. Categories do not reconfigure the component; they simply change how properties are displayed in the Object Inspector. Note that with my code, TMyComponent no longer has the SomeValue property. Instead, it simply has one Properties property, and this object has other properties. Think about whether you really want the consumers of your component to have access to it.


If the Properties property is not read-only, it must have the setter property; you cannot write it directly on FProperties . Write it like this:

 procedure TMyComponent.SetProperties(const Value: TProperties); begin FProperties.Assign(Value); end; 

It also requires that you redefine the Assign method to do the right thing:

 procedure TComponentProperties.Assign(Other: TPersistent); begin if Other is TComponentProperties then begin SomeValue := TComponentProperties(Other).SomeValue; SomeValueEx := TComponentProperties(Other).SomeValueEx; end else inherited; end; 

We also assume that the properties of a property object are also not readable. When the properties of a property object change, the owner object will probably want to know about it, so it should have an event to which the component assigns a value. When properties change, they trigger an event.

+17
source

All Articles