怎样才能在TextBox里面限制只能输入0811 .就是输入的只能是两位的年份和2位的月份.当中不能输入0000.

解决方案 »

  1.   

    Private Sub Text1_Change()
        If Not IsNumeric(Text1.Text) Then
            MsgBox "?"
            Text1.Text = ""
            Text1.SetFocus
        End If
        
        If Len(Text1.Text) > 4 Then
            MsgBox "?"
            Text1.Text = Left(Text1.Text, 4)
        End If
        
        If Mid(Text1.Text, 3, 2) > 12 Then
            MsgBox "?"
            Text1.Text = Left(Text1.Text, 2)
        End If
            
    End Sub
      

  2.   

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            Me.TextBox1.MaxLength = 4
            If Char.IsNumber(e.KeyChar) = False Then
                If Char.IsControl(e.KeyChar) = True Then
                    e.Handled = False
                Else
                    e.Handled = True
                End If
            End If
    End SubPrivate Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            If Me.TextBox1.Text = "00" Then
                Me.TextBox1.Text = ""
            End If
    End Sub
      

  3.   

    Private Sub Text1_LostFocus()
        If (Text1 Like "####") And (IsDate(Format(Format("19" & Text1, "####-##"), "yyyy-mm"))) Then
            MsgBox "合法"
        Else
            MsgBox "非法"
        End If
    End Sub
      

  4.   


    比较麻烦:Private Sub Text1_KeyPress(KeyAscii As Integer)Select Case Text1.SelStart
        Case 0
            Select Case KeyAscii
                Case 8, 13, 48 To 57
                Case Else
                    KeyAscii = 0
            End Select
        Case 1
            Select Case KeyAscii
                Case 8, 9, 13, 48 To 57
                Case Else
                    KeyAscii = 0
            End Select
        Case 2
            Select Case KeyAscii
                Case 8, 9, 13, 48 To 49
                Case Else
                    KeyAscii = 0
            End Select
        Case 3
            Select Case KeyAscii
                Case 8, 9, 13
                Case 48 To 57
                    If (Mid(Text1, 3, 1) & Chr(KeyAscii) = "00") Or (Mid(Text1, 3, 1) & Chr(KeyAscii) > 12) Then KeyAscii = 0
                Case Else
                    KeyAscii = 0
            End Select
     
    End SelectEnd Sub
      

  5.   

    还是输入完毕再判断简单,简化一下前面的代码:Private Sub Text1_LostFocus()
        If (Text1 Like "####") And (IsDate(Format("19" & Text1, "####-##"))) Then
            MsgBox "合法"
        Else
            MsgBox "非法"
        End If
    End Sub
      

  6.   


    Option ExplicitPrivate Sub Form_Load()
        Text1.MaxLength = 4
        Text1 = ""
    End SubPrivate Sub Text1_Change()
        Dim strS As String
        strS = Text1
        If Len(Text1) > 2 Then
            If CInt(Mid(strS, 3, Len(Text1) - 2)) > 30 Then
                Text1.SelStart = 2
                Text1.SelLength = 2
            End If
            
        Else
            If CInt(Left(strS, 2)) > 12 Then
                Text1.SelStart = 0
                Text1.SelLength = 2
            End If
        End If
    End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
            Case 48 To 57
            Case Else
            KeyAscii = 0
        End Select
    End Sub
      

  7.   

    上一个错了Option ExplicitPrivate Sub Form_Load()
        Text1.MaxLength = 4
        Text1 = ""
    End SubPrivate Sub Text1_Change()
        Dim strS As String
        strS = Text1
        If Len(Text1) > 2 Then
            If CInt(Mid(strS, 3, Len(Text1) - 2)) > 12 Then
                Text1.SelStart = 2
                Text1.SelLength = 2
            End If
        End If
    End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
            Case 48 To 57
            Case Else
            KeyAscii = 0
        End Select
    End Sub
      

  8.   

    Private Sub Text1_LostFocus()
    b = Left(Text1.Text, 2)
    If Len(Trim(Text1.Text)) = 4 And IsNumeric(Text1.Text) And CInt(b) <> 0 And CInt(b) < 13 Then
    MsgBox "ok"
    Else
    MsgBox "no"
    End If
    End Sub