如何写出象ttable, tadotable ,tquery,tadoquery 等控件一样的自动下拉显示field属性
delphi vcl在delphi 中有很多源码,但是有些关键的属性编辑器却找不到,下面我将举几个例子来说明一下
我们可以研究一下tdbedit的两个关键属性一个datasource,datafield.
示例如下
ttestcontrol=class(twincontrol)
private
fdatalink: tfielddatalink; //定义一个存储字段的属性
function getdatafield: string;
function getdatasource: tdatasource;
procedure setdatafield(const value: string);
procedure setdatasource(const value: tdatasource);
published
//下面说明时要用到
//property resultfield :string read getdatafield write setdatafield;
property datafield: string read getdatafield write setdatafield;
property datasource: tdatasource read getdatasource write setdatasource;
end;
//获取字段名
function ttestcontrol.getdatafield: string;
begin
result := fdatalink.fieldname; //
end;
//获取数据源
function ttestcontrol.getdatasource: tdatasource;
begin
result := fdatalink.datasource;
end;
//设置字段名
procedure ttestcontrol.setdatafield(const value: string);
begin
if not (csdesigning in componentstate) then
resetmaxlength;
fdatalink.fieldname := value;
end;
//设置数据源
procedure ttestcontrol.setdatasource(const value: tdatasource);
begin
if not (fdatalink.datasourcefixed and (csloading in componentstate)) then
fdatalink.datasource := value;
if value <> nil then
value.freenotification(self);
end;
当实现完这样的控件后,只要你在界面放置了一个tadoquery,并且把tadoquery打开,
你给ttestcontrol指定了datasource之后,你会发现datafield已经可以下拉,并不需要
写什么属性编辑器,这是因为delphi内属性编辑器注册是针对所有delphi控件的,所有
符合datasource,datafield的属性规范的都自动可以了。如果你把上述控件中的名字改了,
注释掉的部分,datafield改为resultfield你就不能自动下拉了,就只能输入字符串(其实还是有属性编辑器的,至少是字符串嘛),这就需要自己编写属性编辑器,且注册给自己的控件
我写出如下一个属性编辑器
tlistfieldproperty = class(tstringproperty) //继承于tstringproperty,因为fieldname是字符串
public
function getattributes: tpropertyattributes; override;
procedure getvaluelist(list: tstrings); virtual;
procedure getvalues(proc: tgetstrproc); override;
function getdatasourcepropname: string; virtual;
end;
{ tlistfieldproperty }
function tlistfieldproperty.getattributes: tpropertyattributes;
begin
result := [pavaluelist, pasortlist, pamultiselect];
end;
function tlistfieldproperty.getdatasourcepropname: string;
begin
result := 'datasource'; //指定字段下拉列表到什么属性里取值
end;
procedure tlistfieldproperty.getvaluelist(list: tstrings);
var
datasource: tdatasource;
begin
//从上述getdatasourcepropname方法指定的属性去取出所有字段用来下拉显示
//因为在本控件中定义的属性是tdatasource 所以这里用tdatasource, 如果是tadoquery, 或者其他派生于
//tdataset类的这里就只需要点定义一个dataset 而不需要tdatasource了
datasource := getpropertyvalue(getcomponent(0), getdatasourcepropname) as tdatasource;
if (datasource <> nil) and (datasource.dataset <> nil) then
datasource.dataset.getfieldnames(list);
end;
procedure tlistfieldproperty.getvalues(proc: tgetstrproc);
var
i: integer;
values: tstringlist;
begin
//用来显示列表
values := tstringlist.create;
try
getvaluelist(values);
for i := 0 to values.count - 1 do proc(values[i]);
finally
values.free;
end;
end;
属性编辑器已经写好,
我们需来注册这个属性编辑器,注册代码如下
registerpropertyeditor(typeinfo(string), ttestcontrol, 'resultfield', tlistfieldproperty);
另外还可以给你注册的属性加一个归类categoryname,我们把上述resultfield属性指定为database型属性,注册示例如下
:registerpropertiesincategory('database', ttestcontrol, ['resultfield']);
假设上面的控件ttestcontrol没有datasource属性,那也不能成功的, 所以在属性编辑器方法内指定字段来源哪个数据源,假设你的字段是listsource属性,那这样指定
function tlistfieldproperty.getdatasourcepropname: string;
begin
result := 'listsource'; //指定字段下拉列表到什么属性里取值
end;
相信到这里,有很多朋友举一反三了,实现数据感知控件已经不在话下了, 因为难点就在这儿,其他代码在delphi里可
以找到
下一次,我将讲另一个难点,关于集合的属性编辑器编写,比如dbgrid的tcolumn风格