CSDN博客

img benna

不用日期控件的智能日期输入法(vb代码版)

发表于2004/9/14 22:25:00  2063人阅读

分类: 技术文章

1、在文本框中只接受“0-9”这10字符。而且年月日分隔符会自动生成。

2、VB的年份为100-9999,本人限在1000-2999,不够用吗?如果真得不够用,你可以自己改造加以控制。或许你会问:为什么年份不用两位数来表示?

我的观点:

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 txt_age_Change()
Dim a, b, c As String
'---------------------------------------------------------------------------
'年份输入的控制
'---------------------------------------------------------------------------
'限制第一位必须为"1"或"2" ,也就说年份必须在1000-2999之间,够用吧!
If Len(txt_age.Text) = 1 Then
  If Left((txt_age.Text), 1) <> "1" And Left((txt_age.Text), 1) <> "2" Then
    txt_age.Text = ""
  End If
End If
'限制第二、三、四位必须为“1、2、3、4、5、6、7、8、9、0”
If (Len(txt_age.Text) = 2 Or Len(txt_age.Text) = 3 Or Len(txt_age.Text)) = 4 Then
  If (Right((txt_age.Text), 1) <> "0" And Right((txt_age.Text), 1) <> "1" And _
       Right((txt_age.Text), 1) <> "2" And Right((txt_age.Text), 1) <> "3" And _
       Right((txt_age.Text), 1) <> "4" And Right((txt_age.Text), 1) <> "5" And _
       Right((txt_age.Text), 1) <> "6" And Right((txt_age.Text), 1) <> "7" And _
      Right((txt_age.Text), 1) <> "8" And Right((txt_age.Text), 1) <> "9") Then
            txt_age.Text = Left((txt_age.Text), Len(txt_age.Text) - 1)
            txt_age.SelStart = Len(txt_age.Text)
  End If
End If
If Len(txt_age.Text) = 4 Then
    txt_age.Text = txt_age.Text + "/"
    txt_age.SelStart = Len(txt_age.Text)
 End If ' 当年份正确输入后就自动加上的“-”分隔符
    '---------------------------------------------------------------------------
    '月份输入的控制
    '---------------------------------------------------------------------------
 If Len(txt_age.Text) = 6 Then
     If Right((txt_age.Text), 1) <> "0" And Right((txt_age.Text), 1) <> "1" Then
          If Right((txt_age.Text), 1) = "2" Or Right((txt_age.Text), 1) = "3" Or _
             Right((txt_age.Text), 1) = "4" Or Right((txt_age.Text), 1) = "5" Or _
             Right((txt_age.Text), 1) = "6" Or Right((txt_age.Text), 1) = "7" Or _
             Right((txt_age.Text), 1) = "8" Or Right((txt_age.Text), 1) = "9" Then
                    a = Right((txt_age.Text), 1)
                    txt_age.Text = Left((txt_age.Text), 5) + "0" + a + "/"
                    '如果这样,那下面一段if len(txt_age.text)=7的判断自然就自动跳过去了。
                    txt_age.SelStart = Len(txt_age.Text)
           Else '限制只能输入“0-9”
                    txt_age.Text = Left((txt_age.Text), Len(txt_age.Text) - 1)
                    txt_age.SelStart = Len(txt_age.Text)
           End If
     End If
End If
If Len(txt_age.Text) = 7 Then
  If Left(Right(txt_age.Text, 2), 1) = "0" Then '如果月份第一位为“0”
        If Right((txt_age.Text), 1) <> "1" And Right((txt_age.Text), 1) <> "2" And _
            Right((txt_age.Text), 1) <> "3" And Right((txt_age.Text), 1) <> "4" And _
            Right((txt_age.Text), 1) <> "5" And Right((txt_age.Text), 1) <> "6" And _
            Right((txt_age.Text), 1) <> "7" And Right((txt_age.Text), 1) <> "8" And _
            Right((txt_age.Text), 1) <> "9" Then
                txt_age.Text = Left((txt_age.Text), Len(txt_age.Text) - 1)
                txt_age.SelStart = Len(txt_age.Text)
         Else
                txt_age.Text = txt_age.Text + "/" '当月份输入正确后自动加一个“-”分隔符
                txt_age.SelStart = Len(txt_age.Text)
         Exit Sub '少不了!如果少,那当月份为“01”时,紧接的If...End IF就
            '成立,这样会在这里出现死循环,而出现溢出堆栈空间的错误!
            '注:本程序好几个地方都可以用上Exit Sub,要加你自己补上吧!
   End If
End If

If Left(Right((txt_age.Text), 2), 1) = "1" Then '如果月份第一位为“1”
    If Right((txt_age.Text), 1) <> "0" And Right((txt_age.Text), 1) <> "1" And Right((txt_age.Text), 1) <> "2" Then
        txt_age.Text = Left((txt_age.Text), Len(txt_age.Text) - 1)
        txt_age.SelStart = Len(txt_age.Text)
    Else
        txt_age.Text = txt_age.Text + "-" '当月份输入正确后自动加一个“-”分隔符
        txt_age.SelStart = Len(txt_age.Text)
    End If
