0

我想爲我的定製組件編寫定製屬性編輯器。我有一個組件聲明如下圖所示:GetStrValue在定製屬性編輯器上返回空字符串

type 
    TEJsonQuery = class(TComponent) 
    private 
     FSql: TStrings; 

     procedure SetSQL(const Value: TStrings); 
     { Private declarations } 
    protected 
     { Protected declarations } 
    public 
     constructor Create(AOwner: TComponent); override; 
     destructor Destroy; override; 
     { Public declarations } 
    published 
     property SQL: TStrings read FSql write SetSQL; 
     { Published declarations } 
    end; 

constructor TEJsonQuery.Create; 
begin 
    inherited Create(AOwner); 
    FSql := TStringList.Create; 
end; 

procedure TEJsonQuery.SetSQL(const Value: TStrings); 
begin 
    if SQL.Text <> Value.Text then 
    begin 
     //Close; 
     SQL.BeginUpdate; 
     try 
     SQL.Assign(Value); 
     finally 
     SQL.EndUpdate; 
     end; 
    end; 
end; 

destructor TEJsonQuery.Destroy; 
begin 
    inherited Destroy; 
    FSql.Free; 
end; 

和屬性編輯器,聲明如下圖所示:如果Assert(False, '"' + GetStrValue + '"');與空備忘錄評論說,因爲GetStrValue返回空字符串

type 
    TQuerySQLProperty = class(TStringProperty) 
    public 
     function GetAttributes: TPropertyAttributes; override; 
     procedure Edit; override; 
    end; 

    Tfrm_JsonQuerySQL = class(TForm) 
     btn_JsonQuerySQL: TButton; 
     mem_SQL: TMemo; 
    btn_OK: TButton; 
    btn_Cancel: TButton; 
    private 
     { Private declarations } 
    public 
     { Public declarations } 
    end; 

var 
    frm_JsonQuerySQL: Tfrm_JsonQuerySQL; 

procedure Register; 

implementation 

{$R *.dfm} 

procedure Register; 
begin 
    RegisterComponents('MyComponents', [TEJsonQuery]); 
    RegisterPropertyEditor(TypeInfo(TStrings), TEJsonQuery, 'SQL', TQuerySQLProperty); 
end; 

procedure TQuerySqlProperty.Edit; 
begin 
    frm_Ekol_JsonQuerySQL := Tfrm_Ekol_JsonQuerySQL.Create(Application); 
    try 
     Assert(False, '"' + GetStrValue + '"'); 
     frm_Ekol_JsonQuerySQL.mem_SQL.Lines.Text := GetStrValue; 
     // show the dialog box 
     if frm_Ekol_JsonQuerySQL.ShowModal = mrOK then 
     begin 
     SetStrValue(frm_Ekol_JsonQuerySQL.mem_SQL.Lines.Text); 
     end; 
    finally 
     frm_Ekol_JsonQuerySQL.Free; 
    end; 
end; 

function TQuerySQLProperty.GetAttributes: TPropertyAttributes; 
begin 
    // editor, sorted list, multiple selection 
    // Result := [paDialog, paMultiSelect, paValueList, paSortList]; 
    Result := [paDialog]; 
end; 

屬性編輯器打開。

+2

的SQL屬性是TStrings屬性中,而不是字符串屬性,GetStrValue只適用於字符串屬性,如果選擇了多個組件,則返回GetComponent(0)的值。 GetStrValue是一個虛擬屬性,因此您可以實現自己的屬性,但更簡單的可能是將SQL屬性重新定義爲字符串屬性而不是TStrings屬性。 – Dsm

+0

我有兩個組件,另一個繼承祖先類的SQL屬性,並且更改SQL屬性的類型不可能。我應該將GetComponent(0)轉換爲myclass併到達sql屬性,還是應該用rtti或類似的東西來查找SQL屬性? –

+0

鑄造工作,特別是如果圍繞着一個**如果組件(0)是TEJSONQuery **。不過,不需要使用RTTI,只需使用SQL屬性的「文本」屬性即可。 – Dsm

回答

3

SQL屬性是一個TStrings屬性,而不是一個字符串屬性,GetStrValue只對字符串屬性起作用,並且如果選擇了多個組件,它將返回GetComponent(0)的值。 GetStrValue是一個虛擬財產,所以你可以實現你自己的。

這裏是我的想法:

type 
    TQuerySqlProperty = ... 
    public 
    function GetStrValue : string; override; 
    ... 
    end; 
    ... 

function TQuerySqlProperty.GetStrValue : string; 
begin 
    if GetComponent(0) is TEJsonQuery then 
    begin 
    Result := (GetComponent(0) as TEJsonQuery).SQL.Text; 
    end 
    else 
    begin 
    Result := inherited; 
    end; 
end; 
+0

這次它顯示對象檢查器中的sql.text值而不是「(TStrings)」,是否有可能從對象檢查器中隱藏sql.text內容。我只想在自定義屏幕中點擊3個點時顯示內容。 –

+0

不重寫GetXxxValue方法解決了問題:)取而代之,我寫了一個新方法,並使用它的名稱不是GetXxxValue –

+0

我確實考慮過這種可能性,但我認爲在編輯器中看到SQL會很有用。 – Dsm