如何在自定义控件中新增一个事件呀?
我做了一个文本框控件,实现数据验证,我想增加一个change事件,可是不知如何做?
我的代码如下:
Option Explicit
'自定义文本框输入控件
'检测用户输入是否为数值Private m_Font As StdFont
Private m_blnchange As BooleanPrivate Sub m_Font_FontChanged(ByVal PropertyName As String)
    Set Text1.Font = m_Font
End SubPrivate Sub Text1_Change()
    m_blnchange = True
    If IsNumeric(Text1.Text) = False And Trim(Text1.Text) <> "-" And Trim(Text1.Text) <> "" And Trim(Text1.Text) <> "." Then
        MsgBox "输入中含有非法字符!", 16, ""
        Text1.SetFocus
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1.Text)
    End If
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
    Dim str As String    '取出按键以前的字符
    str = Trim(Text1.Text)    Select Case KeyAscii
    'Case 8, 9, 13, &H30 To &H39    '0---9的ascii码是 48--57
    '13----回车键
    '9-----?????
    '8----  退格键    Case 8, 13, 48 To 57
        If KeyAscii = 9 Then
            MsgBox ""
        End If        KeyAscii = KeyAscii    Case 45             '负号[只充许字符的第一个字符是负号]
        If str = "" Then
            KeyAscii = KeyAscii
        Else
            KeyAscii = 0
        End If    Case 46             '小数点处理[前面字符中没有小数点则可以输入]
                        '如果是空字符则可以输入小数点,即第一个字符可以以小数点开头
        If str = "" Then
            KeyAscii = KeyAscii
        Else
                        '如果不空则要判断是否以前没有小数点,不存在小数点则可以录入小数点
            If (IsNumeric(str) = True And InStr(1, str, ".") = 0) Then
                KeyAscii = KeyAscii
            Else
                KeyAscii = 0
            End If
        End If    Case Else
        KeyAscii = 0
    End Select
End SubPrivate Sub Text1_LostFocus()
    If IsNumeric(Text1.Text) = False And Trim(Text1.Text) <> "" Then
        MsgBox "输入中含有非法字符!", 16, ""
        Text1.SetFocus
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1.Text)
    End If
End Sub'text属性
Public Property Get Text() As String
    Text = Text1.Text
End PropertyPublic Property Let Text(ByVal NewValue As String)
    Text1.Text = NewValue
    PropertyChanged "Text"
End Property'changed属性
Public Property Get changed() As String
    changed = m_blnchange
End Property'Public Property Let changed(ByVal NewValue As String)
'    m_blnchange = NewValue
'    PropertyChanged "changed"
'End Property'lock属性
Public Property Get locked() As Boolean
    locked = Text1.locked
End PropertyPublic Property Let locked(ByVal NewValue As Boolean)
    Text1.locked = NewValue
    PropertyChanged "locked"
End Property
'为用户控件初始化属性
Private Sub UserControl_InitProperties()
    Text1.Text = ""
End SubPrivate Sub UserControl_Resize()
    Call SetTextBoxPos
End SubPrivate Sub UserControl_Terminate()
    Set m_Font = Nothing
    m_blnchange = False
End Sub'将属性值写到存储器
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    Call PropBag.WriteProperty("Text", Text1.Text, "")
    Call PropBag.WriteProperty("locked", Text1.locked, False)
End Sub'从存贮器中加载属性值
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    Text1.Text = PropBag.ReadProperty("Text", "")
    Text1.locked = PropBag.ReadProperty("locked", False)
End Sub'新增加方法
Private Sub SetTextBoxPos()
    With Text1
        .Left = 0
        .Top = 0
        .Width = UserControl.ScaleWidth
        .Height = UserControl.ScaleHeight
    End With
End SubPrivate Sub UserControl_Initialize()    Set m_Font = New StdFont
    m_Font.Name = "宋体"
    m_Font.Size = 9    Set Text1.Font = m_Font    SetTextBoxPos
End Sub

解决方案 »

  1.   

    我想实现这个文本框控件在用户键入字符时触发change事件.
      

  2.   

    我想加一个change事件,因为自定义的控件,默认是没有这个事件的.可是text控件是有这个事件的.所以想为我自定义的控件增加一个change事件
      

  3.   

    在 菜单 "工具" --> "添加过程" 中添加一个事件,比如名称:change。
    然后在 text1_change 事件中写:RaiseEvent change这样你在 text1 中修改时,就能触发控件的 change 事件