if (selstartsave = 1) and (textsave[1] = '2') and
(not (key in ['0'..'3'])) then charvalid := false;
end;
2: if (selstartsave = 3) and not (key in ['0'..'5']) then charvalid := false;
3: if (selstartsave = 6) and not (key in ['0'..'5']) then charvalid := false;
end;
if not charvalid then begin
key:=#0;exit;
end;
if dts<>nil then dts.dataset.edit;
if not (selstartsave in [2,5]) then textsave[selstartsave+1]:=key;
if sellengthsave>1 then begin
for i:=selstartsave+2 to selstartsave+sellengthsave do
if i in [1,2,4,5,7,8] then textsave[i]:='0';
sellengthsave:=1;
end;
for i:=1 to length(textsave) do
if (i in [1,2,4,5,7,8]) and (not (textsave[i] in numchars ) ) then
textsave[i]:='0';
////////////////////////////////////
if selstartsave in [1,4] then
selstartsave :=selstartsave+2
else if selstartsave=length(textsave)-1 then
selstartsave :=selstartsave
else selstartsave :=selstartsave+1;
/////////////////////////////////////
ctl.text :=textsave;
ctl.selstart :=selstartsave;
ctl.sellength :=sellengthsave;
key:=#0;
end;
//此函数分割时间,因为有时候会遇到非法的时间字符串,所以不采用decodetime。
function mxsplitstr(sourcestr,splitstr:string;var resultarray:array of string):integer;
var
i:integer;
strtmp:string;
begin
strtmp:=sourcestr;
i:=0;
while pos(splitstr,strtmp)>0 do begin
resultarray[i]:=copy(strtmp,1,pos(splitstr,strtmp)-1);
strtmp:=copy(strtmp,pos(splitstr,strtmp)+length(splitstr),length(strtmp)-
pos(splitstr,strtmp));
i:=i+1;
end;
resultarray[i]:=strtmp;
result:=i+1;
end;
//此函数检查字符串是否为合法的时间
function timevalid(timestr:string;timeformat:integer):boolean;
var
h,m,s:string;
ary:array[0..2] of string;
splitret:integer;
i:integer;
begin
result:=true;
splitret:=mxsplitstr(timestr,':',ary);
if splitret<2 then begin
result:=false; exit;
end;
for i:=0 to 2 do begin
if length(ary[i])>2 then begin
result:=false; exit;
end;
ary[i]:=trim(ary[i]);
end;
h:=ary[0];m:=ary[1];
if timeformat=3 then s:=ary[2];
///////////////////////////////
if (h='') or (strtoint(h)>23 ) then begin
result:=false; exit;
end;
if (m='' ) or (strtoint(m)>59) then begin
result:=false; exit;
end;
if (timeformat=3) then
if (s='') or (strtoint(s)>59) then begin
result:=false; exit;
end;
end;
//此函数对时分秒进行加减运算
function inctime(atime: tdatetime; hours, minutes, seconds,
msecs: integer): tdatetime;
begin
result := atime + (hours div 24) + (((hours mod 24) * 3600000 +
minutes * 60000 + seconds * 1000 + msecs) / msecsperday);
if result < 0 then result := result + 1;
end;
//时分秒加减运算的二级函数
function timeadd(timestr:string;timeformat:integer;
hstep,mstep,sstep:integer):string;
var
dt:tdatetime;
begin
if not timevalid(timestr,timeformat) then
if timeformat=2 then begin result:='00:00'; exit; end
else begin result:='00:00:00';exit; end;
dt:=strtotime(timestr);
if timeformat=2 then
result:=formatdatetime('hh:mm',inctime(dt,hstep,mstep,sstep,0))
else
result:=formatdatetime('hh:mm:ss',inctime(dt,hstep,mstep,sstep,0))
end;
//限制组件的onkeydown
procedure mxformattimekeydown(ctl:tcustommaskedit;timeformat:integer;
var key:word;shift: tshiftstate;dts:tdatasource);
var
textsave:string;
selstartsave,sellengthsave:integer;
editingpos:integer;//1-h 2-m 3-s
i:integer;
begin
if (ssshift in shift) and (key<>vk_delete ) then exit;
if not (key in [vk_delete,vk_back,vk_up,vk_down] ) then exit;
if (dts<>nil ) and (not dts.dataset.active ) then exit;
if (dts<>nil) and (not dts.dataset.modified ) then