怎样在数据库客户程序中输入日期型变量?(100分)

  • 主题发起人 主题发起人 anno
  • 开始时间 开始时间
A

anno

Unregistered / Unconfirmed
GUEST, unregistred user!
如题!
数据库中用10位字符(××××.××.××)表示日期,在前端用什么组件来保证
用户输入的正确性?
DBEdit虽然可以和数据库直接相连,但很难做到象DateTimePicker
一样输入正确的日期!
而DateTimePicker这个组件又不能动态的和数据库相连接。
而采用DBGrid组件显示和输入日期时更难做到。
有什么类似DBEdit组件的日期输入组件吗?
谢谢!
 
如果你不急,我可以试试作一个!
思路:
从TDateTimePicker继承.
 
最简单就是写事件啦. 比如dbedit.OnExit中try strtodate(..) except...end
 
1、服务器上加check或者用trigger保证
2、双击dataset控件,右键点击,选择add all fields ,增加所有field进去,
对你需要限制的field设置 EditMask 属性可以限制该field的输入的字符串
 
我也遇到过同样的问题,我最后没有用Datetimepicker,而是模拟了一个
下拉,我觉得也很方便,你要吗?
 
{
作者:沈前卫 shenqw@cmmail.com 2000-02-28 0:45
转贴请保持本文的完整性
}
unit DBDateTimePicker;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls,DB,dbctrls;

type
TDBDateTimePicker = class(TDateTimePicker)
private
{ Private declarations }
FFieldDataLink:TFieldDataLink;
function GetReadOnly:boolean;
procedure SetReadOnly(Value:boolean);
function GetDataField:string;
procedure SetDataField(Value:string);
function GetDataSource:TDataSource;
procedure SetDataSource(Value:TDataSource);
function GetField:TField;
procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;
procedure CMExit(var Message:TCMExit);message CM_EXIT;
protected
{ Protected declarations }
procedure FDataChange(Sender:TObject);
procedure FUpdateData(Sender:TObject);
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
property Field:TField read GetField;
published
{ Published declarations }
property Anchors;
property BiDiMode;
property CalAlignment;
property CalColors;
property Constraints;
property DataSource:TDataSource read GetDataSource write SetDataSource;
property DataField:string read GetDataField write SetDataField;
property ReadOnly:boolean read GetReadOnly write SetReadOnly;
// The Date, Time, ShowCheckbox, and Checked properties must be in this order:
property ShowCheckbox;
property Checked;
property Color;
property DateFormat;
property DateMode;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property ImeMode;
property ImeName;
property Kind;
property MaxDate;
property MinDate;
property ParseInput;
property ParentBiDiMode;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnClick;
property OnCloseUp;
property OnChange;
property OnDropDown;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnStartDock;
property OnStartDrag;
property OnUserInput;
end;

procedure Register;

implementation

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

{ TDBDateTimePicker }

procedure TDBDateTimePicker.CMExit(var Message: TCMExit);
begin
try
FFieldDataLink.UpdateRecord;
except
Self.SetFocus;
raise;
end;
inherited;
end;

procedure TDBDateTimePicker.CNNotify(var Message: TWMNotify);
begin
FFieldDataLink.Edit;
inherited;
FFieldDataLink.Modified;
end;

constructor TDBDateTimePicker.Create(AOwner: TComponent);
begin
inherited Create(AOwner);

FFieldDataLink:=TFieldDataLink.Create;
FFieldDataLink.ReadOnly:=False;
FFieldDataLink.Control:=Self;
FFieldDataLink.OnDataChange:=FDataChange;
FFieldDataLink.OnUpdateData:=FUpdateData;
end;

destructor TDBDateTimePicker.Destroy;
begin
FFieldDataLink.Free;
FFieldDataLink:=nil;

inherited;
end;

procedure TDBDateTimePicker.FDataChange(Sender: TObject);
begin
if (FFieldDataLink<>nil) and (FFieldDataLink.Field<>nil) then
begin
Self.Date:=Trunc(FFieldDataLink.Field.AsDateTime);
Self.Time:=Frac(FFieldDataLink.Field.AsDateTime);
end;
end;

procedure TDBDateTimePicker.FUpdateData(Sender: TObject);
begin
if (FFieldDataLink<>nil) and (FFieldDataLink.Field<>nil) then
FFieldDataLink.Field.AsDateTime:=TDateTime(Date);
end;

function TDBDateTimePicker.GetDataField: string;
begin
Result:=FFieldDataLink.FieldName;
end;

function TDBDateTimePicker.GetDataSource: TDataSource;
begin
Result:=FFieldDataLink.DataSource;
end;

function TDBDateTimePicker.GetField: TField;
begin
Result:=FFieldDataLink.Field;
end;

function TDBDateTimePicker.GetReadOnly: boolean;
begin
Result:=FFieldDataLink.ReadOnly;
end;

procedure TDBDateTimePicker.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent,Operation);
if (Operation=opReMove) and (FFieldDataLink<>nil) and
(AComponent=FFieldDataLink.DataSource) then
FFieldDataLink.DataSource:=nil;
end;

procedure TDBDateTimePicker.SetDataField(Value: string);
begin
FFieldDataLink.FieldName:=Value;
end;

procedure TDBDateTimePicker.SetDataSource(Value: TDataSource);
begin
FFieldDataLink.DataSource:=Value;
if Value<>nil then Value.FreeNotification(Self);
end;

procedure TDBDateTimePicker.SetReadOnly(Value: boolean);
begin
FFieldDataLink.ReadOnly:=Value;
end;

end.
 
这么麻烦!用其他控件吧!多的是!
 
可以用Woll2woll's InfoPower的控件,
它有TwwDBDateTimePicker控件可以直接和数据库字段绑定,而且有些地方比
DateTimePicker还要好一些。
 
DBEdit和DateTimePicker配合行不行?
 
多人接受答案了。
 
后退
顶部