我的作法通常是在DateTimePicker上覆盖一个edit,但不覆盖其弹出按钮。然后分别定义
两个控件的相关事件.(edit控件edt_dcsj,DateTimePicker控件Dtp_Dcsj),另有一个
Mp_ReplaceStr函数;
procedure Edt_dcsj1exit(Sender: TObject);
var B_Str:string; //系统日期分隔符。
begin
if B_Boiler=0 then exit;
G_String:=DateToStr(Dtp_Dcsj.Date);
B_Str:=iif(pos('-',G_String)<>0,'-',iif(pos('.',G_String)<>0,'.','/'));
Edt_Dcsj.text:=MP_ReplaceStr(Edt_Dcsj.text,'.',B_Str,true,true);
Edt_Dcsj.text:=MP_ReplaceStr(Edt_Dcsj.text,'/',B_Str,true,true);
Edt_Dcsj.Text:=MP_ReplaceStr(Edt_Dcsj.text,'-',B_Str,true,true);
//以上代码根据系统日期分隔符作调整,以适应不同的设置
if isDate(Edt_Dcsj.text) then
Dtp_Dcsj.Date:=StrToDate(Edt_Dcsj.text)
else Edt_Dcsj.text:=iif(trim(Edt_Dcsj.text)='','',DateToStr(Dtp_Dcsj.date));
end;
procedure Edt_DcsjKeyPress(Sender: TObject; var Key: Char);
begin
if B_Boiler>0 then if key=#13 then SelectNext(ActiveControl,true,true)
if (not (key in ['0'..'9'])) and (key<>#8) and (key<>'.') and (key<>'-') and (key<>'/') then key:=#0;
end;
procedure Dtp_DcsjChange(Sender: TObject);
begin
Edt_Dcsj.text:=DateToStr(Dtp_Dcsj.Date);
end;
function MP_ReplaceStr(ss,SubStr,NewStr:string;RepAll,IgCase:Boolean):string;
//查找ss中是否有Substr替换成Newstr
var
a:TReplaceFlags;
begin
if RepAll=true then
include(a,rfReplaceAll);
if IgCase=true then
include(a,rfIgnoreCase);
Result:=stringReplace(ss,Substr,Newstr,a);
end;