144 lines
3.4 KiB
ObjectPascal
144 lines
3.4 KiB
ObjectPascal
unit L_DBDateTime;
|
||
|
||
interface
|
||
|
||
uses
|
||
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,db,dbtables,dbctrls,Dialogs,
|
||
ComCtrls;
|
||
|
||
type
|
||
TDBDateTime = class(TDateTimePicker)
|
||
private
|
||
FDataLink:TFieldDataLink;
|
||
//TFieldDataLink是TDataLink的派生类,
|
||
// 处理单个字段与DataSource的交互
|
||
procedure DataChange(sender:Tobject);
|
||
//当DataSet的记录改变(如浏览记录)
|
||
// 时触发OnDataChange
|
||
// 事件,DataChange将作为该事件的事件处理句柄
|
||
procedure UpdateData(sender:Tobject);
|
||
//更新DataSet前触发OnUpdateData事件,
|
||
// UpdateData将作为该事件的事件处理句柄
|
||
function GetDataSource:TDataSource;
|
||
procedure SetdataSource(value:TDataSource);
|
||
Function GetDataField:String;
|
||
procedure SetdataField(Value:String);
|
||
procedure CMexit(var Message:TCMExit);
|
||
message CM_EXIT;//当控件失去焦点时触发CM_EXIT消息
|
||
protected
|
||
procedure Change;override;//控件中日期、
|
||
// 时间改变时触发OnChange事件
|
||
procedure Notification(AComponent:TComponent;
|
||
Operation:Toperation);override;
|
||
//当某一控件从FORM上移走时DELPHI的
|
||
// IDE调用该方法通知其它控件
|
||
public
|
||
constructor Create(AOwner:Tcomponent);override;
|
||
destructor Destroy;override;
|
||
published
|
||
property DataSource:TDataSource read GetDataSource
|
||
write SetDataSource;//为控件增加DataSource属性,
|
||
// 使它能与DataSource构件连接
|
||
property DataField:String read GetDataField
|
||
write SetDataField;
|
||
end;//为控件增加DataField属性,
|
||
// 使它指向代表某一字段的TField对象
|
||
|
||
procedure Register;//注册构件
|
||
|
||
implementation
|
||
|
||
procedure TDBDateTime.CMExit;
|
||
begin
|
||
try
|
||
FDataLink.UpdateRecord;
|
||
//控件失去焦点时更新DataSet,
|
||
// 这将触发OnUpdateData事件
|
||
except
|
||
Setfocus;
|
||
raise;
|
||
end;
|
||
DoExit;
|
||
end;
|
||
|
||
constructor TDBDateTime.Create(Aowner:Tcomponent);
|
||
begin
|
||
inherited Create(Aowner);
|
||
//创建DataLink对象,挂接OnDataChange、
|
||
//OnUpdateData事件处理句柄
|
||
FDataLink:=TFieldDataLInk.Create;
|
||
FDataLink.OnDataChange:=DataChange;
|
||
FDataLink.OnUpdateData:=Updatedata;
|
||
end;
|
||
|
||
Destructor TDBDateTime.Destroy;
|
||
begin
|
||
FDataLink.OnDataChange:=nil;
|
||
FDataLink.OnUpdateData:=nil;
|
||
FDataLink.Free;
|
||
inherited Destroy;
|
||
end;
|
||
|
||
function TDBDateTime.GetdataSource:TdataSource;
|
||
begin
|
||
result:=FDataLink.DataSource;
|
||
end;
|
||
|
||
Procedure TDBDateTime.SetDataSource
|
||
(Value:TDataSource);
|
||
begin
|
||
FDataLink.DataSource:=Value;
|
||
end;
|
||
|
||
function TDBDateTime.GetDatafield:String;
|
||
begin
|
||
result:=FDataLink.FieldName;
|
||
end;
|
||
|
||
procedure TDBDateTime.SetDataField(value:String);
|
||
begin
|
||
FdataLink.FieldName:=value;
|
||
end;
|
||
|
||
procedure TDBDateTime.DataChange(Sender:Tobject);
|
||
begin
|
||
DateTime:=now;
|
||
//若控件连了活动的DataSet则数据集变动时
|
||
//控件显示当前记录的相应字段值
|
||
if FDataLink.Field<>nil then
|
||
if FDataLink.Field.Text<>'' then
|
||
DateTime:=FDatalink.Field.AsDateTime;
|
||
end;
|
||
|
||
Procedure TDBDateTime.UpdateData(sender:Tobject);
|
||
begin
|
||
FDatalink.Field.AsDateTime:=DateTime;
|
||
//用控件中的日期、时间更新相应字段
|
||
end;
|
||
|
||
procedure TDBDateTime.Change;
|
||
begin
|
||
//当用户改变了控件中的内容时将DataSet置为编辑状态
|
||
FDataLink.Modified;
|
||
if not FDataLink.Editing then
|
||
FdataLink.Edit;
|
||
inherited Change;
|
||
end;
|
||
procedure TDBDateTime.Notification
|
||
(AComponent:TComponent;Operation:TOperation);
|
||
begin
|
||
inherited Notification(Acomponent,Operation);
|
||
//当与控件相连的TdataSource
|
||
// 被删除时将控件的DataSource属性置为空
|
||
if (Operation=opRemove) and (FDataLink<>nil)
|
||
and (AComponent=Datasource) then
|
||
DataSource:=nil;
|
||
end;
|
||
|
||
procedure Register;
|
||
begin
|
||
RegisterComponents('Data Controls',[TDBDateTime]);
|
||
end;
|
||
|
||
end.
|