unit EVDBDateTimePicker;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
ComCtrls,dbctrls,db;
type
TEVDBDateTimePicker = class(TDateTimePicker)
private
FDataLink : TFieldDataLink;
FCloseUP : TNotifyEvent;
function GetReadOnly: Boolean;
procedure SetReadOnly(Value: Boolean);
function GetDataField: string;
function GetDataSource: TDataSource;
procedure SetDataField(const Value : string);
procedure SetDataSource(Value: TDatasource);
procedure DataChange(Sender: TObject);
procedure UpdateData(Sender: TObject);
procedure CloseUp(Sender: TObject);
procedure CMExit (var Message: TWMNoParams); message CM_EXIT;
procedure CMGetDataLink(var Message: TMessage); message CM_GETDATALINK;
{ Private declarations }
protected
procedure KeyDown ( var Key: word; shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
procedure Change; override;
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function ExecuteAction(Action: TBasicAction): Boolean; override;
function UpdateAction(Action: TBasicAction): Boolean; override;
{ Public declarations }
published
Property Caption;
property Datasource : TDataSource read GetDatasource write SetDatasource;
property DataField: string read GetDataField write SetDataField;
property ReadOnly: Boolean read GetReadOnly write SetReadOnly default False;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Evon', [TEVDBDateTimePicker]);
end;
constructor TEVDbDateTimePicker.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FDataLink.OnDataChange := DataChange;
FDataLink.OnUpdateData := UpdateData;
FCloseUP := OnCloseUp;
OnCloseUp := CloseUP;
end;
destructor TEVDbDateTimePicker.Destroy;
begin
FDataLink.OnDataChange:=nil;
FDataLink.Free;
inherited Destroy;
end;
function TEVDbDateTimePicker.GetReadOnly: Boolean;
begin
Result := FDataLink.ReadOnly;
end;
procedure TEVDbDateTimePicker.SetReadOnly(Value: Boolean);
begin
FDataLink.ReadOnly := Value;
end;
function TEVDbDateTimePicker.GetDataField: string;
begin
Result:=FDataLink.fieldname;
end;
function TEVDbDateTimePicker.GetDataSource:TDataSource;
begin
Result:=FDataLink.DataSource;
end;
Procedure TEVDbDateTimePicker.SetDataField(const value: string);
begin
FDataLink.FieldName:=Value;
end;
procedure TEVDbDateTimePicker.SetDataSource(Value: TDataSource);
begin
FDataLink.DataSource := Value;
if Value <> nil then Value.FreeNotification(Self);
end;
procedure TEVDbDateTimePicker.DataChange(Sender: TObject);
begin
if FDataLink.field = nil then dateTime :=0
else
if FDataLink.Field.AsVariant <> null then
DateTime:=FDataLink.Field.AsDateTime;
end;
procedure TEVDbDateTimePicker.UpdateData(Sender: TObject);
begin
if FDataLink.CanModify then FDataLink.Field.AsDateTime:=Datetime;
end;
procedure TEVDbDateTimePicker.Change;
begin
if FDataLink.Editing then
begin
UPdateData(Self);
FDataLink.modified;
end;
inherited;
end;
procedure TEVDbDateTimePicker.KeyDown(var Key: word; Shift:TshiftState);
begin
inherited KeyDown(Key, Shift);
if (Key = VK_DELETE) or ((Key = VK_INSERT) and (ssShift in Shift)) then
FDataLink.Edit;
end;
procedure TEVDbDateTimePicker.KeyPress(var Key: Char);
begin
if FDataLink.CanModify then
begin
inherited KeyPress(Key);
case Key of
^H, ^V, ^X, #32..#255:FDataLink.Edit;
#27:
begin
FDataLink.Reset;
Key := #0;
end;
end;
end else Key := #0;
end;
procedure TEVDbDateTimePicker.CMExit(Var Message: TWMNoParams);
begin
try
FdataLink.UpdateRecord;
except
SetFocus;
Raise;
end;
inherited;
end;
procedure TEVDbDateTimePicker.CloseUp(Sender: TObject);
begin
if Not FDataLink.CanModify then FdataLink.Reset
else FdataLink.Edit;
if Assigned(FCloseUP) then FCloseUP(Self);
end;
function TEVDbDateTimePicker.ExecuteAction(Action: TBasicAction): Boolean;
begin
Result := inherited ExecuteAction(Action) or (FDataLink <> nil) and
FDataLink.ExecuteAction(Action);
end;
function TEVDbDateTimePicker.UpdateAction(Action: TBasicAction): Boolean;
begin
Result := inherited UpdateAction(Action) or (FDataLink <> nil) and
FDataLink.UpdateAction(Action);
end;
procedure TEVDbDateTimePicker.CMGetDataLink(var Message: TMessage);
begin
Message.Result := Integer(FDataLink);
end;
end.
请给分! 谢谢!