以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  请教:关于日期的判断以及日期控件  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=54790)

--  作者:bahamute
--  发布时间:2014/8/4 19:38:00
--  请教:关于日期的判断以及日期控件
“签订时间”列为日期格式,“所属年度”列的值从“签订时间”列取年度值。

If e.DataCol.Name = "签订日期" Then
    e.DataRow("年度") =Format(e.DataRow("签订日期"), "yyyy")
End If

1、上述代码在输入“签订时间”格式无误的情况下,运行正常,一旦日期格式错误,项目就会挂掉,请教如何判断日期输入格式正确?
2、日期控件能否禁止掉手动输入?



--  作者:有点甜
--  发布时间:2014/8/4 19:42:00
--  

 日期格式根本不能输入有错的日期啊

 

If e.DataCol.Name = "签订日期" AndAlso e.NewValue <> Nothing Then
    e.DataRow("年度") =Format(e.DataRow("签订日期"), "yyyy")
End If

--  作者:bahamute
--  发布时间:2014/8/4 19:47:00
--  
比如误操作,我刚刚试了一下,在日期控件输入一个不完整的日期,比如2014-4-,然后单击其他控件,项目就会报错然后挂掉。
所以才想到判断日期格式是否完整。

--  作者:有点甜
--  发布时间:2014/8/4 19:48:00
--  

参考 http://www.foxtable.com/help/topics/0324.htm

 


--  作者:bahamute
--  发布时间:2014/8/4 20:30:00
--  

以下代码无效,如果输入日期格式错误,项目任然报错。

Validating事件:

If e.Sender.Value <>"" Then
    Dim d As Date \'变量d用于存储转换结果
    Dim dt As Date = e.Sender.Value
    If Date.TryParse(dt, d) Then \'如果转换成功
        e.Sender.WriteValue()
    Else
        messagebox.Show("无效日期格式","提示",MessageBoxButtons.ok, MessageBoxIcon.warning) \'给出错误提示
        e.Cancel=True
    End If
End If

--  作者:有点甜
--  发布时间:2014/8/4 20:33:00
--  

改一下

 

If e.Sender.Text <>"" Then
    Dim d As Date \'变量d用于存储转换结果
    If Date.TryParse(e.Sender.Text, d) Then \'如果转换成功
        e.Sender.WriteValue()
    Else
        messagebox.Show("无效日期格式","提示",MessageBoxButtons.ok, MessageBoxIcon.warning) \'给出错误提示
        e.Cancel=True
    End If
End If