Combo1控件如何限制输入的字符长度?

解决方案 »

  1.   

    Private Sub Combo1_KeyPress(KeyAscii As Integer)
    If Len(Combo1.Text) >= 4 And KeyAscii <> 8 Then KeyAscii = 0
    End Sub只能输入 4 个
      

  2.   

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

  3.   

    以上方法都无法控制粘帖入的数据,也无法控制中文字。应该在 Validate 事件中判断:Private Sub Combo1_Validate(Cancel As Boolean)
    If LenB(StrConv(txtVal, vbFromUnicode)) > 4 And KeyAscii <> 8 Then KeyAscii = 0
    End Sub
      

  4.   

    靠,上面不小心按了 空格键,居然默认发出了。粘过来还没改好呢!Private Sub Combo1_Validate(Cancel As Boolean)
        If LenB(StrConv(txtVal, vbFromUnicode)) > 4  Then Cancel=true
    End Sub
      

  5.   

    还有错,再改:Private Sub Combo1_Validate(Cancel As Boolean)
        If LenB(StrConv(Combo1.Text, vbFromUnicode)) > 4  Then Cancel=true
    End Sub这样只能输入4个英文字,否则不能离开 combo1 控件。
    之所以控制中文字,是因为如果数据库设置了 10 个char长,输入 10 个汉字后可能会超长而出错。