我正在开发组件,但无法使其在设计时考虑属性集。
以下是该组件的摘录:
TRVEditFrame = class(TFrame)
...
private
{ Private declarations }
FRVEditor:TCustomRichView;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
protected
function GetRVEditor:TCustomRichView;
procedure SetRVEditor(Editor:TCustomRichView);
published
property RVEditor:TCustomRichView read GetRVEditor write SetRVEditor;
end;
...
constructor TRVEditFrame.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
SetRVEditor(FRVEditor);
...
end;
function TRVEditFrame.GetRVEditor:TCustomRichView;
begin
Result:=FRVEditor;
end;
procedure TRVEditFrame.SetRVEditor(Editor:TCustomRichView);
begin
if Assigned(Editor) then begin
FRVEditor:=Editor;
end;
end;
我可以注册该组件,将其放置为其他形式,并在设计时设置FRVEditor。
问题是当我运行应用程序时,由于Editor = nil,SetRVEditor()中的代码未执行。
如果我能够在设计时设置FRVEditor,那么为什么它在运行时设置为nil?我怎样才能解决这个问题 ?
我在这里添加我的进一步评论,因为解释时间太长
@Kenneth,谢谢您的答复
我的组件背后的想法是创建一个具有菜单,快捷方式,弹出菜单等的单个框架(因此可以选择组件TFrame)来管理4个TCustomRichView后代中的每一个。
这正是我使用TCustomRichView的原因:我可以将4个后代中的任何一个“插入”到我的组件框架中。这是可以与TTAble和TQuery连接的TDatasource的相同原理(它们具有相同的祖先)。
我想VCL不能将RVEditor链接到我在设计时设置的TCustomRichView后代的原因是因为TFrame没有OnCreate事件,例如TForm。
到目前为止,我设法通过在托管TRVEditFrame的TForm.OnCreate中手动调用TRVEditFrame.SetRVEditor来解决了该问题,但是我想知道是否有更好的方法来做到这一点,这就是为什么我在这里提出建议。
我知道您也可以为TFrames创建一个OnCreate事件,也许我可以在其中放置TRVEditFrame.SetRVEditor,但是,我再次想知道是否有更好的方法。
关于您的意见的最后一部分,我知道注册程序,但是考虑到组件正在开发中。在开发组件时,我永远不会将其安装在IDE中,因为我更喜欢将测试内容保留在“正式”组件之外。我使用这种方法,并且一旦组件准备就绪,就可以使用您提到的过程进行注册。如果我想对同一组件实现其他功能,则可以使用测试功能,并继续使用我在IDE中同时拥有的“正式”功能。
我的建议是更改设计,以便通过使用Notification方法来获取引用自定义编辑器的框架组件,而不是手动设置属性。因此,将您的框架类更改为
TRVEditFrame = class(TFrame)
...
private
{ Private declarations }
FRVEditor: TCustomRichView;
protected
procedure Notification(aComponent: TComponent; aOperation: TOperation); override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
public
property RVEditor:TCustomRichView read FRVEditor;
end;
Notification方法的实现神奇地将框架连接/断开与自定义编辑器的连接
procedure TRVEditFrame.Notification(aComponent: TComponent;
aOperation: TOperation);
begin
inherited;
if aComponent is TCustomRichView then
if aOperation=opRemove then begin
if aComponent=FRVEditor then
FRVEditor := nil;
end else
FRVEditor := TCustomRichView(aComponent);
end;
我不知道在将编辑器设置/重置为框架时是否需要任何特殊处理,因此此代码不会做任何特殊操作,仅在适当的时候将组件或nil分配给FRVEditor数据成员。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句