别人在我的textbox里面打内容,我一定要控制其打中文,就是他打的不是中文我就要报错,怎么判断啊??

解决方案 »

  1.   

    在对于每个键入的字符,判断其Ascii码是否大于128,汉字的都大于,用Asc(string)获得。
      

  2.   

    对。
    在text1_Change()
    事件添加判断代码,如楼上所述。
      

  3.   

    Public Sub OnlyInputNumber(objText As Object)
        '相关字符的ASCII码
        '字符: (退格键) +(正号),(逗号) -(负号)
        'Ascii: 8 43 44 45
        '字符: .(小数点) /(斜杠) 0~9
        ' Ascii: 46 47 48~57
        '退格键有效
        If KeyAscii = 8 Then Exit Sub
        '正号(+)、负号(-)、小数点及数字以外的字符视为无效键入
        If KeyAscii < Asc("+") Or KeyAscii = Asc(",") Or KeyAscii = Asc("/") Or KeyAscii > Asc("9") Then
            KeyAscii = 0
            Exit Sub
        End If    '正负号(未被选取)前不能再键入字符
        If Left(objText.Text, 1) = "+" Or Left(objText.Text, 1) = "-" Then
            If objText.SelStart = 0 Then
                If InStr(objText.SelText, "+") = 0 And InStr(objText.SelText, "-") = 0 Then
                    KeyAscii = 0
                    Exit Sub
                End If
            End If
        End If    '键入小数点    If KeyAscii = Asc(".") Then
        '原TextBox中已有小数点
            If InStr(objText.Text, ".") > 0 Then
            '原TextBox中的小数点未被选取,作为无效处理(不能有两个小数点)
                If InStr(objText.SelText, ".") = 0 Then
                    KeyAscii = 0
                End If
            '原TextBox中的小数点已被选取(选取的部分将被替换),键入有效
            End If
            '原TextBox中没有小数点,键入有效,可以是首位
            Exit Sub
        End If    '键入正号或负号
        If KeyAscii = Asc("+") Or KeyAscii = Asc("-") Then
            '原TextBox中已有正号或负号
            If InStr(objText.Text, "+") > 0 Or InStr(objText.Text, "-") > 0 Then
                    '原TextBox中的正负号未被选取,作为无效处理(不能有两个正负号)
                    If InStr(objText.SelText, "+") = 0 And InStr(objText.SelText, "-") = 0 Then
                        KeyAscii = 0
                    End If
            '原TextBox中的正号或负号已被选取(选取的部分将被替换),键入有效
            Else '原TextBox中没有正号或负号
                '光标不位于首位,正号或负号也无效
                If objText.SelStart > 0 Then
                    KeyAscii = 0
                End If
                '光标只有在首位时正号或负号才键入有效
            End If
        End If
    End Sub
      

  4.   

    If KeyAscii > 0 And KeyAscii <> 8 Then
    KeyAscii = 0