我正在制作一个Delphi vcl组件,组件类有一个'images'属性,可以让我选择一个TImagelist。
组件类还有一个子属性“按钮”,它本身具有imageindex属性。
我已经为imageindex属性编写了一个组件编辑器,以便我可以从图像列表中选择按钮上的图像;我以前在其他组件中做过这个,但我现在面临的问题是我需要从'buttons'子类事件中的事件中获取基类的images属性。
因此,组件的基类具有以下属性:
property Buttons: TFlexButtons read FButtons write FButtons;
property Images: TCustomImageList read FImages write SetImages;
buttons类具有以下属性:
property ImageIndex: TImageIndex read FImageIndex write SetImageIndex default -1;
我在ImageIndex属性的单独单元中注册一个属性编辑器,以便选择一个图像但是在这个事件中我需要从组件的基类中获取图像列表,如何从这个子属性获取此属性?
function TImageIndexProperty.GetImageListAt(Index: Integer): TCustomImageList;
var APersistent: TPersistent;
begin
APersistent := GetComponent(Index);
if APersistent is TFlexButton then
Result := ??????????.Images //how do i refer to the images property of the component here?
else
Result := nil;
end;
所有课程:
TFlexButton = class(TCollectionItem)
private
FWidth: Word;
FCaption: string;
FHeight: Word;
FImageIndex: TImageIndex;
procedure SetCaption(const Value: string);
procedure SetHeight(const Value: Word);
procedure SetWidth(const Value: Word);
procedure SetImageIndex(const Value: TImageIndex);
public
constructor Create(AOwner: TComponent);
destructor Destroy; override;
published
property Caption: string read FCaption write SetCaption;
property Height: Word read FHeight write SetHeight;
property Width: Word read FWidth write SetWidth;
property ImageIndex: TImageIndex read FImageIndex write SetImageIndex default -1;
end;
TFlexButtons = class(TCollection)
private
function GetItem(Index: Integer): TFlexButton;
public
function Add: TFlexButton;
property Item[index: Integer]: TFlexButton read GetItem;
end;
TFlexButtonGroupBox = class(TcxGroupBox)
private
FDataLink: TFieldDataLink;
FAbout: string;
FAlignment: TAlignment;
FEnabled: Boolean;
FButtons: TFlexButtons;
FImages: TCustomImageList;
FSql: TStrings;
FAutosize: Boolean;
procedure SetAlignment(const Value: TAlignment);
function GetDataField: string;
function GetDataSource: TdataSource;
procedure SetDataField(const Value: string);
procedure SetDataSource(const Value: TdataSource);
procedure DataChange(Sender: TObject);
procedure SetEnabled(const Value: Boolean);
procedure SetImages(const Value: TCustomImageList);
procedure SetSql(const Value: TStrings);
procedure SetAutosize(const Value: Boolean);
protected
public
procedure Loaded; override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property DataField: string read GetDataField write SetDataField;
property DataSource: TdataSource read GetDataSource write SetDataSource;
property Enabled: Boolean read FEnabled write SetEnabled;
property Autosize: Boolean read FAutosize write SetAutosize;
property About: string read FAbout write FAbout;
property Buttons: TFlexButtons read FButtons write FButtons;
property Images: TCustomImageList read FImages write SetImages;
property Alignment: TAlignment read FAlignment write SetAlignment;
property Sql: TStrings read FSql write SetSql;
end;
答案 0 :(得分:8)
在设计时公开集合时,请直接使用TOwnedCollection
代替TCollection
。这有助于DFM流式传输,而无需编写额外的代码来启用它。
TCollectionItem
具有Collection
属性,该属性又具有TOwnedCollection
实现的Owner
方法。这样,您就可以从代码中的按钮到其拥有的组框。
试试这个:
TFlexButton = class(TCollectionItem)
private
...
public
constructor Create(ACollection: TCollection); override;
end;
TFlexButtonGroupBox = class;
TFlexButtons = class(TOwnedCollection)
private
...
public
constructor Create(AOwner: TFlexButtonGroupBox); reintroduce;
...
end;
TFlexButtonGroupBox = class(TcxGroupBox)
private
...
procedure SetButtons(AValue: TFlexButtons;
public
constructor Create(AOwner: TComponent); override;
...
published
...
property Buttons: TFlexButtons read FButtons write SetButtons;
...
end;
constructor TFlexButton.Create(ACollection: TCollection);
begin
inherited;
...
end;
constructor TFlexButtons.Create(AOwner: TFlexButtonGroupBox);
begin
inherited Create(AOwner, TFlexButton);
...
end;
constructor TFlexButtonGroupBox.Create(AOwner: TComponent);
begin
inherited;
FButtons := TFlexButtons.Create(Self);
...
end;
procedure TFlexButtonGroupBox.SetButtons(AValue: TFlexButtons;
begin
FButtons.Assign(AValue);
end;
function TImageIndexProperty.GetImageListAt(Index: Integer): TCustomImageList;
begin
Result := ((GetComponent(Index) as TFlexButton).Collection.Owner as TFlexButtonGroupBox).Images;
end;