属性编辑器的开发!(50分)

  • 主题发起人 燕岛秋潮
  • 开始时间

燕岛秋潮

Unregistered / Unconfirmed
GUEST, unregistred user!
我在开发控件的属性编辑器的时候,遇到一个问题,如果在form上放置多个相同的控件,在
变化一个控件时,其余的几个也同样跟着变化,不能独立设置!请各位指点!
具体的实例如下:
unit ssTest;

interface

uses
Windows, Messages, SysUtils, Classes,DesignEditors,DesignIntf;
type TCataProperty = class(TPropertyEditor)
public
function GetAttributes:TPropertyAttributes;override;
function GetValue:string;override;
procedure SetValue(const Value:String);override;
procedure GetValues (proc:TGetStrProc);override;
end;
type
TssTest = class(Tcomponent)
private
{ Private declarations }
FCataProperty : string;
function GetCatlaog:string;
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
property Catalog:string read GetCatlaog write FCataProperty stored true;
end;

procedure Register;

implementation
var
CName:string;
procedure Register;
begin
RegisterComponents('HDC4', [TssTest]);
RegisterPropertyEditor(TypeInfo(string),TssTest,'Catalog',TCataProperty);
end;

{ TCataProperty }

function TCataProperty.GetAttributes: TPropertyAttributes;
begin
result := [paValueList];
end;

function TCataProperty.GetValue: string;
begin
result := CName;
end;

procedure TCataProperty.GetValues(proc: TGetStrProc);
var
i:integer; st:TStringList;
begin
//st := TStringList.Create;
//for i:=0 to 4 do
// st.Add(inttostr(i));
for i:=0 to 4 do
begin
proc (inttostr(i));
end;

end;

procedure TCataProperty.SetValue(const Value:String);
begin
CName := Value;

end;

{ TssTest }

function TssTest.GetCatlaog: string;
begin
result := CName;
FCataProperty:= CName;
end;

end.
 
在为有一个变量CName定义成了全局变量,也就是说每一个属性编辑器实例都用同一个变量,当然会改变其中一个其他的也会改变了.
你把这个变量定义成私有成员就可以了!

unit ssTest;

interface

uses
Windows, Messages, SysUtils, Classes,DesignEditors,DesignIntf;
type TCataProperty = class(TPropertyEditor)
private
//////////////////////////////////////////////////////////
CName:string;//这个变量应该是TCataProperty 的私有成员.
//////////////////////////////////////////////////////////
public
function GetAttributes:TPropertyAttributes;override;
function GetValue:string;override;
procedure SetValue(const Value:String);override;
procedure GetValues (proc:TGetStrProc);override;
end;
type
TssTest = class(Tcomponent)
private
{ Private declarations }
FCataProperty : string;
function GetCatlaog:string;
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
property Catalog:string read GetCatlaog write FCataProperty stored true;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('HDC4', [TssTest]);
RegisterPropertyEditor(TypeInfo(string),TssTest,'Catalog',TCataProperty);
end;

{ TCataProperty }

function TCataProperty.GetAttributes: TPropertyAttributes;
begin
result := [paValueList];
end;

function TCataProperty.GetValue: string;
begin
result := CName;
end;

procedure TCataProperty.GetValues(proc: TGetStrProc);
var
i:integer; st:TStringList;
begin
//st := TStringList.Create;
//for i:=0 to 4 do
// st.Add(inttostr(i));
for i:=0 to 4 do
begin
proc (inttostr(i));
end;

end;

procedure TCataProperty.SetValue(const Value:String);
begin
CName := Value;

end;

{ TssTest }

function TssTest.GetCatlaog: string;
begin
result := CName;
FCataProperty:= CName;
end;

end.


但是我不明白这个属性编辑器和TssTest类有什么关系,为什么TssTest类的属性也从CName变量得到值.
也就是说你认为属性编辑器是什么的?你需要做什么?我认为你有些概念上的错误.
 
to aizb:
感谢你的帮助!
对于全局变量CName,主要是因为TCataProperty并未在TssTest 中存在一个控供操作的对象,
可能是在进行RegisterPropertyEditor(TypeInfo(string),TssTest,'Catalog',TCataProperty);
操作后已经将catalog属性与TCataProperty关联。所以我为了取得TCataProperty中选择的值,才
设置了一个全局变量。

可能我的理解有误,我原来认为在form上创建一个TssTest控件,同样也会创建一个TCataProperty
对象与之对应,现在看来TCataProperty对象始终只有一个。

如果把CName设置为私有变量,那么TssTest控件如何去接受选定的值。
 
谢谢你的帮助。
分数我会给你加上的。
 
接受答案了.
 
你对属性编辑器的理解有点问题,属性编辑器只需要RegisterPropertyEditor函数与所需要编辑的类的某个属性关联起来,其他的事情都由Delphi来
 
你对属性编辑器的理解有点问题,属性编辑器只需要RegisterPropertyEditor函数与所需要编辑的类的某个属性关联起来,其他的事情都由Delphi来完成,比如你在属性编辑器中修改了值,这个值如何定入到类实例中,这是Delphi来完成的,不需要你去处理,有空的时候我写一个简单的例子给你吧,你给我来信吧,aizb@163.net
 
顶部