在 IDE 的 属性 编辑器中链接时限制组件列表
Restrict list of components when linking in property editor in IDE
我创建了一个新的设计时组件,其中包含已发布的 属性 Handler
类型 TComponent
并将其注册到工具面板中。
当我将这种类型的组件放在我的表单上时,IDE 的 属性 编辑器向我显示 属性 'Handler' 和一个下拉框,允许我在设计时设置这个属性。下拉框显示当前表单上所有可用的 TComponents。
如何将此处显示的组件列表(设计时)限制为特定类型或具有特定 属性 的组件?即实现特定(一组)接口的组件。
我知道你也可以使用interface-properties,但是网上也遇到过几个帖子说这个很不稳定,会出现各种问题。
是否有一种方法可以为每个建议的组件调用,我可以在设计时确定它们是否应该出现在列表中?
@David回答后的补充:
现在我已经知道 TComponentProperty 是我要找的东西,我在这里也发现了一个相关的问题:How to modify TComponentProperty to show only particular items on drop down list?
正如@TLama 已经指出的,您需要更改处理程序的类型 field/property。
现在,如果您想将特定类型的组件分配给此 field/property,请将此字段类型设置为与该组件相同的类型。
但是,如果您希望能够分配多个不同的组件类型而不是所有组件,请确保 Handler field/property 类型是您所需组件的第一个共同祖先 class/component 的类型您希望能够分配给处理程序 field/property.
- 导出
TComponentProperty
的子 class。
- 覆盖其
GetValues
方法以应用您的过滤器。
- 将此
TComponentProperty
注册为您 属性 的 属性 编辑器。
这是一个非常简单的例子:
组件
unit uComponent;
interface
uses
System.Classes;
type
TMyComponent = class(TComponent)
private
FRef: TComponent;
published
property Ref: TComponent read FRef write FRef;
end;
implementation
end.
报名
unit uRegister;
interface
uses
System.SysUtils, System.Classes, DesignIntf, DesignEditors, uComponent;
procedure Register;
implementation
type
TRefEditor = class(TComponentProperty)
private
FGetValuesProc: TGetStrProc;
procedure FilteredGetValuesProc(const S: string);
public
procedure GetValues(Proc: TGetStrProc); override;
end;
procedure TRefEditor.FilteredGetValuesProc(const S: string);
begin
if S.StartsWith('A') then
FGetValuesProc(S);
end;
procedure TRefEditor.GetValues(Proc: TGetStrProc);
begin
FGetValuesProc := Proc;
try
inherited GetValues(FilteredGetValuesProc);
finally
FGetValuesProc := nil;
end;
end;
procedure Register;
begin
RegisterComponents('Test', [TMyComponent]);
RegisterPropertyEditor(TypeInfo(TComponent), nil, 'Ref', TRefEditor);
end;
end.
这个相当无用的 属性 编辑器只会为您提供名称以 A
开头的组件。尽管它完全没有实用性,但这确实说明了您想要的过滤能力。您可能希望调用 Designer.GetComponent(...)
传递组件名称以获取组件实例,并根据该组件实例的类型和状态实施过滤。
我创建了一个新的设计时组件,其中包含已发布的 属性 Handler
类型 TComponent
并将其注册到工具面板中。
当我将这种类型的组件放在我的表单上时,IDE 的 属性 编辑器向我显示 属性 'Handler' 和一个下拉框,允许我在设计时设置这个属性。下拉框显示当前表单上所有可用的 TComponents。
如何将此处显示的组件列表(设计时)限制为特定类型或具有特定 属性 的组件?即实现特定(一组)接口的组件。
我知道你也可以使用interface-properties,但是网上也遇到过几个帖子说这个很不稳定,会出现各种问题。
是否有一种方法可以为每个建议的组件调用,我可以在设计时确定它们是否应该出现在列表中?
@David回答后的补充:
现在我已经知道 TComponentProperty 是我要找的东西,我在这里也发现了一个相关的问题:How to modify TComponentProperty to show only particular items on drop down list?
正如@TLama 已经指出的,您需要更改处理程序的类型 field/property。
现在,如果您想将特定类型的组件分配给此 field/property,请将此字段类型设置为与该组件相同的类型。
但是,如果您希望能够分配多个不同的组件类型而不是所有组件,请确保 Handler field/property 类型是您所需组件的第一个共同祖先 class/component 的类型您希望能够分配给处理程序 field/property.
- 导出
TComponentProperty
的子 class。 - 覆盖其
GetValues
方法以应用您的过滤器。 - 将此
TComponentProperty
注册为您 属性 的 属性 编辑器。
这是一个非常简单的例子:
组件
unit uComponent;
interface
uses
System.Classes;
type
TMyComponent = class(TComponent)
private
FRef: TComponent;
published
property Ref: TComponent read FRef write FRef;
end;
implementation
end.
报名
unit uRegister;
interface
uses
System.SysUtils, System.Classes, DesignIntf, DesignEditors, uComponent;
procedure Register;
implementation
type
TRefEditor = class(TComponentProperty)
private
FGetValuesProc: TGetStrProc;
procedure FilteredGetValuesProc(const S: string);
public
procedure GetValues(Proc: TGetStrProc); override;
end;
procedure TRefEditor.FilteredGetValuesProc(const S: string);
begin
if S.StartsWith('A') then
FGetValuesProc(S);
end;
procedure TRefEditor.GetValues(Proc: TGetStrProc);
begin
FGetValuesProc := Proc;
try
inherited GetValues(FilteredGetValuesProc);
finally
FGetValuesProc := nil;
end;
end;
procedure Register;
begin
RegisterComponents('Test', [TMyComponent]);
RegisterPropertyEditor(TypeInfo(TComponent), nil, 'Ref', TRefEditor);
end;
end.
这个相当无用的 属性 编辑器只会为您提供名称以 A
开头的组件。尽管它完全没有实用性,但这确实说明了您想要的过滤能力。您可能希望调用 Designer.GetComponent(...)
传递组件名称以获取组件实例,并根据该组件实例的类型和状态实施过滤。