我想在TEXT文本框里找有没有跟某一个字母一样的内容,该如何实现?比如输入邮箱地址,里面有没有@在。还有,我想限制输和长度,比如只能输入3-18个字符之间,又该如何实现?

解决方案 »

  1.   

    Private Sub Form_Load()
        Text1.MaxLength = 18
    End SubPrivate Sub Text1_Validate(Cancel As Boolean)
        If Len(Text1.Text) < 3 Then
            Cancel = True
        End If
        If InStr(Text1.Text, "@") > 0 Then
            MsgBox "有@在"
        Else
             Cancel = True
        End If
        
    End Sub
      

  2.   

    Private Sub Form_Load()
        Text1.MaxLength = 18
    End SubPrivate Sub Text1_Validate(Cancel As Boolean)
        If Len(Text1.Text) <= 3 Then
            Cancel = True
        End If
        If InStr(Text1.Text, "@") > 0 Then
            MsgBox "有@在"
        Else
             Cancel = True
        End If
        
    End Sub
      

  3.   

    在Validate事件中判断,如果少于3或者没有@就不能离开
      

  4.   

    TextBox 有 Maxlength 属性,定义最大长度,超过会忽略。用 Len 测试长度,少于某个长度提示错误,从新输入。@ 符号可以用 Mid 函数挨个测试,每次测试一个字符,直到找到一个 @ 为止。
    Textbox1.Maxlength = 18在文本框的 LostFocus 事件中
    If Len(Textbox1.Text) <= 3 Then
        MsgBox ("输入长度过短,请输入一个 3-18 位的字符串。")
        TextBox1.Text = ""
    End IfFor I = 1 To Len(TextBox1.Text)
        Test$ = Mid(TextBox.Text,I,1)
        If Test$ = "@" Then
            OkBoolean = True
            Exit For
        else
            OkBoolean = False
        End IF
    Next I
    If OkBoolean = False Then
        MsgBox ("在字符串中至少包含一个 @ ,请从新输入。")
        TextBox1.Text = ""
    End IF