How to code a property with sub-properties? (Translate)

I am sure I received a good answer to my previous question , because earlier I had a lot of help on other issues from the guys who posted there.

But I'm obviously doing something wrong, because when I copy the sample code that the object inspector for the MyProp property shows me, this is one text input field. I expected to see something similar to the Font property with Pitch, a font family, etc. I expect to see a tree structure, but I do not see the Color, Height, or Width properties of the MyProp property.

Any ideas? Again, I just copied this code.


Edit: I forgot to mention (in this question) that I am using a TMS pro script that allows users to create forms at runtime and provides their own object inspector, but this probably comes from Delphi standard material, I think.

Anyway, it seems I'm too dumb to code Delphi, since I just can't get it to work.


Edit: TMS assure me that if a class with โ€œsub-propertiesโ€ comes from TPresistent, it will appear in the object inspector with sub-properties, just like Font, Anchors, etc.

When I use this code, the Warning property appears as a text field in the object inspector and has no sub-properties

unit IntegerEditBox; // An edit box which only accepts integer values and warns if the value is not in a certain range interface uses SysUtils, Classes, Controls, StdCtrls, EditBox_BaseClass; type TWarning = Class(TPersistent) private FWarningBelowValue : Integer; FWarningAboveValue : Integer; FWarningEmailTo : String; FWarningSmsTo : String; published property WarningBelowValue : Integer read FWarningBelowValue write FWarningBelowValue; property WarningAboveValue : Integer read FWarningAboveValue write FWarningAboveValue; property WarningEmailTo : String read FWarningEmailTo write FWarningEmailTo; property WarningSmsTo : string read FWarningSmsTo write FWarningSmsTo; end; TIntegerEditBox = class(TEditBox_BaseClass) private FWarning : TWarning; procedure WriteValue(const newValue : Integer); protected // The new property which w/e introduce in this class FValue : Integer; public { Public declarations } Constructor Create(AOwner: TComponent); override; // This constructor uses defaults property Text; published { Published declarations - available in the Object Inspector at design-time } property Hint; // Now our own properties, which we are adding in this class property Value : Integer read FValue write WriteValue; property Warning : TWarning read FWarning write FWarning ; end; // of class TIntegerEditBox() procedure Register; implementation uses Dialogs; procedure Register; begin RegisterComponents('Standard', [TIntegerEditBox]); end; Constructor TIntegerEditBox.Create(AOwner: TComponent); begin inherited; // Call the parent Create method Hint := 'Only accepts a number|Only accepts a number'; // Tooltip | status bar text Mandatory := True; Value := 0; Text := IntToStr(Value); end; procedure TIntegerEditBox.WriteValue(const newValue : Integer); begin Text := IntToStr(newValue); end; end. 
+4
source share
2 answers

the original demo code neglected to instantiate the property object.

 constructor TMyControl.Create(AOwner: TComponent) begin inherited; FMyProp := TCustomType.Create; end; 

Remember to free it in the destructor.

The Remy comment in this answer indicates that the property needs to be assigned differently. The attribute of the write property should not be written directly to the field. Instead, it should have a setter method that works as follows:

 procedure TMyControl.SetMyProp(const Value: TCustomType); begin FMyProp.Assign(Value); end; 

It also emphasizes the requirement that the class method of the Assign class be implemented, otherwise you will get strange error messages like "Unable to assign TCustomType to TCustomType". A simple implementation might look like this:

 procedure TCustomType.Assign(Source: TPersistent); begin if Source is TCustomType then begin Color := TCustomType(Source).Color; Height := TCustomType(Source).Height; Width := TCustomType(Source).Width; end else inherited; end; 
+4
source

Read in this article he clearly explains the creation of additional properties.

+2
source

All Articles