Delphi:覆盖Object Inspector中组件属性的赋值过程 [英] Delphi: override an assignment proc for components property in Object Inspector

查看:277
本文介绍了Delphi:覆盖Object Inspector中组件属性的赋值过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组件,例如 TBrandNewComponent ,它发布了属性 Jumbo:TJumboClass ,它是:

  TJumboClass = class 
P:指针
end;

我重载了 GetValues 过程来显示Object Inspector中的 Jumbos 值列表中的不同组件。现在列表显示 TJumboClass 的组件和 TMassiveClass TRotefulClass
$ b

还有 TBrandNewComponent.JumboSelectedComponentType:integer ,我想在 TBrandNewComponent.Jumbo 并且我想类似于 cast TMassiveClass TRotefulClass TJumboClass 将它们保存如下: TJumboClass.P =指针(TMassiveClass (电流))

这就是说,我需要存储一个属性 TBrandNewComponent.JumboClass.P 所以使用 JumboSelectedComponentType 我可以正确访问这个指针并在进一步的操作中返回正确的类。



那么,是否有任何程序,影响到这一点,并有可能吗?



任何建议和想法,赞赏!



PS:我已经发现了方法 SetValue 。现在我需要以某种方式重写它。它可能是什么?

解决方案

您描述的内容没有任何意义。

<如果你从 TJumboClass 获得 TMassiveClass TRotefulClass ,则Object Inspector会自动列出这些类类型的所有组件实例,无需编写自定义设计时编辑器来覆盖 GetValues(),默认实现

 类型
TBrandNewComponent =类(...)
私人
fJumbo :TJumboClass;
发布
属性Jumbo:TJumboClass读取fJumbo写入fJumbo;
end;

 类型
TMassiveClass = class(TJumboClass)
...
end;

TRotefulClass = class(TJumboClass)
...
end;

如果您不派生 TMassiveClass TRotefulClass 来自 TJumboClass 但仍希望Object Inspector自动列出这些类类型的所有组件实例,您可以使用为了这个目的,定制 IInterface (是的,您可以在发布的属性中使用接口):

 类型
IJumbo =接口(IInterface)
['DA51630E-2FFB-425D-A62D-F1BE414DBC6F}']
end;

TBrandNewComponent = class(...)
private
fJumbo:IJumbo;
发布
属性Jumbo:IJumbo读取fJumbo写入fJumbo;
end;

  type 
TJumboClass = class(...,IJumbo)
...
end;

TMassiveClass = class(...,IJumbo)
...
end;

TRotefulClass = class(...,IJumbo)
...
end;

不管怎样,要更新 TBrandNewComponent.JumboSelectedComponentType 只要 TBrandNewComponent.Jumbo 被更改,您可以为 Jumbo 属性提供一个setter方法,在运行时和设计时都被称为。再次,不需要自定义设计时编辑器:

 类型
TBrandNewComponent = class(...)
私人
fJumbo:TJumboClass;
程序SetJumbo(Value:TJumboClass);
发布
属性Jumbo:TJumboClass读取fJumbo写入SetJumbo;
end;

程序TBrandNewComponent.SetJumbo(Value:TJumboClass);
begin
if fJumbo<>价值然后
开始
fJumbo:=价值;
//根据需要更新JumboSelectedComponentType ...
end;

您对 JumboClass.P 的描述确实如此没有道理。假设 current 是当前分配给 Jumbo 属性的组件对象,那么您基本上将 Jumbo.P 指向组件对象的 Self 指针,这基本上是无用的。



由于您的其他属性名为 JumboSelectedComponentType ,因此听起来像您想要存储选定的类类型组件,而不是对象指针。如果是这种情况,那么更健壮的设计是给 JumboSelectedComponentType 属性一个getter方法(或者只是将其更改为公共函数而不是属性),该查询当前 Jumbo 对象为它的 ClassType



<$ p $类型
TBrandNewComponent = class(...)
private
fJumbo:TJumboClass;
public
函数JumboSelectedComponentType:TClass;
发布
属性Jumbo:TJumboClass读取fJumbo写入fJumbo;
end;

函数TBrandNewComponent.JumboSelectedComponentType:TClass;
begin
if fJumbo<>零然后
结果:= fJumbo.ClassType
else
结果:=零;
end;

或者:

 键入
TJumboClassType = TJumboClass类;

TBrandNewComponent = class(...)
private
fJumbo:TJumboClass;
public
函数JumboSelectedComponentType:TJumboClassType;
发布
属性Jumbo:TJumboClass读取fJumbo写入fJumbo;
end;

函数TBrandNewComponent.JumboSelectedComponentType:TJumboClassType;
begin
if fJumbo<> nil然后
结果:= TJumboClassType(fJumbo.ClassType)
else
结果:= nil;
end;

