Private WithEvents te As TextBoxPrivate Sub te_Change()
    On Error GoTo er
    If te.Text = "" Then te.Text = "0": te.SelStart = Len(te.Text)
er:
End SubPrivate Sub te_KeyPress(KeyAscii As Integer)
    On Error GoTo er
    If KeyAscii = 8 Then Exit Sub
    If KeyAscii = 46 And InStr(1, te.Text, ".") <> 0 Then KeyAscii = 0: Exit Sub
    If KeyAscii = 45 And InStr(1, te.Text, "-") <> 0 Then KeyAscii = 0: Exit Sub
    If KeyAscii = 45 And te.SelStart <> 0 Then KeyAscii = 0: Exit Sub
    If (KeyAscii < 45 Or KeyAscii > 57) Or KeyAscii = 47 Then KeyAscii = 0: Exit Sub
    If te.Text = "0" Then te.Text = Chr(KeyAscii): KeyAscii = 0: te.SelStart = Len(te.Text)
er:
    If Err.Number <> 0 Then KeyAscii = 0
End SubPublic Function NumericTextBox(TBox As Object)
On Error Resume Next
If TypeName(TBox) <> "TextBox" Then MsgBox "该函数只对TextBox控件操作", vbCritical, "Faib Studio": Exit Function
Set te = TBox
End Function对于没有设置Index的文本框可以
但对于多个数组的文本框就出错
如:
Private Sub Text1_GotFocus(Index As Integer)
NumericTextBox Text1(Index)
End Sub请问该如何改?

解决方案 »

  1.   

    一种可以实现的笨办法Private Sub te_Change(te As TextBox)
        On Error GoTo er
        If te.Text = "" Then te.Text = "0": te.SelStart = Len(te.Text)
    er:
    End SubPrivate Sub te_KeyPress(te As TextBox, KeyAscii As Integer)
        On Error GoTo er
        If KeyAscii = 8 Then Exit Sub
        If KeyAscii = 46 And InStr(1, te.Text, ".") <> 0 Then KeyAscii = 0: Exit Sub
        If KeyAscii = 45 And InStr(1, te.Text, "-") <> 0 Then KeyAscii = 0: Exit Sub
        If KeyAscii = 45 And te.SelStart <> 0 Then KeyAscii = 0: Exit Sub
        If (KeyAscii < 45 Or KeyAscii > 57) Or KeyAscii = 47 Then KeyAscii = 0: Exit Sub
        If te.Text = "0" Then te.Text = Chr(KeyAscii): KeyAscii = 0: te.SelStart = Len(te.Text)
    er:
        If Err.Number <> 0 Then KeyAscii = 0
    End SubPrivate Sub text1_Change(Index As Integer)
    Call te_Change(text1(Index))
    End Sub
    Private Sub text1_KeyPress(Index As Integer, KeyAscii As Integer)
    Call te_KeyPress(text1(Index), KeyAscii)
    End Sub
      

  2.   

    应该没有办法了,WithEvents无法声明给对象数组的