在设计一个控件时,需要利用属性编辑器为这个控件增加两个属性。其中Cube属性需要根据catalog的值进行选择。为了方便操作,我增加了一个单元变量slxg,通过这个变量来实现在不同类的内部进行操作。下面的代码是一个简单的例子。但是出现了一个问题,在注册这个控件之后,在form上放置两个TlxgTest控件,如lxgTest1和lxgTest2,当点击lxgTest1的catalog属性进行选择的时候,可以看到(通过showmessage(slxg.Name);)showmessage出来的名称是lxgTest2,也就是说此时lxgTest1中的单元变量slxg已经变成了lxgTest2中的slxg,难道新增的属性编辑器只能保持一个(即以最后一个为准?)?请高手指点迷津!谢谢!unit lxgTest;interfaceuses
  Windows, Messages, SysUtils, Classes,DesignEditors,DesignIntf,dialogs;
type TCataProperty = class(TStringProperty)
    public
        function GetAttributes:TPropertyAttributes;override;
        function GetValue:string;override;
        procedure SetValue(const Value:string);override;
        procedure GetValues (proc:TGetStrProc);override;
    end;
type TCubeProperty = class(TStringProperty)
    public
        function GetAttributes:TPropertyAttributes;override;
        function GetValue:string;override;
        procedure SetValue(const Value:string);override;
        procedure GetValues (proc:TGetStrProc);override;
    end;
type
  TlxgTest = class(TComponent)
  private
        FCataProperty : string;
        FCubeProperty:string;
        procedure SetCatalog(const Value:string);
        procedure SetCube(const Value:string);
  public
        constructor Create(AOwner:TComponent);override;
  published
        property Catalog:string read FCataProperty write SetCatalog stored true;
        property Cube:string read FCubeProperty write SetCube stored true;
  end;procedure Register;implementation
var
    sTemp:string;
    slxg:TlxgTest;
procedure Register;
begin
  RegisterComponents('HDC4', [TlxgTest]);
  RegisterPropertyEditor(TypeInfo(string),TlxgTest,'Catalog',TCataProperty);
  RegisterPropertyEditor(TypeInfo(string),TlxgTest,'Cube',TCubeProperty);
end;{ TCataProperty }function TCataProperty.GetAttributes: TPropertyAttributes;
begin
    result := [paValueList];
end;function TCataProperty.GetValue: string;
begin
    sTemp := GetStrValue;
    result := GetStrValue;
end;procedure TCataProperty.GetValues(proc: TGetStrProc);
var
    i:integer;
begin
    showmessage(slxg.Name);
    for I := 0 to 4 do
    if Assigned(Proc)
    then Proc(IntToStr(I));
end;procedure TCataProperty.SetValue(const Value: string);
begin
    SetStrValue(Value);
end;{ TCubeProperty }function TCubeProperty.GetAttributes: TPropertyAttributes;
begin
    result := [paValueList];
end;function TCubeProperty.GetValue: string;
begin
    result := GetStrValue;
end;procedure TCubeProperty.GetValues(proc: TGetStrProc);
begin    if (sTemp = '1') then
        proc('this is 1');
    if (sTemp = '2') then
        proc('this is 2');
    if (sTemp = '3') then
        proc('this is 3');
    if (sTemp = '4') then
        proc('this is 4');
    if (sTemp = '5') then
        proc('this is 5');
end;procedure TCubeProperty.SetValue(const Value: string);
begin
  SetStrValue(Value);
end;
{ TlxgTest }
constructor TlxgTest.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  slxg := self;
end;procedure TlxgTest.SetCatalog(const Value: string);
begin
    FCataProperty := Value;
end;procedure TlxgTest.SetCube(const Value: string);
begin
    FCubeProperty := Value;
end;end.

解决方案 »

  1.   

    constructor TlxgTest.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      slxg := self; //好象这里有问题吧?slxg总是保存最后创建的TlxgTest对象
    end;
      

  2.   

    to movingboy(movingboy):感谢你的指正!
    但我在procedure TCataProperty.GetValues(proc: TGetStrProc);中需要
    使用一个TlxgTest对象,如果不通过一个全局变量,那我该如何实现!