How do I group my component's properties in the Object Inspector?

柔情痞子 提交于 2019-12-19 02:43:09

问题


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 :)


回答1:


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.



来源:https://stackoverflow.com/questions/9434423/how-do-i-group-my-components-properties-in-the-object-inspector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!