首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当“.”时没有反应单击表示另一个TCollectionItem的TCollection属性。

当“.”时没有反应单击表示另一个TCollectionItem的TCollection属性。
EN

Stack Overflow用户
提问于 2018-04-24 13:41:47
回答 1查看 116关注 0票数 0

我从来没有遇到过需要它的情况,这是我第一次尝试将TCollection作为另一个TCollectionTCollectionItem。它都编译得很好,但是当单击TCollectionItemTCollection属性后面的三个点时,没有反应,即没有出现带有子TCollection列表的对话框。

我的印象是,由于不需要任何花哨的属性编辑器(子TCollection只包含具有stringsingle属性的项),IDE将自动处理它。

很明显,事实并非如此,或者我在监督显而易见的事情,这是一种长期困扰。

实现(运行时)单元如下:

代码语言:javascript
复制
type

  TBitmapItemTag = class(TCollectionItem)
  private
    FTagName: string;
    FTagFloat: Single;
  published
    property TagName: string read FTagName write FTagName;
    property TagFloat: Single read FTagFloat write FTagFloat;
  end;

  TBitmapItemTags = class(TOwnedCollection)

  end;

  TBitmapItem = class(TCollectionItem)
  private
    FBitmap: TBitmap;
    FBitmapItemTags: TBitmapItemTags;
  public
    constructor Create(Collection: TCollection); override;
    destructor Destroy; override;
  published
    property Bitmap: TBitmap read FBitmap write FBitmap;
    property Tags: TBitmapItemTags read FBitmapItemTags write FBitmapItemTags;
  end;

  TBitmaps = class(TCollection)

  end;

  TBitmapCollection = class(TComponent)
  private
    FBitmaps: TBitmaps;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Bitmaps: TBitmaps read FBitmaps write FBitmaps;
  end;

implementation

{ TBitmapItem }

constructor TBitmapItem.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  FBitmap := TBitmap.Create(0, 0);
  FBitmapItemTags := TBitmapItemTags.Create(Self,TBitmapItemTag);
end;

destructor TBitmapItem.Destroy;
begin
  FBitmap.Free;
  FBitmapItemTags.Free;
  inherited;
end;

{ TBitmapCollection }

constructor TBitmapCollection.Create(AOwner: TComponent);
begin
  inherited;
  FBitmaps := TBitmaps.Create(TBitmapItem);
end;

destructor TBitmapCollection.Destroy;
begin
  FBitmaps.Free;
  inherited;
end;

Register过程在设计时单元中实现,只调用RegisterComponents过程.并进行了一些懒惰的RegisterPropertyEditor尝试,但都没有成功。

如果有人能为我指出最短的路径,以便IDE识别TBitmapItemTag TCollectionItem,我将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-24 18:11:01

您需要将TBitmaps更改为从TOwnedCollection派生而不是从TCollection派生。

我还建议为TBitmapItemTagsTBitmaps定义显式构造函数。

您还需要向基于对象的属性添加一些setter方法,否则在运行时可能会发生内存泄漏。您的设置程序应该在对象上调用Assign(),以便将属性值从一个对象复制到另一个对象。TCollection已经为您实现了Assign(),但是您必须在集合项中实现Assign()

试试这个:

代码语言:javascript
复制
type
  TBitmapItemTag = class(TCollectionItem)
  private
    FTagName: string;
    FTagFloat: Single;
  public
    procedure Assign(ASource: TPersistent); override;
  published
    property TagName: string read FTagName write FTagName;
    property TagFloat: Single read FTagFloat write FTagFloat;
  end;

  TBitmapItem = class;

  TBitmapItemTags = class(TOwnedCollection)
  public
    constructor Create(AOwner: TBitmapItem); reintroduce;
  end;

  TBitmapItem = class(TCollectionItem)
  private
    FBitmap: TBitmap;
    FTags: TBitmapItemTags;
    procedure SetBitmap(AValue: TBitmap);
    procedure SetTags(AValue: TBitmapItemTags);
  public
    constructor Create(Collection: TCollection); override;
    destructor Destroy; override;
    procedure Assign(ASource: TPersistent); override;
  published
    property Bitmap: TBitmap read FBitmap write SetBitmap;
    property Tags: TBitmapItemTags read FTags write SetTags;
  end;

  TBitmapCollection = class;

  TBitmaps = class(TOwnedCollection)
  public
    constructor Create(AOwner: TBitmapCollection); reintroduce;
  end;

  TBitmapCollection = class(TComponent)
  private
    FBitmaps: TBitmaps;
    procedure SetBitmaps(AValue: TBitmaps);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Bitmaps: TBitmaps read FBitmaps write SetBitmaps;
  end;

代码语言:javascript
复制
{ TBitmapTagItem }

procedure TBitmapItemTag.Assign(ASource: TPersistent);
begin
  if ASource is TBitmapItemTag then
  begin
    FTagName := TBitmapItemTag(ASource).TagName;
    FTagFloat := TBitmapItemTag(ASource).TagFloat;
  end
  else
    inherited;
end;

{ TBitmapItemTags }

constructor TBitmapItemTags.Create(AOwner: TBitmapItem);
begin
  inherited Create(AOwner, TBitmapItemTag);
end;

{ TBitmapItem }

constructor TBitmapItem.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  FBitmap := TBitmap.Create(0, 0);
  FTags := TBitmapItemTags.Create(Self);
end;

destructor TBitmapItem.Destroy;
begin
  FBitmap.Free;
  FTags.Free;
  inherited;
end;

procedure TBitmapItem.Assign(ASource: TPersistent);
begin
  if ASource is TBitmapItem then
  begin
    FBitmap.Assign(TBitmapItem(ASource).Bitmap);
    FTags.Assign(TBitmapItem(ASource).Tags);
  end
  else
    inherited;
end;

procedure TBitmapItem.SetBitmap(AValue: TBitmap);
begin
  FBitmap.Assign(AValue);
end;

procedure TBitmapItem.SetTags(AValue: TBitmapItemTags);
begin
  FTags.Assign(AValue);
end;

{ TBitmaps }

constructor TBitmaps.Create(AOwner: TBitmapCollection);
begin
  inherited Create(AOwner, TBitmapItem);
end;

{ TBitmapCollection }

constructor TBitmapCollection.Create(AOwner: TComponent);
begin
  inherited;
  FBitmaps := TBitmaps.Create(Self);
end;

destructor TBitmapCollection.Destroy;
begin
  FBitmaps.Free;
  inherited;
end;

procedure TBitmapCollection.SetBitmaps(AValue: TBitmaps);
begin
  FBitmaps.Assign(AValue);
end;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50003211

复制
相关文章

相似问题

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