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.
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.