2010-09-16 14 views
7

Ad esempio, come Font. Qualcuno può dare un esempio molto semplice? Forse solo una proprietà con due sotto-proprietàCome si codifica una proprietà con proprietà secondarie?


Edit: voglio dire che quando guardo Font nella finestra di ispezione oggetto ha un po 'di segno più che posso fare clic per impostare il nome del font "Times New Roman", dimensione del carattere "10", ecc. Sorrry se uso i termini sbagliati, questo è quello che metto per "sotto-proprietà".

risposta

15

Tutto ciò che dovete fare è creare una nuova proprietà pubblicata che punta a un tipo che ha proprietà pubblicate.

controllo di questo codice

type 
    TCustomType = class(TPersistent) //this type has 3 published properties 
    private 
     FColor : TColor; 
     FHeight: Integer; 
     FWidth : Integer; 
    public 
     procedure Assign(Source: TPersistent); override; 
    published 
     property Color: TColor read FColor write FColor; 
     property Height: Integer read FHeight write FHeight; 
     property Width : Integer read FWidth write FWidth; 
    end; 

    TMyControl = class(TWinControl) 
    private 
     FMyProp : TCustomType; 
     FColor1 : TColor; 
     FColor2 : TColor; 
     procedure SetMyProp(AValue: TCustomType); 
    public 
     constructor Create(AOwner: TComponent); override; 
     destructor Destroy; override; 
    published 
     property MyProp : TCustomType read FMyProp write SetMyProp; //now this property has 3 "sub-properties" (this term does not exist in delphi) 
     property Color1 : TColor read FColor1 write FColor1; 
     property Color2 : TColor read FColor2 write FColor2; 
    end; 

    procedure TCustomType.Assign(Source: TPersistent); 
    var 
    Src: TCustomType; 
    begin 
    if Source is TCustomType then 
    begin 
     Src := TCustomType(Source); 
     FColor := Src.Color; 
     Height := Src.Height; 
     FWidth := Src.Width; 
    end else 
     inherited; 
    end; 

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

    destructor TMyControl.Destroy; 
    begin 
    FMyProp.Free; 
    inherited; 
    end; 

    procedure TMyControl.SetMyProp(AValue: TCustomType); 
    begin 
    FMyProp.Assign(AValue); 
    end; 
+6

Il setter per la proprietà TMyControl.MyProp non dovrebbe essere scrivendo FMyProp direttamente. Questo perderà l'oggetto FMyProp originale e diventerà proprietario dell'oggetto del chiamante. Invece, deve utilizzare un metodo setter che chiami FMyProp.Assign(), quindi TCustomType deve implementare Assign(). –

1

Cosa vuoi dire, sotto-proprietà? Non esiste una cosa del genere a Delfi.

Forse intendevi composizione di oggetti in cui un oggetto contiene un riferimento a un altro oggetto, per esempio -

interface 

type 
TObj1 = class 
private 
    FFont: TFont; 
    property Font: TFont read FFont; 
end; 

... 

implementation 

var 
    MyObj: TObj1; 
begin 
    MyObj1 := TObj1.Create; 
    MyObj1.Font.Name := 'Arial'; 
+0

(+1 grazie per la risposta) Voglio dire che quando guardo Font nell'ispettore dell'oggetto ha un piccolo segno eplus che posso cliccare per impostare il nome fonat "times new roman", la dimensione del font "10", ecc. Sorrry se Uso i termini sbagliati, questo è quello che metto per "sotto-proprietà". – Mawg

Problemi correlati