不用日期控件的智能日期輸入法
發表于:2007-07-14來源:作者:點擊數:
標簽:
作者:林永華 地址:福建省莆田縣農業 銀行 電腦中心 351100 tel:0594-2298794E-mail:ptnh.lyh@163.com 本人在編程過程中,常遇到要編寫記載日期信息的功能程序,為此,我曾經也下載選用了一些日期控件,但都覺得不好用(比如:不能隨意改變大
作者:林永華
地址:福建省莆田縣農業
銀行電腦中心 351100 tel:0594-2298794 E-mail:ptnh.lyh@163.com
本人在編程過程中,常遇到要編寫記載日期信息的功能程序,為此,我曾經也
下載選用了一些日期控件,但都覺得不好用(比如:不能隨意改變大小、或它是按月遞變等等,不信你自己去體驗吧)。就連Windows98中查找功能的日期選項卡中的那個日期控件(在
VB6部件中的Microsoft
Windows Common Controls-2 6.0中也有DTPicker控件 )我也覺得不是很好用,因為它是按月遞變等等。鑒于這些,我還是鐘情于VB自帶的標準文本框控件,因為它可以隨意改變框的大?。ò@示區文本的大?。?、顏色、字體。所以,我就編寫了以下的那段程序,用以進行日期型信息錄入?!皟烖c”如下:
1、在文本框中只接受“0-9”這10字符。而且年月日分隔符會自動生成。
2、VB的年份為100-9999,本人限在1000-2999,不夠用嗎?如果真得不夠用,你可以自己改造加以控制?;蛟S你會問:為什么年份不用兩位數來表示?
我的觀點:
1、兩位數的年份格式不直觀。
2、如果碰到要記載出生年月日信息時,很可能會很難辦。
3、月份只能從01-12,而且,當輸入3-9時,系統會自動默認為03-09。
4、日期如果輸入4-9時系統也會自動默認為04-09。還有:
A:當月份為1、3、5、7、8、10、12時,日期不能超過31
B:當月份為4、6、9、11時,日期不能超過30
C:當月份為2時且為閏年時,日期不能超過29
D:當月份為2時且為非閏年,日期不能超過28
有了以上的“優點”,只要輸入年月日完畢,就會確保它是一個合法的日期表達式。朋友,趕快把下面那段程序粘貼到VB工程中(當然,你也許要改變Text1為其它的文本框編號如:Text3)去試一下吧,相信,你會立即愛上她,直到永遠......
Private Sub Text1_Change()
Dim a, b, c As String
'---------------------------------------------------------------------------
'年份輸入的控制
'---------------------------------------------------------------------------
'限制第一位必須為"1"或"2" ,也就說年份必須在1000-2999之間,夠用吧!
If Len(Text1.Text) = 1 Then
If Left((Text1.Text), 1) <> "1" And Left((Text1.Text), 1) <> "2" Then
Text1.Text = ""
End If
End If
'限制第二、三、四位必須為“1、2、3、4、5、6、7、8、9、0”
If Len(Text1.Text) = 2 Or Len(Text1.Text) = 3 Or Len(Text1.Text) = 4 Then
If Right((Text1.Text), 1) <> "0" And Right((Text1.Text), 1) <> "1" And _
Right((Text1.Text), 1) <> "2" And Right((Text1.Text), 1) <> "3" And _
Right((Text1.Text), 1) <> "4" And Right((Text1.Text), 1) <> "5" And _
Right((Text1.Text), 1) <> "6" And Right((Text1.Text), 1) <> "7" And _
Right((Text1.Text), 1) <> "8" And Right((Text1.Text), 1) <> "9" Then
Text1.Text = Left((Text1.Text), Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If
End If
If Len(Text1.Text) = 4 Then
Text1.Text = Text1.Text + "-"
Text1.SelStart = Len(Text1.Text)
End If '當年份正確輸入后就自動加上的“-”分隔符
'---------------------------------------------------------------------------
'月份輸入的控制
'---------------------------------------------------------------------------
If Len(Text1.Text) = 6 Then
If Right((Text1.Text), 1) <> "0" And Right((Text1.Text), 1) <> "1" Then
If Right((Text1.Text), 1) = "2" Or Right((Text1.Text), 1) = "3" Or _
Right((Text1.Text), 1) = "4" Or Right((Text1.Text), 1) = "5" Or _
Right((Text1.Text), 1) = "6" Or Right((Text1.Text), 1) = "7" Or _
Right((Text1.Text), 1) = "8" Or Right((Text1.Text), 1) = "9" Then
a = Right((Text1.Text), 1)
Text1.Text = Left((Text1.Text), 5) + "0" + a + "-"
'如果這樣,那下面一段if len(text1.text)=7的判斷自然就自動跳過去了。
Text1.SelStart = Len(Text1.Text)
Else '限制只能輸入“0-9”
Text1.Text = Left((Text1.Text), Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If
End If
End If
If Len(Text1.Text) = 7 Then
If Left(Right(Text1.Text, 2), 1) = "0" Then '如果月份第一位為“0”
If Right((Text1.Text), 1) <> "1" And Right((Text1.Text), 1) <> "2" And _
Right((Text1.Text), 1) <> "3" And Right((Text1.Text), 1) <> "4" And _
Right((Text1.Text), 1) <> "5" And Right((Text1.Text), 1) <> "6" And _
Right((Text1.Text), 1) <> "7" And Right((Text1.Text), 1) <> "8" And _
Right((Text1.Text), 1) <> "9" Then
Text1.Text = Left((Text1.Text), Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
Else
Text1.Text = Text1.Text + "-" '當月份輸入正確后自動加一個“-”分隔符
Text1.SelStart = Len(Text1.Text)
Exit Sub '少不了!如果少,那當月份為“01”時,緊接的If...End IF就
'成立,這樣會在這里出現死循環,而出現溢出堆??臻g的錯誤!
'注:本程序好幾個地方都可以用上Exit Sub,要加你自己補上吧!
End If
End If
If Left(Right((Text1.Text), 2), 1) = "1" Then '如果月份第一位為“1”
If Right((Text1.Text), 1) <> "0" And Right((Text1.Text), 1) <> "1" And _
Right((Text1.Text), 1) <> "2" Then
Text1.Text = Left((Text1.Text), Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
Else
Text1.Text = Text1.Text + "-" '當月份輸入正確后自動加一個“-”分隔符
Text1.SelStart = Len(Text1.Text)
End If
End If
End If
'---------------------------------------------------------------------------
'日期輸入的控制。
'---------------------------------------------------------------------------
If Len(Text1.Text) = 9 Then
If Right((Text1.Text), 1) <> "0" And Right((Text1.Text), 1) <> "1" And _
Right((Text1.Text), 1) <> "2" And Right((Text1.Text), 1) <> "3" Then
If Right((Text1.Text), 1) = "4" Or Right((Text1.Text), 1) = "5" Or _
Right((Text1.Text), 1) = "6" Or Right((Text1.Text), 1) = "7" Or _
Right((Text1.Text), 1) = "8" Or Right((Text1.Text), 1) = "9" Then
a = Right((Text1.Text), 1)
Text1.Text = Left((Text1.Text), 8) + "0" + a
Text1.SelStart = Len(Text1.Text)
'Exit Sub
'日期小于10時下面字符長度為10的判斷當然是正確的。讓它執行又如何?
Else
Text1.Text = Left((Text1.Text), Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If
End If
End If
'當要修改日期的最后一位時的控制。
If Len(Text1.Text) = 10 Then
b = Left(Right(Text1.Text, 5), 2) '取月份值,用于下面的日期正確性判斷!
c = Left(Text1.Text, 4) '取年份值,用于下面的日期正確性判斷!
If Right((Text1.Text), 1) <> "0" And Right((Text1.Text), 1) <> "1" And _
Right((Text1.Text), 1) <> "2" And Right((Text1.Text), 1) <> "3" And _
Right((Text1.Text), 1) <> "4" And Right((Text1.Text), 1) <> "5" And _
Right((Text1.Text), 1) <> "6" And Right((Text1.Text), 1) <> "7" And _
Right((Text1.Text), 1) <> "8" And Right((Text1.Text), 1) <> "9" Then
Text1.Text = Left((Text1.Text), Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If '限制非法字符的輸入。
If Right(Text1.Text, 2) = "00" Then
Text1.Text = Left((Text1.Text), Len(Text1.Text) - 2)
Text1.SelStart = Len(Text1.Text)
End If '限制日期不能為0
If (b = "01" And Val(Right(Text1.Text, 2)) > 31) Or _
(b = "03" And Val(Right(Text1.Text, 2)) > 31) Or _
(b = "05" And Val(Right(Text1.Text, 2)) > 31) Or _
(b = "07" And Val(Right(Text1.Text, 2)) > 31) Or _
(b = "08" And Val(Right(Text1.Text, 2)) > 31) Or _
(b = "10" And Val(Right(Text1.Text, 2)) > 31) Or _
(b = "12" And Val(Right(Text1.Text, 2)) > 31) Then
Text1.Text = Left((Text1.Text), Len(Text1.Text) - 2)
Text1.SelStart = Len(Text1.Text)
End If '當月份為大月份時日期不能大于31。
If (b = "04" And Val(Right(Text1.Text, 2)) > 30) Or _
(b = "06" And Val(Right(Text1.Text, 2)) > 30) Or _
(b = "09" And Val(Right(Text1.Text, 2)) > 30) Or _
(b = "11" And Val(Right(Text1.Text, 2)) > 30) Then
Text1.Text = Left((Text1.Text), Len(Text1.Text) - 2)
Text1.SelStart = Len(Text1.Text)
End If '當月份為小月份時日期不能大于30。
If b = "02" Then
If Val(c) Mod 4 <> 0 And Val(Right(Text1.Text, 2)) > 28 Then
Text1.Text = Left((Text1.Text), Len(Text1.Text) - 2)
Text1.SelStart = Len(Text1.Text)
End If '非閏年日期不得超過28。
If Val(c) Mod 4 = 0 And Val(Right(Text1.Text, 2)) > 29 Then
Text1.Text = Left((Text1.Text), Len(Text1.Text) - 2)
Text1.SelStart = Len(Text1.Text)
End If '閏年日期不得超過29。
End If '當月份為2時的日期正確性判斷!
End If
'---------------------------------------------------------------------------
'當年月日輸入后就不再接受其它字符了。方法如下:
'---------------------------------------------------------------------------
'第一種方法:
'在Text1的屬性窗口中設Maxlength=10
'第二種方法:
'Text2.Setfocus 即在適當的地方設一個跳轉語句使下一個對象得到焦點。
'第三種方法:
If Len(Text1.Text) = 11 Then
Text1.Text = Left((Text1.Text), Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If
End Sub
原文轉自:http://www.anti-gravitydesign.com