我想为我的自定义组件编写一个自定义属性编辑器。我有一个如下的组件声明:
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;
以及如下的属性编辑器声明:
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;
如果使用空备注注释属性编辑器将打开Assert(False, '"' + GetStrValue + '"');
,因为 GetStrValue 返回空字符串。