如何在对象检查器中对组件的属性进行分组? [英] How do I group my component's properties in the Object Inspector?

查看:157
本文介绍了如何在对象检查器中对组件的属性进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请看下面的例子,我想要我的组件是非视觉的,使其发布的属性不在对象检查器的顶层。 :



  type 
TMyComponent = class(TComponent)
protected
function GetSomeValue:string;
函数GetSomeValueExt:string;
发布
属性SomeValue:string read GetSomeValue;
属性SomeValueExt:string读取GetSomeValueExt;
结束

程序注册;

执行

程序注册;
begin
RegisterComponents('My Component',[TMyComponent]);
结束

function TMyComponent.GetSomeValue:string;
begin
结果:='test';
结束

函数TMyComponent.GetSomeValueExt:string;
begin
结果:='extended ..';
结束

如何使用SomeValue和SomeValueExt在Object Inspector中注册一个名为something的类别像MyProperties?



插图:





我的组件可能会有很多已发布的属性,我宁愿他们在自己的层面下对象检查器的子类别,使其远离常见属性,如名称和标签。



感谢:)

解决方案

创建一个具有这些属性的类,然后给您的组件一个类型的属性。属性类应为 TPersistent 后代:

 键入
TComponentProperties = class(TPersistent)
private
函数GetSomeValue:string;
函数GetSomeValueExt:string;
发布
属性SomeValue:string read GetSomeValue;
属性SomeValueExt:string读取GetSomeValueExt;
结束

TMyComponent = class(TComponent)
private
FProperties:TComponentProperties;
public
构造函数Create(AOwner:TComponent);覆盖
析构函数覆盖
发布
属性属性:TComponentProperties读取FProperties;
结束

组件拥有其属性对象,因此需要创建和销毁它:

 构造函数TMyComponent.Create; 
开始
继承;
FProperties:= TComponentProperties.Create;
结束

析构函数TMyComponent.Destroy;
begin
FProperties.Free;
继承;
结束

使用该代码,组件的属性现在应该列在属性项下。但这不是一个类别。类别完全是别的。类别不重新安排组件;他们只是在对象检查器中更改属性 的显示方式。请注意,使用我的代码, TMyComponent 不再有任何 SomeValue 属性。相反,它只有一个属性属性 对象具有其他属性。考虑这是否真的希望您的组件的消费者能够访问它。






如果属性属性不是只读的,那么它需要一个属性设置器;你不能直接写到 FProperties 。写这样:

  procedure TMyComponent.SetProperties(const Value:TProperties); 
begin
FProperties.Assign(Value);
结束

这也要求您覆盖 Assign 方法做正确的事情:

  procedure TComponentProperties.Assign(Other:TPersistent); 
begin
如果其他是TComponentProperties然后开始
SomeValue:= TComponentProperties(Other).SomeValue;
SomeValueEx:= TComponentProperties(Other).SomeValueEx;
end else
继承;
结束

我们也假设属性对象的属性不是只读的。当属性对象的属性更改时,拥有的对象可能会想知道它,因此它应该有一个事件,该组件分配一个值。当属性更改时,它们将触发事件。


I want my component which will be non-visual to have its published properties under a category not on the top level of the Object Inspector.

Take the below example:

type
  TMyComponent = class(TComponent)
  protected
    function GetSomeValue: string;
    function GetSomeValueExt: string;
  published
    property SomeValue: string read GetSomeValue;
    property SomeValueExt: string read GetSomeValueExt;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('My Component', [TMyComponent]);
end;

function TMyComponent.GetSomeValue: string;
begin
  Result := 'test';
end;

function TMyComponent.GetSomeValueExt: string;
begin
  Result := 'extended..';
end;

How do I get my component to register in the Object Inspector with SomeValue and SomeValueExt under a category named something like MyProperties?

Illustration:

My component could potentially have a lot of published properties and I would rather they went under there own level subcategory of the Object Inspector to keep it away from the common properties such as Name and Tag.

Thanks :)

解决方案

Create a class that has those properties, and then give your component a single property of that class type. The property class should be a TPersistent descendant:

type
  TComponentProperties = class(TPersistent)
  private
    function GetSomeValue: string;
    function GetSomeValueExt: string;
  published
    property SomeValue: string read GetSomeValue;
    property SomeValueExt: string read GetSomeValueExt;
  end;

  TMyComponent = class(TComponent)
  private
    FProperties: TComponentProperties;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Properties: TComponentProperties read FProperties;
  end;

The component owns its property object, so it needs to be created and destroyed:

constructor TMyComponent.Create;
begin
  inherited;
  FProperties := TComponentProperties.Create;
end;

destructor TMyComponent.Destroy;
begin
  FProperties.Free;
  inherited;
end;

With that code, the component's properties should now be listed under the "Properties" item. It's not a category, though. Categories are something else entirely. Categories don't re-arrange the component; they just change how the properties are displayed in the Object Inspector. Note that with my code, TMyComponent doesn't have any SomeValue property anymore. Instead, it just has one property, Properties, and that object has other properties. Consider whether that's really how you want consumers of your component to have to access it.


If the Properties property is not read-only, then it needs to have a property setter; you cannot have it write directly to FProperties. Write it like this:

procedure TMyComponent.SetProperties(const Value: TProperties);
begin
  FProperties.Assign(Value);
end;

That also requires that you override the Assign method to do the right thing:

procedure TComponentProperties.Assign(Other: TPersistent);
begin
  if Other is TComponentProperties then begin
    SomeValue := TComponentProperties(Other).SomeValue;
    SomeValueEx := TComponentProperties(Other).SomeValueEx;
  end else
    inherited;
end;

We're also assuming that the property object's properties aren't read-only, either. When the property object's properties change, the owning object will probably want to know about it, so it should have an event that the component assigns a value to. When the properties change, they'll trigger the event.

这篇关于如何在对象检查器中对组件的属性进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