写了一个控件,属性却保存不住,到底是怎么回事呢?(50分)

Y

yaxich

Unregistered / Unconfirmed
GUEST, unregistred user!
写了一个控件,自定义了一个属性,代码如下:

Type
TCmps = Class(TPersistent)
private
FItems: TStrings;
function GetCount: Integer;
function GetItems(Index: Integer): String;
public
Constructor Create;
Destructor Destroy; override;
procedure Add(Const S: String);
procedure Clear;
property Count: Integer Read GetCount;
property Items[Index: Integer]: String Read GetItems;
end;

implementation

{ TCmps }

procedure TCmps.Add(const S: String);
begin
FItems.Add(S);
end;

procedure TCmps.Clear;
begin
FItems.Clear;
end;

constructor TCmps.Create;
begin
Inherited Create;
FItems := TStringList.Create;
end;

destructor TCmps.Destroy;
begin
FItems.Free;
inherited Destroy;
end;

function TCmps.GetCount: Integer;
begin
Result := FItems.Count;
end;

function TCmps.GetItems(Index: Integer): String;
begin
Try
Result := FItems[Index];
Except
Raise;
end;
end;

然后我对自定义控件声明属性:

Type
TMyComponent = Class(TComponent)
private
FCmps: TCmps;
procedure SetCmps(Value: TCmps);
...
public
Constructor Create(AOwner: TComponent); Override;
Destructor Destroy; Override;
...
published
property Cmps: TCmps Read FCmps Write SetCmps;

...
Constructor Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCmps := TCmps.Create;
end;

Destructor Destroy;
begin
FCmps.Free;
inherited Destroy;
end;

procedure TMyComponent.SetCmps(Value: TCmps);
begin
if Assigned(Value) then
FCmps.Assign(Value);
end;

自定义属性编辑器:
TCmpsProperty = Class(TClassProperty)
public
function GetAttributes : TPropertyAttributes ; override;
procedure Edit; override;
end;
...

{TCmpsProperty}

function TCmpsProperty.GetAttributes: TPropertyAttributes;
begin
Result := [paDialog];
end;

procedure TCtrlBtnProperty.Edit;
Var
aComponent: TMyComponent;
I: Integer;
begin
aComponent := TMyComponent(GetComponent(0));
with TCmpsEditor.Create(Nil) do
Try
if ShowModal = MrOK then
begin
aComponent.Cmps.Clear;
for I := 0 to CheckListBox.Items.Count - 1 do
if CheckListBox.Checked then
aComponent.Cmps.Add(ListBox.Items);
end;
Finally
Free;
end;
end;

注册控件编辑器:
begin
RegisterPropertyEditor(TypeInfo(TCmps),TMyComponent,'Cmps',TCmpsProperty);
end;

编译安装后,一切正常,但打开Cmps编辑器进行编辑后,Delphi的Save All按钮并没有
变化,单击Save按钮后,关闭MyComponent所在程序再重新打开时,Cmps的值并没有被保
存,
我百思不得其解,这到底是怎么会事呢?请高手指点,我应该从哪方面解决?
 
你的代码:
property Count: Integer Read GetCount;
property Items[Index: Integer]: String Read GetItems;
应该还有一个write的方法
 
to donkey:
为什么?请指教.我不需要向此属性写入数据呀.
 
需要用到 if not (csLoading in ComponentState) then 来判断一下
查查帮助,我以前也是这个问题,请了个高手给解决了,就是上面这句话
 
[Storage specifiers are not supported for array properties.]
属性保存不支持数组类型,详细可以查阅关于Stored 指示字的帮助
 
to antic_ant & yvtong:
非常感谢,小弟这就去查关于ComponentState 和 Stored的知识点。
感谢老人家的提携。
 
根据楼上几位的指点,进行了ComponentState和Stored的学习,但因为经验欠缺,一时仍
云山雾海般找不到北斗,同时发现当再属性编辑器中进行编辑后,单击OK按钮退回到程序
设计介面时,发现根本不执行属性的写方法:
procedure TMyComponent.SetCmps(Value: TCmps);
begin
if Assigned(Value) then
FCmps.Assign(Value);
end;
对此,小弟又是百思不得东西和南北,还请高手指点迷津。
 
既然没有人再发表高论,我也就结贴了。
 
顶部