属性编辑器结果回传问题。头痛!(200分)

  • 主题发起人 主题发起人 kusanagi
  • 开始时间 开始时间
K

kusanagi

Unregistered / Unconfirmed
GUEST, unregistred user!
整个控件由三个文件组成:

定义控件的单元
MyDialog=(TComponent)
...
Selected: TMySelected;
end;
implementation
......

定义TMySelected单元。
TMySelected=(TObject)
...
property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;

定义PeropertyEditor单元
TSelectedEditor = class(TClassProperty)
private
function GetAttributes: TPropertyAttributes; override;
procedure Edit; override;
end;

function TSelectedEditor.GetAttributes: TPropertyAttributes;
begin
result:= [paDialog];
end;

procedure TSelectedEditor.Edit;
begin
SelectedEditorFrm:= TSelectedEditorFrm.Create(Application);
try
SelectedEditorFrm.SelectedEditor:= (GetComponent(0) as THrDialog).Selected;
SelectedEditorFrm.bbtnReset.OnClick:= (GetComponent(0) as THrDialog).ResetSelected;
SelectedEditorFrm.ShowModal;
finally
SelectedEditorFrm.Free;
end;
end;

现在的问题是Selected属性保存不上,或者说把控件放在Form上 and view text 的时候就没有看到这个属性???
谁能告诉我是为什么。
 

TMyDialog = class(TComponent)
private
FSelected: TMySelected;
procedure SetSelected(Value: TMySelected);
...
...
published
property Selected: TMySelected read FSelected write SetSelected;
...
...
end;

procedure SetSelected(Value: TMySelected);
begin
将Value赋值给FSelected及其他操作
end;

 
康夫说的有道理,你应该把它 Publish 出来,但是这不是最重要的。
你应该 override TComponent 的 DefineProperties 方法,看看 TDBGrid 吧:

procedure DefineProperties(Filer: TFiler); override;

procedure TCustomDBGrid.DefineProperties(Filer: TFiler);
begin
...
// 定义属性
Filer.DefineProperty('Columns', ReadColumns, WriteColumns, StoreIt);
end;

然后定义读写你的 TMySelected 类对象的方法,ReadColumns,、WriteColumns,
最好是使用流操作。

还有 TCustomGrid 保存列宽行高的例子:

procedure TCustomGrid.DefineProperties(Filer: TFiler);
// 判断有无必要保存列宽(没有改变则不保存)
function DoColWidths: Boolean;
begin
if Filer.Ancestor <> nil then
Result := not CompareExtents(TCustomGrid(Filer.Ancestor).FColWidths, FColWidths)
else
Result := FColWidths <> nil;
end;
// 判断有无必要保存行高(没有改变则不保存)
function DoRowHeights: Boolean;
begin
if Filer.Ancestor <> nil then
Result := not CompareExtents(TCustomGrid(Filer.Ancestor).FRowHeights, FRowHeights)
else
Result := FRowHeights <> nil;
end;


begin
inherited DefineProperties(Filer);
if FSaveCellExtents then
with Filer do
begin
DefineProperty('ColWidths', ReadColWidths, WriteColWidths, DoColWidths);
DefineProperty('RowHeights', ReadRowHeights, WriteRowHeights, DoRowHeights);
end;
end;

procedure TCustomGrid.ReadColWidths(Reader: TReader);
var
I: Integer;
begin
with Reader do
begin
ReadListBegin;
for I := 0 to ColCount - 1 do ColWidths := ReadInteger;
ReadListEnd;
end;
end;

procedure TCustomGrid.ReadRowHeights(Reader: TReader);
var
I: Integer;
begin
with Reader do
begin
ReadListBegin;
for I := 0 to RowCount - 1 do RowHeights := ReadInteger;
ReadListEnd;
end;
end;

最关键的一点是:自定义的对象,需要自己动手去读写。

From: BaKuBaKu
 
老哥,还没解决问题啊,我给你发的E-mail看了么?
 
写Selected属性,该句应改为:
property OnChange: TNotifyEvent read FOnChange write FSetSelected;

在Implement中定义FSetSelected方法
Procedure/Function SetSelected();
 
老兄,TMySelected的基类选错了,
tobject太低级了,它能为你做什么?
用TPersistent.
 
多人接受答案了。
 
后退
顶部