在KeyPress里面用Len判断它的长度,如果大于等于4,让KeyAscii=0

解决方案 »

  1.   

    Private Sub Combo1_KeyPress(KeyAscii As Integer)
    If Len(Combo1.Text) >= 4 Then KeyAscii = 0
    End Sub
      

  2.   


    http://expert.csdn.net/Expert/topic/1432/1432990.xml?temp=.2927515
    帮我骂那个畜生
      

  3.   

    还应该考虑以下几个问题
    1.Len()得到的是字符的个数(1个汉字也只算1个字符),
      而实际需要控制的是字符的存储字节数,
      即1个汉字占2字节,1个半角字符占1字节。
    2.在计算现有字符数时,应该去掉已经选中的部分,
      因为输入的字符会将之替换掉。
    3.只差1个子节时,需要判别正在输入的字符是否是半角字符。
    4.BackSpace键不应该屏蔽掉。例:
    Private Sub Combo1_KeyPress(KeyAscii As Integer)
        Dim intSel  As Integer
        Dim intLen  As Integer
        Dim intLenMax   As Integer     
        intLenMax = 4                  '限定长度
        Select Case KeyAscii
            Case 13                    '是Enter键,无需特殊处理
            Case 8                     '是Backspace键,无需特殊处理
            Case Else
                With Combo1
                    '计算已选择部分的字节数
                    intSel = .SelLength
                    If intSel > 0 Then
                        intSel = LenB(StrConv(Mid(.Text, _
                            .SelStart + 1, intSel), vbFromUnicode))
                    End If
                    '扣除已选择部分的字节数
                    intLen = LenB(StrConv(.Text, vbFromUnicode)) _
                        - intSel
                    If intLen >= intLenMax Then
                        '长度已够,输入字符作废
                        KeyAscii = 0
                    ElseIf intLen = intLenMax - 1 And _
                        (KeyAscii < 0 Or KeyAscii > 127) Then
                        '长度只差1字节,输入的全角字符作废
                        KeyAscii = 0
                    End If
                End With
        End Select
    End Sub