我要在textbox里要输入的类型是 1-1,1-2,2-1····
如果输入的不是这样的类型,该怎么判断?
代码该怎么写小弟求教了。。
谢谢各位了、、

解决方案 »

  1.   

    最好使用正则表达式,简单理解的话可以自己写函数,判断字符串是否有"-"存在(instr),然后使用"-"分割(split),查询分割的数组是否是两个(ubound)字符串,然后判断这两个字符串是否是数字(isnumber)。
      

  2.   

    Private Sub Text1_LostFocus()
    s = Text1.Text
    s1 = Split(s, "-")
    If UBound(s1) <> 1 Then MsgBox "输入格式不对!": Exit Sub
    If IsNumeric(s1(0)) = False Or IsNumeric(s1(1)) = False Or Val(s1(0)) < 1 Or Val(s1(1)) < 1 Then MsgBox "输入格式不对!": Exit Sub
    If InStr(s1(0), ".") <> 0 Or InStr(s1(1), ".") <> 0 Then MsgBox "输入格式不对!": Exit Sub
    End Sub
     
    放两个文本框,点第二个文本框,让第一个失去焦点
      

  3.   


    Private Function ISlegal(vText As String) As Boolean
        On Error Resume Next
        Dim boolYN As Boolean
        If InStr(1, vText, "-") = 0 Then
            boolYN = False
        Else
           If (IsNumeric(Left(vText, InStr(1, vText, "-") - 1)) = False Or _
                  Val(Left(vText, InStr(1, vText, "-") - 1)) <= 0) _
               Or (IsNumeric(Mid(vText, InStr(1, vText, "-") + 1)) = False Or _
                  Val(Left(vText, InStr(1, vText, "-") - 1)) <= 0) Then            boolYN = False
            Else
                boolYN = True
            End If
        End If
        ISlegal = boolYN
    End Function……
        If ISlegal(Trim(Text1.Text)) = False Then
            MsgBox "输入不合法"
            Exit Sub
        End If
      

  4.   

    如果限制"-"前后的数字为整数,加条件判断一下,方法比较多
    IsNumeric(Left(vText, InStr(1, vText, "-") - 1)) = False Or (instr(1, vText,".")>0) 
      

  5.   


    private function Check_String_Style(byval strTemp as string ) as boolean
    dim strArr() as string 
    dim i as longstrArr = split(strTemp,"-")
    if len(strArr(0)) = 0 then
    '不满足
    else
       if isnumeric(strArr(0) then
       else
       '不满足
       end if
    end if
    if len(strArr(1)) = 0 then
    '不满足
    else
       if isnumeric(strArr(1) then
       else
       '不满足
       end if
    end if
    end function