既它只能输入指定长度的文本;只能输入数字,不能输入其它的字符,
应该对何事件编程或控制何属性值?

解决方案 »

  1.   

    在keyPress事件或keyDown中编写代码。
      

  2.   

    1、控制长度
    用len函数
    if len(text1.txt)>5 then
        msgbox "只允许输入5个字符"
    end if
    2、非数字字符
    在文本框的keypress事件里控制他,只让他能输入aci码是数字的字符(48-57)
      

  3.   

    问题1、maxlength属性=控制长度
    问题2、同上
      

  4.   

    text1.maxlength=intmaxlength
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If (KeyAscii < Asc(0) Or KeyAscii > Asc(9)) And KeyAscii <> 8 Then
            KeyAscii = 0
        End If
    End Sub
      

  5.   

    谢谢,的确可通过maxlength控制长度,
    那组合框呢?好象没这样的属性值。稍后结
    贴给分。
      

  6.   

    你可以试试 MaskEdit 这个控件.
      

  7.   

    代码控制长度的方法:
    Private Sub Text1_Change()
        If Len(Text1.Text) >= 5 Then
            Text1.Text = Left$(Text1.Text, 5)
            Text1.SelStart = Len(Text1.Text)
        End If
    End Sub
      

  8.   

    组合框可以在他的keypress或change中用len来控制输入的长度,超过指定的字符就不让继续输入
    Private Sub Combo1_KeyPress(KeyAscii As Integer)
        if len(combo1.text)>5 then keyascii=0
    End Sub

    Private Sub Combo1_Change()
        dim temp  as string
        if len(combo1.text)>5 then 
            combo1.text=temp
        else
            temp=combo1.text
        end if
    End Sub
      

  9.   

    我也是一个初学者,刚刚给你实验完的,好用的@_@Option Explicit
    Dim comp As BooleanPrivate Sub Form_Load()
        Text1 = ""
    End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
       comp = Chr(KeyAscii) Like "[0-9]"
        If comp = False Or Len(Text1) > 3 Then'如果你设的长度为5,你就改为len(Text1)>4
            KeyAscii = 0                      '你也可以用maxlength设置长度
        End If
    End Sub
      

  10.   

    谢谢给出代码的各位。方法都不错,有几点小小的问题:
    horsefly()   dim temp  as string
                 if len(combo1.text)>5 then 
                   combo1.text=temp
                 else
                 temp=combo1.text
                 end if  好象不对吧,是我没看懂?
    freezx(freezx) 不是初学者了,都两个星啦,你的代码好象有
                   这样的问题:运行后文本框内的内容不能用退
                   格键进行删除,至少在俺的机器上运行是如此。
    xsp(半个程序员) 在我的另一个问题也得到你的指点,要说?吗。
    加分,明日分配分数。
      

  11.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
       if keyascii=8 then exit sub
       
       comp = Chr(KeyAscii) Like "[0-9]"
        If comp = False Or Len(Text1) > 3 Then'如果你设的长度为5,你就改为len(Text1)>4
            KeyAscii = 0                      '你也可以用maxlength设置长度
        End If
    End Sub
      

  12.   

    那个是我粗略写的,因该要控制的更严谨一些。
    你也可以用这个,弥补了combobox没有Maxlength的缺陷。
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const CB_LIMITTEXT = &H141
    'set max length of text of combobox
    'hWnd       handle of combobox
    'Maxlength  allow max length of input
    'ReturnValue:
    '   >0 success
    '   elsewise fail
    Public Function SetMaxLength(ByVal hWnd As Long, Optional Maxlength As Integer = 0) As Long
        Dim Rtn&
     
        Rtn = SendMessage(hWnd, CB_LIMITTEXT, Maxlength, 0)
        SetMaxLength = Rtn
    End Function
    这个可以看得懂把^_^
      

  13.   

    horsefly(),不瞒您说,还真看不懂 ^_^,惭愧。
    基本上 IwantFlay(我会等她!!!) ,freezx(freezx) 的方法就
    已搞定此问题,长了不少知识,谢谢。
    结贴。