判断是否是中文字符的函数如下
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Byte, ByVal Source As String, ByVal Length As Long)
'该函数判断传入字符是否是GBK汉字库中的字符
'如果是则返回True,否则返回False
Private Function IS_GBK_HZ(ByVal sHZ As String) As Boolean
    Dim aHZ(1) As Byte
    sHZ = Left(sHZ, 1)
    If Len(sHZ) > 0 Then
        CopyMemory aHZ(0), sHZ, 2
        IS_GBK_HZ = (aHZ(0) >= &H81) And (aHZ(0) < &HFE) And (aHZ(1) >= &H40) And (aHZ(1) < &HFF)
    End If
End Function

解决方案 »

  1.   

    兄弟,用得着这么复杂吗,在keypress事件里判断keyascii<0就ok了,试试吧
      

  2.   

    在keypress事件中
        If (KeyAscii > 0) And (KeyAscii <> 8) Then
            KeyAscii = 0
        End If
      

  3.   

    to chenhw(我要回复) and mrlining(ball):中文不在Ascii范围内
      

  4.   

    to chenhw(我要回复) and mrlining(ball):我的要求是不包括标点符号和特殊符号的中文
      

  5.   

    to ltpao(啊炮):请问如何把标点和特殊符号去掉?(你提供的程序我已试过了,确实可行,但却包括了标点和特殊符号)
      

  6.   

    Private Sub Text3_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
      Case 8  Case 13
           Text4.SetFocus
      Case Else
           If hex(KeyAscii)<&h4E00 and hex(KeyAscii)>&9FAF Then
              Text3.Text = Text3.Text
              msgbox "这是你的要求",,"提示信息"
           Else
              KeyAscii = 0
           End If
      End Select
    End Sub
      

  7.   


    to   MonkeyLin
    记得资源共享
      

  8.   

    to Ganman():小弟试了一下,好像你的程序有点问题--小弟输入中文,它照样认为不是中文.可能小弟说的还不够清楚,因为小弟是把文本框的字符取出,然后按位判的(用MID(text1.text,i,1)),而非在输入时判的.
    private sub command1_click()
    for i=0 to len(text1.text)
    if IS_GBK_HZ(mid(text1.text,i,1))=false then  'IS_GBK_HZ是ltpao(啊炮)的程序
       msgbox"it is not Chinese"
    end if 
    next i
    end sub
    可惜的是该程序没把标点符号去掉.
      

  9.   

    to Ganman():我想你的程序问题(输入中文,它照样认为不是中文),是因为4E00-9FAF是UNICODE码,而非ASCII码(ASCII码是UNICODE(有6万多个字符,用来代表全世界的语言只用了3万多个)的前128个字符)
      

  10.   

    应该在text_change中添加代码,否则按住键盘不放怎么办?
      

  11.   

    to limengchen(lmc):排除这种不必要的因素,在按钮中判文本框中的字符正是我现在的需求,不知你能否给解决一下。
      

  12.   

    '这是修正过的程序,绝对不会包括标点和特殊符号
    Private Function IS_GBK_HZ(ByVal sHZ As String) As Boolean
        Dim aHZ(1) As Byte
        sHZ = Left(sHZ, 1)
        If Len(sHZ) > 0 Then
            CopyMemory aHZ(0), sHZ, 2
            IS_GBK_HZ = (((aHZ(0) >= &H81) And (aHZ(0) < &HA1)) Or ((aHZ(0) >= &HAA) And (aHZ(0) < &HFE))) And (aHZ(1) >= &H40) And (aHZ(1) < &HFF)
        End If
    End Function
      

  13.   

    我的测试程序如下:
    Private Sub Text1_Change()
        Debug.Print Right(Text1.Text, 1) & " " & IS_GBK_HZ(Right(Text1.Text, 1))
    End Sub
    你的测试程序怎样?
      

  14.   

    我的是
    private sub command1_click()
    if is_gbk_hz(text1.text) then
    msgbox "true"
    else
    msgbox "false"
    end if
    end sub
      

  15.   

    没问题,我怀疑你的CopyMemory 函数声明有问题,你是不是这样声明的
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    ,如果是这样则有可能在内存拷贝时出错,请把你的声明改为下面的声明
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Byte, ByVal Source As String, ByVal Length As Long)
      

  16.   

    to ltpao(啊炮):小弟试了一下你的程序,果然解决了这个问题,非常感谢