给VFP做一个智能日期识别控件
出处:网络
1、新建一个类,派生于TextBox,取类名如InputDate。
2、对InputDate的 LostFocus 和 GotFocus 分别写入如下代码:
LostFocus:
IF !EMPTY(THIS.VALUE)
cYear=allt(str(year(date())))
cMonth=allt(str(month(date())))
nPoint=0
cInput=allt(this.value)
nLenInput=len(cInput)
if left(cInput,1)=="."
cInput=right(cInput,nLenInput-1)
nLenInput=Len(cInput)
endif
if right(cInput,1)=="."
cInput=left(cInput,nLenInput-1)
nLenInput=Len(cInput)
endif
n=1
DO WHILE n<=nLenInput
cChar=subst(cInput,n,1)
if cChar=='.'
nPoint=nPoint+1
endif
n=n+1
ENDDO
DO CASE
CASE nPoint=0
cOutPut=cYear+'.'+cMonth+'.'+cInput
CASE nPoint=1
cOutPut=cYear+'.'+cInput
CASE nPoint=>2
cOutPut=cInput
ENDCASE
dOutPut=ctod(cOutPut)
if empty(dOutput)
??chr(7)
this.value="日期错误!"
else
this.value=dOutPut
endif
ENDIF
GotFocus:
if type('this.value')='D'
this.value=dtoc(this.value)
endif
3、对InputDate的属性设置如下:
SelectOnEntry=.T.
Century=1 - 开
DateFormat=14 - 汉语
4、注意事项,因以上代码必须设置:
Set date ansi
Set cent on
如果绑定其数据源ControlSource时,运行会出错,可以根据具体情况改写上述两段代码,如果提示是“日期错误!”则数据类型是字符型的,如果给出正确的日期,则数据类型是日期型的。
5、用法:
在表单上放入类InputDate1,运行时,输入一个有效的日期数字,如输入2,则返回当前年当前月的2日,如输入2.2,则返回当前年的2月2日。
[返回] |