End If

End If
'---------------------------------------------------------------------------
'日期输入的控制?
'---------------------------------------------------------------------------
If Len(txt_age.Text) = 9 Then
    If Right((txt_age.Text), 1) <> "0" And Right((txt_age.Text), 1) <> "1" And Right((txt_age.Text), 1) <> "2" And Right((txt_age.Text), 1) <> "3" Then
        If Right((txt_age.Text), 1) = "4" Or Right((txt_age.Text), 1) = "5" Or _
            Right((txt_age.Text), 1) = "6" Or Right((txt_age.Text), 1) = "7" Or _
            Right((txt_age.Text), 1) = "8" Or Right((txt_age.Text), 1) = "9" Then
            a = Right((txt_age.Text), 1)
            txt_age.Text = Left((txt_age.Text), 8) + "0" + a
            txt_age.SelStart = Len(txt_age.Text)
            Exit Sub
            '日期小于10时下面字符长度为10的判断当然是正确的。让它执行又如何?
        Else
            txt_age.Text = Left((txt_age.Text), Len(txt_age.Text) - 1)
            txt_age.SelStart = Len(txt_age.Text)
        End If
    End If
End If
'当要修改日期的最后一位时的控制?
If Len(txt_age.Text) = 10 Then
    b = Left(Right(txt_age.Text, 5), 2) '取月份值,用于下面的日期正确性判断!
    c = Left(txt_age.Text, 4) '取年份值,用于下面的日期正确性判断!
    If Right((txt_age.Text), 1) <> "0" And Right((txt_age.Text), 1) <> "1" And _
        Right((txt_age.Text), 1) <> "2" And Right((txt_age.Text), 1) <> "3" And _
        Right((txt_age.Text), 1) <> "4" And Right((txt_age.Text), 1) <> "5" And _
        Right((txt_age.Text), 1) <> "6" And Right((txt_age.Text), 1) <> "7" And _
        Right((txt_age.Text), 1) <> "8" And Right((txt_age.Text), 1) <> "9" Then
            txt_age.Text = Left((txt_age.Text), Len(txt_age.Text) - 1)
            txt_age.SelStart = Len(txt_age.Text)
    End If '限制非法字符的输入。
    If Right(txt_age.Text, 2) = "00" Then
        txt_age.Text = Left((txt_age.Text), Len(txt_age.Text) - 2)
        txt_age.SelStart = Len(txt_age.Text)
    End If '限制日期不能为0
    If (b = "01" And Val(Right(txt_age.Text, 2)) > 31) Or _
    (b = "03" And Val(Right(txt_age.Text, 2)) > 31) Or _
    (b = "05" And Val(Right(txt_age.Text, 2)) > 31) Or _
    (b = "07" And Val(Right(txt_age.Text, 2)) > 31) Or _
    (b = "08" And Val(Right(txt_age.Text, 2)) > 31) Or _
    (b = "10" And Val(Right(txt_age.Text, 2)) > 31) Or _
    (b = "12" And Val(Right(txt_age.Text, 2)) > 31) Then
        txt_age.Text = Left((txt_age.Text), Len(txt_age.Text) - 2)
        txt_age.SelStart = Len(txt_age.Text)
    End If '当月份为大月份时日期不能大于31。
    If (b = "04" And Val(Right(txt_age.Text, 2)) > 30) Or _
    (b = "06" And Val(Right(txt_age.Text, 2)) > 30) Or _
    (b = "09" And Val(Right(txt_age.Text, 2)) > 30) Or _
    (b = "11" And Val(Right(txt_age.Text, 2)) > 30) Then
        txt_age.Text = Left((txt_age.Text), Len(txt_age.Text) - 2)
        txt_age.SelStart = Len(txt_age.Text)
    End If '当月份为小月份时日期不能大于30。
    If b = "02" Then
        If Val(c) Mod 4 <> 0 And Val(Right(txt_age.Text, 2)) > 28 Then
            txt_age.Text = Left((txt_age.Text), Len(txt_age.Text) - 2)
            txt_age.SelStart = Len(txt_age.Text)
        End If '非闰年日期不得超过28。
        If Val(c) Mod 4 = 0 And Val(Right(txt_age.Text, 2)) > 29 Then
            txt_age.Text = Left((txt_age.Text), Len(txt_age.Text) - 2)
            txt_age.SelStart = Len(txt_age.Text)
        End If '闰年日期不得超过29。
    End If '当月份为2时的日期正确性判断!
  End If
'---------------------------------------------------------------------------
'当年月日输入后就不再接受其它字符了?方法如下:
'---------------------------------------------------------------------------
'第一种方法:
'在txt_age的属性窗口中设Maxlength = 10
'第二种方法:
'Text2.SetFocus 即在适当的地方设一个跳转语句使下一个对象得到焦点?
'第三种方法:
If Len(txt_age.Text) = 11 Then
txt_age.Text = Left((txt_age.Text), Len(txt_age.Text) - 1)
txt_age.SelStart = Len(txt_age.Text)
End If


-------------------------------------------------------------------------------

0 0

相关博文

我的热门文章

img
取 消
img