收藏一个 DBDateTimePicker

qufo 2007-08-03 11:00:00
unit DBDateTime;

interface

uses
SysUtils, Classes, Controls, ComCtrls,DB,DBCtrls;

type
TDBDateTime=class(TDateTimePicker)
private
FDataLink:TFieldDataLink;
procedure DataChange(sender:Tobject);
procedure UpdateData(sender:Tobject);
function GetDataSource:TDataSource;
procedure SetdataSource(value:TDataSource);
Function GetDataField:String;
procedure SetdataField(Value:String);
procedure CMexit(var Message:TCMExit); message CM_EXIT;
protected
procedure Change;override;
procedure Notification(AComponent:TComponent;Operation:Toperation);override;
public
constructor Create(AOwner:Tcomponent);override;
destructor Destroy;override;
published
property DataSource:TDataSource read GetDataSource write SetDataSource;
property DataField:String read GetDataField write SetDataField;
end;
procedure Register;

implementation

procedure TDBDateTime.CMExit;
begin
try
FDataLink.UpdateRecord;
except
Setfocus;
raise;
end;
DoExit;
end;

constructor TDBDateTime.Create(Aowner:Tcomponent);
begin
inherited Create(Aowner);
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;
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
FDataLink.Modified;
if not FDataLink.Editing then
FdataLink.Edit;
inherited Change;
end;

procedure TDBDateTime.Notification(AComponent:
TComponent;Operation:TOperation);
begin
inherited Notification(Acomponent,Operation);
if(Operation=opRemove)and(FDataLink<>nil) and(AComponent=Datasource)then
DataSource:=nil;
end;

procedure Register;
begin
RegisterComponents('Data Controls', [TDBDateTime]);
end;

end.
...全文
102 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
dernge 2012-01-04
  • 打赏
  • 举报
回复
好东西,十分感谢。

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