如何定义组件的属性让它可以选择返加字段名 ( 积分: 32 )

  • 主题发起人 主题发起人 hfydiy
  • 开始时间 开始时间
H

hfydiy

Unregistered / Unconfirmed
GUEST, unregistred user!
想自己做一个控件属性如
DataSource
DataField1
.....
DataField20
要求:DataField1-DataField20能像TDBText的DataField属性一样可以选择字段名。
如何实现。
 
想自己做一个控件属性如
DataSource
DataField1
.....
DataField20
要求:DataField1-DataField20能像TDBText的DataField属性一样可以选择字段名。
如何实现。
 
做数据感知控件有点麻烦的
 
帮你顶一个
 
参考一下如TDBEdit之类的数据敏感控件的源码。
 
TFieldDataLink 就可以了
==========================
unit DBBitBtn1;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Buttons,
DBCtrls, DB;

type
TDBBitBtn1 = class(TBitBtn)
private
{ Private declarations}
FDataLink: TFieldDataLink;
function GetDataField: string;
procedure SetDataField(const Value: string);
function GetDataSource: TDataSource;
procedure SetDataSource(const Value: TDataSource);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property DataField: string read GetDataField write SetDataField;
property DataSource: TDataSource read GetDataSource write SetDataSource;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TDBBitBtn1]);
end;

{ TDBBitBtn1 }

constructor TDBBitBtn1.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
end;

destructor TDBBitBtn1.Destroy;
begin
FDataLink.Free;
FDataLink := nil;
inherited Destroy;
end;

function TDBBitBtn1.GetDataField: string;
begin
Result := FDataLink.FieldName;
end;

function TDBBitBtn1.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;

procedure TDBBitBtn1.SetDataField(const Value: string);
begin
FDataLink.FieldName := Value;
end;

procedure TDBBitBtn1.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;

end.
 
jfyes:没有达到我要的效果,你把DataField属性改成其它属性名就不行了。而且我要求的是多个,如果只要一个这样的属性我还不如这样定义:
unit aaComponent;

interface

uses
SysUtils, Classes,Db,AdoDb;

type
TaaComponent = class(TComponent)
private
{ Private declarations }
fDataSource:TDataSource;
f1: string;
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
property DataSource:TDataSource read fDataSource write fDataSource;
property DataField:String read f1 write f1 ;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('HFY', [TaaComponent]);
end;

end.
 
ICQ:49896449
 
后退
顶部