Grouping Related Properties

Standard Delphi components like TForm and TShape display related properties as groups of properties. For instance, the Brush, Pen, and Constraints properties of the TShape component are simply groups of related properties (see Figure 24-23).

image from book
Figure 24-23: Grouped properties

Grouped properties, like the Color and Style properties in the Brush group, actually don't belong directly to the TShape component but to a subcomponent of type TBrush.

To create a property group, you first have to derive a new class from TPersistent (for instance, TMyProperties) and add your properties to the published section of that class. When you're done with the TPersistent descendant, add the property group to the main component by creating a TMyProperties object property.

The following listing shows both the TPersistent descendant class and a main component that uses it.

Listing 24-14: Creating a property group

image from book
unit PropertyGroup; interface uses   SysUtils, Classes, Graphics; type   TMyProperties = class(TPersistent)   private     FColor: TColor; { needs Graphics in the uses list }     FID: Integer;     FText: string;   published     property Color: TColor read FColor write FColor;     property ID: Integer read FID write FID;     property Text: string read FText write FText;   end;   TPropertyGroup = class(TComponent)   private     { Private declarations }     FMyProperties: TMyProperties;   protected     { Protected declarations }     procedure SetMyProperties(Value: TMyProperties); virtual;   public     { Public declarations }     constructor Create(AOwner: TComponent); override;     destructor Destroy; override;   published     { Published declarations }     property MyProperties: TMyProperties       read FMyProperties write SetMyProperties;   end; procedure Register; implementation procedure Register; begin   RegisterComponents('My Components', [TPropertyGroup]); end; constructor TPropertyGroup.Create(AOwner: TComponent); begin   inherited Create(AOwner);   FMyProperties := TMyProperties.Create; end; destructor TPropertyGroup.Destroy; begin   FMyProperties.Free;   inherited Destroy; end; procedure TPropertyGroup.SetMyProperties(Value: TMyProperties); begin   FMyProperties.Assign(Value); end; end.
image from book

Finally, recompile the package and drop a TPropertyGroup component on the Designer Surface to see the MyProperties property group.

image from book
Figure 24-24: The MyProperties property group

Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: