首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有TStrings属性的组件在字符串列表编辑器中禁用了“代码编辑器”

具有TStrings属性的组件在字符串列表编辑器中禁用了“代码编辑器”
EN

Stack Overflow用户
提问于 2020-10-05 06:53:52
回答 1查看 250关注 0票数 0

我已经编写了一个具有TStrings类型属性的Delphi组件。除了在启动字符串列表编辑器时,“代码编辑器”按钮被禁用之外,所有这些都工作得很好。有人知道我需要设置什么才能允许这样做吗?

也许这是由于从集合编辑器中调用的原因?

整个组件大约有80行,所以我把它们都放在这里。它是一个VCL组件。

代码语言:javascript
复制
// Simple example of of creating a OwnedCollection of TStrings
unit TextStorageMin;

interface

uses
   System.Classes, System.SysUtils, Winapi.Windows, System.Generics.Collections;

type

// Storage class to store TStrings
  TStorageStrings = class(TCollectionItem)
  private
    FStrings: TStrings;
    procedure SetStrings(const Value: TStrings);
  public
  published
    constructor Create(Collection: TCollection); override;
    destructor Destroy; override;

    // Why, when this is brought up in the Strings List Editor, is
    // the "Code Editor" not enabled.
    property Strings: TStrings read FStrings write SetStrings;
  end;

  // Just simple Owned Collection
  TStorageList = class(TOwnedCollection);

  // This our component.
  TTextStorageMin = class(TComponent)
  private
    FStorageList: TStorageList;
  public
  published
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Items: TStorageList read FStorageList write FStorageList;
  end;

  procedure Register;

implementation

// Register it
procedure Register;
begin
  RegisterComponents('CompDev', [TTextStorageMin]);
end;

{ TTextStorage }

constructor TTextStorageMin.Create(AOwner: TComponent);
begin
  inherited;
  FStorageList := TStorageList.Create(AOwner, TStorageStrings);
end;

destructor TTextStorageMin.Destroy;
begin
  FStorageList.Free;
  inherited;
end;

{ TStorageStrings }

constructor TStorageStrings.Create(Collection: TCollection);
begin
  inherited;
  FStrings := TStringList.Create;
end;

destructor TStorageStrings.Destroy;
begin
  FStrings.Free;
  inherited;
end;

procedure TStorageStrings.SetStrings(const Value: TStrings);
begin
  FStrings.Assign(Value);
end;
EN

回答 1

Stack Overflow用户

发布于 2020-10-05 11:50:54

您的主要组件的代码完全错误。它完全错误地管理了TStorageList对象的所有权。它为对象分配了错误的Owner,并且没有属性设置器实现者来避免内存泄漏和获取外部对象(在本例中,是由集成开发环境在设计时创建和销毁的对象)的所有权。

此外,您的TStorageStrings类缺少Assign() (或AssignTo())的重载,这也导致了上述管理不善。

代码应该看起来更像这样:

代码语言:javascript
复制
// Simple example of of creating a OwnedCollection of TStrings
unit TextStorageMin;

interface

uses
  System.Classes;

type
  // Storage class to store TStrings
  TStorageStrings = class(TCollectionItem)
  private
    FStrings: TStrings;
    procedure SetStrings(const Value: TStrings);
  public
    constructor Create(Collection: TCollection); override;
    destructor Destroy; override;
    procedure Assign(ASource: TPersistent); override;
  published
    property Strings: TStrings read FStrings write SetStrings;
  end;

  // Just simple Owned Collection
  TStorageList = class(TOwnedCollection);

  // This our component.
  TTextStorageMin = class(TComponent)
  private
    FStorageList: TStorageList;
    procedure SetItems(const Value: TStorageList);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Items: TStorageList read FStorageList write SetItems;
  end;

procedure Register;

implementation

// Register it
procedure Register;
begin
  RegisterComponents('CompDev', [TTextStorageMin]);
end;

{ TTextStorage }

constructor TTextStorageMin.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FStorageList := TStorageList.Create(Self, TStorageStrings);
end;

destructor TTextStorageMin.Destroy;
begin
  FStorageList.Free;
  inherited;
end;

procedure TTextStorageMin.SetItems(const Value: TStorageList);
begin
  FStorageList.Assign(Value);
end;

{ TStorageStrings }

constructor TStorageStrings.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  FStrings := TStringList.Create;
end;

destructor TStorageStrings.Destroy;
begin
  FStrings.Free;
  inherited;
end;

procedure TStorageStrings.Assign(ASource: TPersistent);
begin
  if ASource is TStorageStrings then
    FStrings.Assign(TStorageStrings(ASource).Strings)
  else
    inherited;
end;

procedure TStorageStrings.SetStrings(const Value: TStrings);
begin
  FStrings.Assign(Value);
end;

end.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64200630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档