VCL(四) 如何写出象TTable, TAdoTable ,Tquery,TAdoQuery 等控件一样的自动下拉显示Field属性

如何写出象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风格

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值