菜鸟级问题, 200分的高分(200分)

  • 主题发起人 主题发起人 MuMian
  • 开始时间 开始时间
M

MuMian

Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个非常简单的控件:TWJPPrint, 其中有一个属性 HeadItems,其类型为记录类型,HeadItems属性有四个子属性,如下:
Caption: string
Name :String
value :string
Visible:boolean
我从 TCollection 继承了 TWJPPrintHeadItems,从 TCollectionItem 继承了 TWJPPrintHeadItem,做了一个控件,编译并安装之后,在 Object Inspector 编辑HeadItems属性,当我点击右边的按钮时,无法对该属性值进行编辑,出现的提示为:Invalid selection: 'TWJPPrintHeadItems' has no owmer
200大洋绝不吝啬。 这可是我口袋里所有的了,请大侠们给与指点:
----------------------------------------------------------
附原程序:

unit WJPPrint;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls;
type

TWJPPrint = class;

{TWJPPrintHeadItem}
TWJPPrintHeadItem = class(TCollectionItem)
private
FCaption: string;
FName: string;
FValue: string;
FVisible: boolean;
procedure SetCaption(value: string);
procedure SetName(value: string);
procedure SetValue(value: string);
procedure SetVisible(value: boolean);
public
constructor Create(Collection: TCollection); override;
procedure Assign(Source: TPersistent); override;
published
property Caption: string read FCaption write SetCaption;
property Name: string read FName write SetName;
property Value: string read FValue write SetValue;
property Visible: boolean read FVisible write SetVisible;
end;

{TWJPPrintHeadItems}
TWJPPrintHeadItems = class(TCollection)
Private
FWJPPrint: TWJPPrint;
function GetItem(Index: integer):TWJPPrintHeadItem;
procedure SetItem(Index:integer; value:TWJPPrintHeadItem);
public
constructor create(WJPPrint:TWJPPrint);
function Add: TWJPPrintHeadItem;
property Items[Index: integer]:TWJPPrintHeadItem read GetItem write SetItem; default;
end;

{TWJPPntHead}
TWJPPrint = class(TComponent)
private
FHeadItems: TWJPPrintHeadItems;
procedure SetHeadItems(Value: TWJPPrintHeadItems);
public
constructor create(AOwner: TComponent); override;
destructor Destroy; override;
published
property HeadItems: TWJPPrintHeadItems read FHeadItems write SetHeadItems;
end;

procedure Register;

implementation

{TWJPPrintHeadItem}
constructor TWJPPrintHeadItem.Create(Collection: TCollection);
begin
inherited Create(Collection);
FVisible :=false;
end;

procedure TWJPPrintHeadItem.Assign(Source: TPersistent);
begin
if Source is TWJPPrintHeadItem then
begin
FCaption :=TWJPPrintHeadItem(Source).Caption;
FName :=TWJPPrintHeadItem(Source).Name;
FValue :=TWJPPrintHeadItem(Source).Value;
FVisible :=TWJPPrintHeadItem(Source).Visible;
exit;
end;
inherited Assign(Source);
end;

procedure TWJPPrintHeadItem.SetCaption(value: string);
begin
if FCaption<>Value then
FCaption := Value;
end;

procedure TWJPPrintHeadItem.SetName(value: string);
begin
if FName<>Value then
FName := Value;
end;

procedure TWJPPrintHeadItem.SetValue(value: string);
begin
if FValue<>Value then
FValue := Value;
end;

procedure TWJPPrintHeadItem.SetVisible(value: boolean);
begin
if FVisible<>Value then
FVisible := Value;
end;

{TWJPPrintHeadItems}
constructor TWJPPrintHeadItems.create(WJPPrint:TWJPPrint);
begin
inherited Create(TWJPPrintHeadItem);
FWJPPrint :=WJPPrint;
end;

function TWJPPrintHeadItems.GetItem(Index: integer):TWJPPrintHeadItem;
begin
Result :=TWJPPrintHeadItem(inherited GetItem(Index));
end;

procedure TWJPPrintHeadItems.SetItem(Index:integer; Value:TWJPPrintHeadItem);
begin
Inherited SetItem(Index, Value);
end;

function TWJPPrintHeadItems.Add: TWJPPrintHeadItem;
begin
Result :=TWJPPrintHeadItem(inherited Add);
end;

{TWJPPrint}
constructor TWJPPrint.create(AOwner: TComponent);
begin
inherited Create(AOwner);
FHeadItems :=TWJPPrintHeadItems.create(self);
end;

destructor TWJPPrint.Destroy;
begin
FHeadItems.Free;
inherited Destroy;
end;

procedure TWJPPrint.SetHeadItems(Value: TWJPPrintHeadItems);
begin
FHeadItems.Assign(value);
end;


procedure Register;
begin
RegisterComponents('Samples', [TWJPPrint]);
end;

end.
 
重载GetOwner函数,GetOwner函数在TPersistent定义,函数原型为:
function GetOwner: TPersistent; dynamic;

你可以在TWJPPrintHeadItems的protected段加上 function GetOwner : TPersistent; override;
然后在function TWJPPrintHeadItems.GetOwner: TPersistent;写上一段赋值代码:
Result:=FWJPPrint;
 
非常感谢,200分即刻打上
 
后退
顶部