C
chinaceleron
Unregistered / Unconfirmed
GUEST, unregistred user!
我照着网上的例子写了一个数据感知的Datetimepicker控件,但是遇到了一个问题,就是这个控件显示的今天的日期,但实际是空值,如果你不去选择一下日期,当提交时,会发现它连接的字段是null,我怎样让这个控件一生成就有一个默认值,比如今天的日期,这样即使我不操作这个控件,当提交时,和这个控件相连的字段保存今天的日期。
请各位高手给指导,谢谢,我的分不多了,全送了!
下面是这个控件的源代码
unit DBDateEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls,dbctrls,db;
type
TDBDateEdit = class(TDateTimePicker)
private
{ Private declarations }
FDataLink:TFieldDataLink;
FReadOnly : boolean;
function GetReadOnly:Boolean;
function GetDataField:String;
function GetDataSource:TDataSource;
procedure SetDataField(const Value: String);
procedure SetReadOnly(Value: Boolean);
procedure SetDataSource(Value: TDataSource);
procedure DataChange(Sender: TObject);
procedure UpdateData(Sender: TObject);
procedure CMExit (var Messages : TCMExit );message CM_Exit;
procedure WMChar(var Message: TWMChar); message WM_CHAR;
protected
{ Protected declarations }
procedure Change; override;
procedure Notification(AComponent:TComponent;
Operation:Toperation);override;
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;
property JsReadOnly: Boolean read GetReadOnly write SetReadOnly ;
property date:datetime read
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Data Controls', [TDbdateEdit]);
end;
Constructor TDBDateEdit.Create(AOwner:Tcomponent);
begin
Inherited Create(AOwner);
FReadOnly:=False;
FDataLink:=TFieldDataLink.Create;
FDataLink.OnUpdateData := UpdateData;
FDataLink.OnDataChange := DataChange;
end;
Destructor TDBDateEdit.Destroy;
begin
FDataLink.OnDataChange:=nil;
FDataLink.OnUpdateData:=nil;
FDataLink.Free;
FDataLink := nil;
Inherited Destroy;
end;
function TDBDateEdit.GetDataField : String;
begin
Result := FdataLink.FieldName;
end;
function TDBDateEdit.GetReadOnly : Boolean;
begin
Result := FReadOnly;
end;
function TDBDateEdit.GetDataSource : TDataSource;
begin
Result := FdataLink.DataSource;
end;
procedure TDBDateEdit.SetDataField(const Value: String);
begin
Fdatalink.FieldName := Value;
end;
procedure TDBDateEdit.SetDataSource(Value: TDataSource);
begin
Fdatalink.DataSource := Value;
end;
procedure TDBDateEdit.SetReadOnly(Value: Boolean);
begin
if Value <> FReadOnly Then
FReadOnly := Value;
end;
procedure TDBDateEdit.DataChange(Sender: TObject);
{var
JDateTime:TDateTime;
LastTime:TSystemTime;}
begin
inherited;
{ JDateTime:=0;
GetSystemTime(LastTime);
JDateTime:=SystemTimeToDateTime(LastTime);
}
If FdataLink.Field = nil then
begin
if Kind = dtkDate then
Date:=SysUtils.date
else
Time:=SysUtils.Time
end
else
begin
if FdataLink.Field.AsDateTime<> 0 then
begin
if Kind = dtkDate then
Date :=FdataLink.Field.AsDateTime
else
Time:=FdataLink.Field.AsDateTime
end
else
begin
if kind = dtkDate then
Date:=SysUtils.date
else
Time:=SysUtils.Time;
end
end;
end;
procedure TDBDateEdit.UpdateData(Sender: TObject);
begin
if Fdatalink <> nil then
begin
if kind = dtkDate then
FdataLink.Field.AsString:=DateToStr(date)
else
FdataLink.Field.AsString:=TimeToStr(Time);
end;
end;
procedure TDBDateEdit.Change;
begin
if Assigned(Fdatalink) and FReadOnly = False then
begin
Fdatalink.Edit;
FdataLink.Modified;
end;
inherited;
end;
procedure TDBDateEdit.CMExit (var Messages : TCMExit );
begin
if Assigned(Fdatalink) and FReadOnly = False then
begin
try
//控件失去焦点时更新DataSet,这将触发OnUpdateData事件
FDataLink.UpdateRecord;
except
Setfocus;
raise;
end;
end;
DoExit;
inherited;
end;
procedure TDBDateEdit.WMChar(var Message: TWMChar);
begin
if Chr(Message.CharCode) = #13 then
begin
postmessage(Handle,WM_KeyDown,VK_TAB,0);
end;
end;
procedure TDBDateEdit.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;
end.
请各位高手给指导,谢谢,我的分不多了,全送了!
下面是这个控件的源代码
unit DBDateEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls,dbctrls,db;
type
TDBDateEdit = class(TDateTimePicker)
private
{ Private declarations }
FDataLink:TFieldDataLink;
FReadOnly : boolean;
function GetReadOnly:Boolean;
function GetDataField:String;
function GetDataSource:TDataSource;
procedure SetDataField(const Value: String);
procedure SetReadOnly(Value: Boolean);
procedure SetDataSource(Value: TDataSource);
procedure DataChange(Sender: TObject);
procedure UpdateData(Sender: TObject);
procedure CMExit (var Messages : TCMExit );message CM_Exit;
procedure WMChar(var Message: TWMChar); message WM_CHAR;
protected
{ Protected declarations }
procedure Change; override;
procedure Notification(AComponent:TComponent;
Operation:Toperation);override;
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;
property JsReadOnly: Boolean read GetReadOnly write SetReadOnly ;
property date:datetime read
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Data Controls', [TDbdateEdit]);
end;
Constructor TDBDateEdit.Create(AOwner:Tcomponent);
begin
Inherited Create(AOwner);
FReadOnly:=False;
FDataLink:=TFieldDataLink.Create;
FDataLink.OnUpdateData := UpdateData;
FDataLink.OnDataChange := DataChange;
end;
Destructor TDBDateEdit.Destroy;
begin
FDataLink.OnDataChange:=nil;
FDataLink.OnUpdateData:=nil;
FDataLink.Free;
FDataLink := nil;
Inherited Destroy;
end;
function TDBDateEdit.GetDataField : String;
begin
Result := FdataLink.FieldName;
end;
function TDBDateEdit.GetReadOnly : Boolean;
begin
Result := FReadOnly;
end;
function TDBDateEdit.GetDataSource : TDataSource;
begin
Result := FdataLink.DataSource;
end;
procedure TDBDateEdit.SetDataField(const Value: String);
begin
Fdatalink.FieldName := Value;
end;
procedure TDBDateEdit.SetDataSource(Value: TDataSource);
begin
Fdatalink.DataSource := Value;
end;
procedure TDBDateEdit.SetReadOnly(Value: Boolean);
begin
if Value <> FReadOnly Then
FReadOnly := Value;
end;
procedure TDBDateEdit.DataChange(Sender: TObject);
{var
JDateTime:TDateTime;
LastTime:TSystemTime;}
begin
inherited;
{ JDateTime:=0;
GetSystemTime(LastTime);
JDateTime:=SystemTimeToDateTime(LastTime);
}
If FdataLink.Field = nil then
begin
if Kind = dtkDate then
Date:=SysUtils.date
else
Time:=SysUtils.Time
end
else
begin
if FdataLink.Field.AsDateTime<> 0 then
begin
if Kind = dtkDate then
Date :=FdataLink.Field.AsDateTime
else
Time:=FdataLink.Field.AsDateTime
end
else
begin
if kind = dtkDate then
Date:=SysUtils.date
else
Time:=SysUtils.Time;
end
end;
end;
procedure TDBDateEdit.UpdateData(Sender: TObject);
begin
if Fdatalink <> nil then
begin
if kind = dtkDate then
FdataLink.Field.AsString:=DateToStr(date)
else
FdataLink.Field.AsString:=TimeToStr(Time);
end;
end;
procedure TDBDateEdit.Change;
begin
if Assigned(Fdatalink) and FReadOnly = False then
begin
Fdatalink.Edit;
FdataLink.Modified;
end;
inherited;
end;
procedure TDBDateEdit.CMExit (var Messages : TCMExit );
begin
if Assigned(Fdatalink) and FReadOnly = False then
begin
try
//控件失去焦点时更新DataSet,这将触发OnUpdateData事件
FDataLink.UpdateRecord;
except
Setfocus;
raise;
end;
end;
DoExit;
inherited;
end;
procedure TDBDateEdit.WMChar(var Message: TWMChar);
begin
if Chr(Message.CharCode) = #13 then
begin
postmessage(Handle,WM_KeyDown,VK_TAB,0);
end;
end;
procedure TDBDateEdit.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;
end.