自定义属性只触发读取语句

所以,我正在尝试做一个组件来完成设置excel,libreoffice等单元的设置。 起初,我只是想设置的价值,但现在,我需要改变单元格的背景颜色,改变字体名称,风格,设置公式等等。所以,我决定做一个types,将举行所有我想改变的东西,所以我做了这个:

type TMyCell = class private FBgColor: TColor; FValue: String; FFormula: String; FFormat: String; FFont: TFont; public constructor Create; destructor Destroy; property Value: String read FValue write FValue; property Formula: String read FFormula write FFormula; property Format: String read FFormat write FFormat; property BgColor: TColor read FBgColor write FBgColor; property Font: TFont read FFont write FFont; end; { TMyCell } constructor TMyCell.Create; begin FFont := TFont.Create; end; destructor TMyCell.Destroy; begin FFont.Free; end; 

我的组件看起来像这样:

 type TMyPlan = class(TComponent) private FExcel: Variant; procedure SetMyCell(Row, Column: Integer; Value: TMyCell); function GetMyCell(Row, Column: Integer): TMyCell; public constructor Create(AOwner: TComponent); destructor Destroy; property Cell[Row, Column: Integer]: TMyCell read GetMyCell write SetMyCell; end; { TMyPlan } constructor TMyPlan.Create(AOwner: TComponent); begin inherited Create(AOwner); FExcel := CreateOleObject('Excel.Application'); FExcel.Workbooks.Add(1); end; destructor TMyPlan.Destroy; begin FExcel := Unassigned; inherited; end; function TMyPlan.GetMyCell(Row, Column: Integer): TMyCell; begin Result := TMyCell.Create; Result.Value := FExcel.Cells[Row, Column];; end; procedure TMyPlan.SetMyCell(Row, Column: Integer; Value: TMyCell); begin FExcel.Cells[Row, Column] := Value.Value; end; 

只是为了让你知道,我已经做了一些组件,而且我仍然在学习如何正确地做,所以这可能有一个不正常的结构,无论如何,这是我第一次尝试做这样的事情,一个具有子属性的input参数的属性,它似乎没有像我一样工作。

回到这个话题,我怎么称呼我的财产并不重要

设置: MyPlan.Cell [1,1] .Value:='1';

获取: ShowMessage(MyPlan.Cell [1,1] .Value);

无论哪种方式只有GetMyCell函数被触发。 为什么?

在Delphi中查看我对这个问题的回答: “左侧不能分配给”loggingtypes属性

虽然你在做什么不是完全一样的东西,它是相似的。 但是,就你而言,你为每次访问GetMyCell分配一个TMyCell的新实例。 这个“临时”实例没有被释放,并会泄漏(除非你在一个ARC平台上这样做)。

你的SetMyCell没有被调用的原因是你实际上并没有设置单元格,而是在单元实例上设置了一个 (我在上面解释过这个在泄漏)。