2011-02-10 46 views

回答

13

使財產published。例如,

private 
    FMyProperty: integer; 
published 
    property MyProperty: integer read FMyProperty write FMyProperty; 

通常,當屬性發生更改時,您需要重新繪製控件(或執行其他一些處理)。然後,你可以做

private 
    FMyProperty: integer; 
    procedure SetMyProperty(MyProperty: integer); 
published 
    property MyProperty: integer read FMyProperty write SetMyProperty; 

... 

procedure TMyControl.SetMyProperty(MyProperty: integer); 
begin 
    if FMyProperty <> MyProperty then 
    begin 
    FMyProperty := MyProperty; 
    Invalidate; // for example 
    end; 
end; 
+0

哇,前10秒,你快! – jachguate 2011-02-10 17:22:44

4

該屬性添加到已發佈的部分,它將使出現在Object Inspector中,像這樣:

TMyComponent = class(TComponent) 
... 
published 
    property MyProperty: string read FMyProperty write SetMyProperty; 
3

docs

屬性在組件的 聲明的 已發佈 聲明中聲明的聲明在設計時可在對象 Inspector中編輯。

1

不要忘了組件需要在Delphi中註冊(最好在設計時間包中),或者你根本不會在對象檢查器中看到任何東西!

我是說......我可以創建一個新的TPanel後裔稱爲TMyPanel和一個新的發佈時間屬性添加到它:

type 
    TPanel1 = class(TPanel) 
    private 
    FMyName: String; 
    { Private declarations } 
    protected 
    { Protected declarations } 
    public 
    { Public declarations } 
    published 
    { Published declarations } 
    property MyName : String read FMyName write FMyName; 
    end; 

,但如果你HAVN」該屬性將不會顯示在Object Inspector牛逼註冊使用RegisterComponent新的類:

procedure Register; 
begin 
    RegisterComponents('Samples', [TPanel1]); 
end; 

只要是完整的:-)

相關問題