我想爲我的定製組件編寫定製屬性編輯器。我有一個組件聲明如下圖所示: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;
屬性編輯器打開。
的SQL屬性是TStrings屬性中,而不是字符串屬性,GetStrValue只適用於字符串屬性,如果選擇了多個組件,則返回GetComponent(0)的值。 GetStrValue是一個虛擬屬性,因此您可以實現自己的屬性,但更簡單的可能是將SQL屬性重新定義爲字符串屬性而不是TStrings屬性。 – Dsm
我有兩個組件,另一個繼承祖先類的SQL屬性,並且更改SQL屬性的類型不可能。我應該將GetComponent(0)轉換爲myclass併到達sql屬性,還是應該用rtti或類似的東西來查找SQL屬性? –
鑄造工作,特別是如果圍繞着一個**如果組件(0)是TEJSONQuery **。不過,不需要使用RTTI,只需使用SQL屬性的「文本」屬性即可。 – Dsm