TO gutian:
不太明白你的意思,现贴上代码(从TDateTimePicker继承,可以与DataSet中的日期字段
连接),有空指点一二.
TO bbkxjy:
我只是要显示连接的DataSource中的下拉字段好像不用那么麻烦吧!
代码如下:
unit DateTimePickerTim;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, db, dbCtrls;
type
TDateTimePickerTim = class(TDateTimePicker)
private
FTimDataLink:TFieldDataLink;
function GetTimDataField: string;
function GetTimDataSource: TDataSource;
procedure SetTimDataField(const Value: string);
procedure SetTimDataSource(const Value: TDataSource);
procedure TimDataChange(Sender:TObject);
procedure TimUpdateData(Sender:TObject);
procedure CMExit(var msg:TWMNoParams);message CM_EXIT;
protected
procedure Change;override;
public
constructor Create(aOwner:TComponent);override;
destructor Destroy;override;
published
property TimDataField:string read GetTimDataField write SetTimDataField;
property TimDataSource:TDataSource read GetTimDataSource write SetTimDataSource;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('TimDatabase', [TDateTimePickerTim]);
end;
{ TDateTimePickerTim }
constructor TDateTimePickerTim.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
FTimDataLink:=TFieldDataLink.Create;
FTimDataLink.OnDataChange:=TimDataChange;
FTimDataLink.OnUpdateData:=TimUpdateData;
end;
procedure TDateTimePickerTim.TimDataChange(Sender: TObject);
begin
if FTimDataLink.Field=nil then
Date:=0
else
Date:=FTimDataLink.Field.AsDateTime;
end;
destructor TDateTimePickerTim.Destroy;
begin
FTimDataLink.OnDataChange:=nil;
FTimDataLink.OnUpdateData:=nil;
FTimDataLink.Free;
inherited Destroy;
end;
function TDateTimePickerTim.GetTimDataField: string;
begin
result:=FTimDataLink.FieldName;
end;
function TDateTimePickerTim.GetTimDataSource: TDataSource;
begin
result:=FTimDataLink.DataSource;
end;
procedure TDateTimePickerTim.SetTimDataField(const Value: string);
begin
FTimDataLink.FieldName:=value;
end;
procedure TDateTimePickerTim.SetTimDataSource(const Value: TDataSource);
begin
FTimDataLink.DataSource:=value;
end;
procedure TDateTimePickerTim.TimUpdateData(Sender: TObject);
var
MyDate:TDate;
begin
MyDate:=StrToDate(FormatDateTime('yyyy-m-d', DateTime));
FTimDataLink.Edit;
FTimDataLink.Field.AsDateTime:=MyDate;
end;
procedure TDateTimePickerTim.Change;
begin
FTimDataLink.Modified;
inherited Change;
end;
procedure TDateTimePickerTim.CMExit(var msg: TWMNoParams);
begin
try
FTimDataLink.UpdateRecord;
except
on Exception do SetFocus;
end;//try
inherited;
end;
end.