//以下是时间日期敏感控件的代码,你看看应该会明白如何用constructor
unit DBDateTimePicker;
{
************************
TDBDateTimePicker - v1.0
************************
The TDBDateTimePicker is a simple component descended from TDateTimePicker.
When I installed Delphi 3 I noticed that the TDateTimePicker wasn't data aware
and that's what I need. I wrote it and all other Delphi developers can now be
saved some hassle. This is a simple unit and carries no warranties,
expressed or implied. Use it at your own risk.
Installation
------------
Add this unit (DBDateTimePciker.pas' to any of your Delphi 3 packages.
Current Limitations
-------------------
Currently TDBDateTimePicker only works with dates, not time or DateTime. If
someone get's around to implementing it before I do, I'd appreciate a copy.
Written By
----------
Simon Munro
Paradoxical Business Solutions
simon@delphi.co.za
www.delphi.co.za
Check my web site for later versions
Copyright ?1997 Paradoxical Business Solutions
Please ensure that this comment section remains unchanged.
}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, DB, DBCtrls;
type
TDBDateTimePicker = class(TDateTimePicker)
private
{ Private declarations }
FAllowChange: Boolean;
FDataLink: TFieldDataLink;
function GetDataField: string;
function GetDataSource: TDataSource;
procedure SetDataField(const Value: string);
procedure SetDataSource(Value: TDataSource);
procedure DataChange(Sender: TObject);
procedure Change(Sender: TObject);
procedure CMExit(var Message: TWMNoParams)
message CM_EXIT;
protected
{ Protected declarations }
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;
end;
procedure Register;
implementation
constructor TDBDateTimePicker.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDataLink:=TFieldDataLink.Create;
FDataLink.OnDataChange:=DataChange;
OnChange:=Change;
FAllowChange:=True;
end;
destructor TDBDateTimePicker.Destroy;
begin
FDataLink.OnDataChange:=nil;
FDataLink.Free;
inherited Destroy;
end;
procedure TDBDateTimePicker.DataChange(Sender: TObject);
begin
if FDataLink.Field=nil then Self.Date:=0
else
if FAllowChange then Self.Date:=FDataLink.Field.AsDateTime;
end;
procedure TDBDateTimePicker.Change(Sender: TObject);
begin
with FDataLink do begin
FAllowChange:=False;
if not editing then Edit;
end;
FDatalink.Field.AsDateTime:=self.Date;
FAllowChange:=True;
end;
procedure TDBDateTimePicker.CMExit(var Message: TWMNoParams);
begin
try
FDataLink.UpdateRecord;
except
on Exception do SetFocus;
end;
inherited;
end;
function TDBDateTimePicker.GetDataField: string;
begin
Result:=FDataLink.FieldName;
end;
function TDBDateTimePicker.GetDataSource: TDataSource;
begin
Result:=FDataLink.DataSource;
end;
procedure TDBDateTimePicker.SetDataField(const Value: string);
begin
FDataLink.FieldName:=Value;
end;
procedure TDBDateTimePicker.SetDataSource(Value: TDataSource);
begin
FDataLink.DataSource:=Value;
end;
procedure Register;
begin
RegisterComponents('Data Controls', [TDBDateTimePicker]);
end;
end.