简体   繁体   English

GetStrValue在自定义属性编辑器上返回空字符串

[英]GetStrValue returns empty string on Custom Property Editor

I want to write a custom property editor for my custom component. 我想为我的自定义组件编写一个自定义属性编辑器。 I have a component declaration like below: 我有一个组件声明,如下所示:

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;

And a property editor declaration like below: 以及如下所示的属性编辑器声明:

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;

Property editor opens if Assert(False, '"' + GetStrValue + '"'); 如果Assert(False, '"' + GetStrValue + '"');属性编辑器打开Assert(False, '"' + GetStrValue + '"'); is commented with empty memo because GetStrValue returns empty string. 用空备注注释,因为GetStrValue返回空字符串。

The SQL property is a TStrings property, not a string property, and GetStrValue only works on string properties, and if more than one component is selected, it returns the value of GetComponent(0). SQL属性是TStrings属性,不是字符串属性,GetStrValue仅适用于字符串属性,并且如果选择了多个组件,它将返回GetComponent(0)的值。 GetStrValue is a virtual property, so you can implement your own. GetStrValue是一个虚拟属性,因此您可以实现自己的属性。

Here is what I have in mind: 这是我的想法:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM