[英]How to invoke a component's property editor at design time
I have created a component derived from TCustomPanel. 我创建了一个从TCustomPanel派生的组件。 On that panel I have a published property of a class derived from TOwnedCollection.
在该面板上,我有一个派生自TOwnedCollection的类的已发布属性。 All is working well and clicking the ellipsis in the object inspector for that property opens the default collection editor where I can manage the TCollectionItems in the list.
一切运行良好,单击该属性的对象检查器中的省略号将打开默认的集合编辑器,我可以在其中管理列表中的TCollectionItems。
TMyCustomPanel = class(TCustomPanel)
private
...
published
property MyOwnedCollection: TMyOwnedCollection read GetMyOwnedCollection write SetMyOwnedCollection;
end;
I would also like to be able to double-click on the panel at design-time and have the collection editor open up by default. 我还希望能够在设计时双击面板并默认打开集合编辑器。 I've started off by creating a class derived from TDefaultEditor and registering it.
我开始创建一个派生自TDefaultEditor的类并注册它。
TMyCustomPanelEditor = class(TDefaultEditor)
protected
procedure EditProperty(const PropertyEditor: IProperty; var Continue: Boolean); override;
end;
RegisterComponentEditor(TMyCustomPanel, TMyCustomPanelEditor);
This seems to be run at the right time, but I'm stuck on how to launch the property editor for the collection at that time. 这似乎是在正确的时间运行,但我仍然坚持如何在当时启动集合的属性编辑器。
procedure TMyCustomPanelEditor.EditProperty(const PropertyEditor: IProperty; var Continue: Boolean);
begin
inherited;
// Comes in here on double-click of the panel
// How to launch collection editor here for property MyOwnedCollection?
Continue := false;
end;
Any solution or different approach would be appreciated. 任何解决方案或不同的方法将不胜感激。
You aren't using the correct editor, so far as I can tell. 就我所知,你没有使用正确的编辑器。
TDefaultEditor
is described thus: 因此描述了
TDefaultEditor
:
An editor that provides default behavior for the double-click that will iterate through the properties looking the the most appropriate method property to edit
一个编辑器,为双击提供默认行为,该行为将遍历属性,查找要编辑的最合适的方法属性
This is an editor that responds to double clicks on the form by dropping you into the code editor with a newly created event handler. 这是一个编辑器,它通过使用新创建的事件处理程序将您放入代码编辑器来响应对表单的双击。 Think of what happens when you double click a
TButton
and you are dropped in to the OnClick
handler. 想想当您双击
TButton
并将其放入OnClick
处理程序时会发生什么。
It's been a long time since I wrote a design time editor (I hope my memory is working today) but I believe your editor should be derived from TComponentEditor
. 自从我编写设计时编辑器以来已经很长时间了(我希望我的内存今天正常工作),但我相信你的编辑器应该来自
TComponentEditor
。 In order to show the collection editor you call ShowCollectionEditor
from the ColnEdit
unit. 为了显示集合编辑器,您
ShowCollectionEditor
从ColnEdit
单元调用ShowCollectionEditor
。
You can override the Edit
method of TComponentEditor
and call ShowCollectionEditor
from there. 您可以覆盖
Edit
的方法TComponentEditor
并调用ShowCollectionEditor
从那里。 If you want to be more advanced, as an alternative you can declare some verbs with GetVerbCount
, GetVerb
and ExecuteVerb
. 如果你想成为更高级的,作为替代,你可以声明一些动词与
GetVerbCount
, GetVerb
和ExecuteVerb
。 If you do it this way then you extend the context menu and the default Edit
implementation will execute verb 0. 如果你这样做,那么你扩展上下文菜单,默认的
Edit
实现将执行动词0。
Following David's correct answer, I would like to provide the completed code that shows the CollectionEditor for a specific property of a UI control when it is double-clicked at design-time. 按照David的正确答案,我想提供完整的代码,该代码在设计时双击UI控件的特定属性时显示CollectionEditor。
type
TMyCustomPanel = class(TCustomPanel)
private
...
published
property MyOwnedCollection: TMyOwnedCollection read GetMyOwnedCollection write SetMyOwnedCollection;
end;
TMyCustomPanelEditor = class(TComponentEditor)
public
function GetVerbCount: Integer; override;
function GetVerb(Index: Integer): string; override;
procedure ExecuteVerb(Index: Integer); override;
end;
procedure Register;
begin
RegisterComponentEditor(TMyCustomPanel, TMyCustomPanelEditor);
end;
function TMyCustomPanelEditor.GetVerbCount: Integer;
begin
Result := 1;
end;
function TMyCustomPanelEditor.GetVerb(Index: Integer): string;
begin
Result := '';
case Index of
0: Result := 'Edit MyOwnedCollection';
end;
end;
procedure TMyCustomPanelEditor.ExecuteVerb(Index: Integer);
begin
inherited;
case Index of
0: begin
// Procedure in the unit ColnEdit.pas
ShowCollectionEditor(Designer, Component, TMyCustomPanel(Component).MyOwnedCollection, 'MyOwnedCollection');
end;
end;
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.