请问如何从Rxlib控件包里面的TFilenameEdit控件继承开发为一个数据库控件?(100分)

  • 主题发起人 主题发起人 kingdawn
  • 开始时间 开始时间
K

kingdawn

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是我写的源码,与数据库字段绑定正常,但在DBFilenameEdit的值后,无法使数据库字段字段随之改变,问题出在什么地方,好象应该定义一个通知消息处理过程,但我无法解决,请高手们指点!

unit DBFilenameEdit;

interface

uses
SysUtils, Classes, Controls, StdCtrls, Mask, ToolEdit,Windows,Messages,Forms,
Dialogs,ComCtrls,dbCtrls,db;

type
TDBFilenameEdit = class(TFilenameEdit)
private
{ Private declarations }
FDataLink:TFieldDataLink;
procedure ActiveChange(Sender:Tobject);
procedure DataChange(Sender:Tobject);
procedure UpdateData(Sender:Tobject);
function GetField:TField;
function GetDataField:string;
function GetReadOnly:Boolean;
function GetDataSource:TDataSource;
procedure SetDataField(const Value:string);
procedure SetDataSource(Value:TDataSource);
procedure SetReadOnly(Value:Boolean);
procedure KeyPress(Sender:Tobject;var Key:Char);
protected
{ Protected declarations }
procedure Change;override;
procedure DoExit;override;
property Field:TField read GetField;
property OnChange;
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
published
{ Published declarations }
property DataSource:TDataSource read GetDataSource write SetDataSource;
property ReadOnly:Boolean read GetReadOnly write SetReadOnly;
property DataField:string read GetDataField write SetDataField;
end;

procedure Register;

implementation
uses
CommCtrl;
procedure Register;
begin
RegisterComponents('Sample', [TDBFilenameEdit]);
end;

constructor TDBFilenameEdit.Create(AOwner:TComponent);
begin
inherited;
FDataLink:=TFieldDataLink.Create;
with FDataLink do
begin
Control:=self;
OnActiveChange:=ActiveChange;
OnDataChange:=DataChange;
OnUpdateData:=UpdateData;
OnKeyPress:=KeyPress;
end;
end;

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

procedure TDBFilenameEdit.Change;
begin
if FDataLink.Field=Nil then
begin
with FDataLink do
begin
Edit;
Modified;
end;
end;
inherited;
end;

procedure TDBFilenameEdit.SetDataSource(Value:TDataSource);
begin
FDataLink.DataSource:=Value;
end;

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

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

function TDBFilenameEdit.GetReadOnly:Boolean;
begin
Result:=FDataLink.ReadOnly;
end;

procedure TDBFilenameEdit.SetReadOnly(Value:Boolean);
begin
FDataLink.ReadOnly:=Value;
end;

function TDBFilenameEdit.GetField:TField;
begin
Result:=FDataLink.Field;
end;

function TDBFilenameEdit.GetDataSource;
begin
Result:=FDataLink.DataSource;
end;

procedure TDBFilenameEdit.DoExit;
begin
FDataLink.UpdateRecord;
inherited;
end;

procedure TDBFilenameEdit.ActiveChange;
begin
if FDataLink.Active then
begin
if FDataLink.Field<>nil then
begin
Self.Text := FDataLink.Field.Text;
end;
end
else
begin
Self.Text:='';
end;
end;

procedure TDBFilenameEdit.DataChange;
begin
with FDataLink,Field do
if Field<>nil then
begin
Self.Text := FDataLink.Field.Text;
end;
end;
procedure TDBFilenameEdit.UpdateData;
begin
FDataLink.Field.AsString:=Self.Text;
end;

procedure TDBFilenameEdit.KeyPress(Sender:TObject;var Key:Char);
begin
if key=#13 then perform(WM_NEXTDLGCTL,0,0);
end;

end.
 
我想问题的关键
应该是在
type
procedure CMExit(var Message: TCMExit); message CM_EXIT;



procedure TDBFilenameEdit.CMExit(var Message: TCMExit);
begin

FDataLink.Edit;
FDataLink.Field.AsString:=text;{将对应的值写入字段,我用text代替了}

DoExit;
end;
 
对不起,是我有个函数搞错掉了,应修改为:
procedure TDBFilenameEdit.Change;
begin
//原为:
//if FDataLink.Field=Nil then
if FDataLink.Field<>Nil then
begin
with FDataLink do
begin
Edit;
Modified;
end;
end;
inherited;
end;

但现在还存在一点问题:
1、程序编译时警告两个函数
function GetReadOnly:Boolean;
procedure KeyPress(Sender:Tobject;var Key:Char);
直接去掉行不行?
2、在程序中调用时,关联上数据库字段后,在重新定位一个新文件位置时,第一次总是不能修改相应的地址,需第二次才会得到新的地址,不知为何?
请继续赐教!
 
经测试还发现一个问题,就是在TDBFilenameEdit.Change中,在数据集记录移动时也会引起FDataLink状态的改变,这就不符合要求了,请教怎么解决?
 
后退
顶部