如果这不是您要查找的内容,那么您需要提供更多关于您的具体信息是真的在寻找,因为你的原始描述太模糊。


I have a component i.e. TBrandNewComponent, who has published property Jumbo: TJumboClass, which is:

TJumboClass = class
   P: Pointer
end;

I had overriden the GetValues procedure to show different components in a list of Jumbos's values in Object Inspector. Now list shows components of TJumboClass and components of TMassiveClass and TRotefulClass.

There is also TBrandNewComponent.JumboSelectedComponentType: integer, which I want to change due to selected component in TBrandNewComponent.Jumbo and also I want something-like cast TMassiveClass and TRotefulClass to TJumboClass saving them something like that: TJumboClass.P = Pointer(TMassiveClass(current)).

By that I mean, that I need to store to a property TBrandNewComponent.JumboClass.P a pointer of appropriate selected component, so that with a JumboSelectedComponentType I can correctly access this pointer and return the right class in further operations.

So, is there any procedure, that affects that and is that possible anyway?

Any suggestions and thoughts are appreciated!

PS: I've already discovered method SetValue. Now I need somehow to override it. What it might be?

解决方案

What you describe makes no sense.

If you derive TMassiveClass and TRotefulClass from TJumboClass then the Object Inspector will automatically list all component instances of those class types for you, there is no need to write a custom design-time editor to override GetValues(), the default implementation is sufficient.

type
  TBrandNewComponent = class(...)
  private
    fJumbo: TJumboClass;
  published
    property Jumbo: TJumboClass read fJumbo write fJumbo;
  end;

type
  TMassiveClass = class(TJumboClass)
    ...
  end;

  TRotefulClass = class(TJumboClass)
    ...
  end;

If you do not derive TMassiveClass and TRotefulClass from TJumboClass but still want the Object Inspector to automatically list all component instances of those class types for you, you can use a custom IInterface for that purpose (yes, you can use interfaces in published properties):

type
  IJumbo = interface(IInterface)
  ['{DA51630E-2FFB-425D-A62D-F1BE414DBC6F}']
  end;

  TBrandNewComponent = class(...)
  private
    fJumbo: IJumbo;
  published
    property Jumbo: IJumbo read fJumbo write fJumbo;
  end;

type
  TJumboClass = class(..., IJumbo)
    ...
  end;

  TMassiveClass = class(..., IJumbo)
    ...
  end;

  TRotefulClass = class(..., IJumbo)
    ...
  end;

Either way, to update the value of TBrandNewComponent.JumboSelectedComponentType whenever TBrandNewComponent.Jumbo is changed, you can provide a setter method for the Jumbo property, which will be called at both run-time and design-time. Again, no custom design-time editor is needed:

type
  TBrandNewComponent = class(...)
  private
    fJumbo: TJumboClass;
    procedure SetJumbo(Value: TJumboClass);
  published
    property Jumbo: TJumboClass read fJumbo write SetJumbo;
  end;

procedure TBrandNewComponent.SetJumbo(Value: TJumboClass);
begin
  if fJumbo <> Value then
  begin
    fJumbo := Value;
    // update JumboSelectedComponentType as needed...
end;

Your description of JumboClass.P really does not make sense. Assuming current is the component object currently assigned to the Jumbo property, then you are essentially setting Jumbo.P to the Self pointer of the component object, which is basically useless.

Since your other property is named JumboSelectedComponentType, it sounds like you want to store the class type of the selected component, not the object pointer. If that is the case, then a much more robust design is to give the JumboSelectedComponentType property a getter method (or just change it into a public function instead of a property) that queries the current Jumbo object for its ClassType:

type
  TBrandNewComponent = class(...)
  private
    fJumbo: TJumboClass;
  public
    function JumboSelectedComponentType: TClass;
  published
    property Jumbo: TJumboClass read fJumbo write fJumbo;
  end;

function TBrandNewComponent.JumboSelectedComponentType: TClass;
begin
  if fJumbo <> nil then
    Result := fJumbo.ClassType
  else
    Result := nil;
end;

Alternatively:

type
  TJumboClassType = class of TJumboClass;

  TBrandNewComponent = class(...)
  private
    fJumbo: TJumboClass;
  public
    function JumboSelectedComponentType: TJumboClassType;
  published
    property Jumbo: TJumboClass read fJumbo write fJumbo;
  end;

function TBrandNewComponent.JumboSelectedComponentType: TJumboClassType;
begin
  if fJumbo <> nil then
    Result := TJumboClassType(fJumbo.ClassType)
  else
    Result := nil;
end;

If this is NOT what you are looking for, then you need to provide more details about what exactly you are really looking for, because your original description is just too vague.

这篇关于Delphi:覆盖Object Inspector中组件属性的赋值过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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