1,453
社区成员
发帖
与我相关
我的任务
分享Public WithEvents ctrl As VBControlExtender
Dim m_Parent As ControlItems
Sub Init(ctl As Object, parnt As ControlItems)
Set ctrl = ctl
Set m_Parent = parnt
End Sub
Private Sub Ctrl_Validate(Cancel As Boolean)
m_Parent.Notify_Validate Me, Cancel
End Sub
Event Validate(CtrlItem As ControlItem, Cancel As Boolean)
Private m_ControlItems As New Collection
Function Add(ctrl As Control) As ControlItem
Dim newItem As New ControlItem
newItem.Init ctrl, Me
m_ControlItems.Add newItem
Set Add = newItem
End Function
Friend Sub Notify_Validate(Item As ControlItem, Cancel As Boolean)
RaiseEvent Validate(Item, Cancel)
End Sub'Dim WithEvents CtrlItems As New ControlItems 文章上是这样的,但不行,会报错,要去掉New
Dim WithEvents CtrlItems As ControlItems
Private Sub cmdCreateControls_Click()
Dim ctrl As Control
Set ctrl = Controls.Add("VB.TextBox", "One")
ctrl.Move 100, 200, 1000, 300
ctrl.Visible = True
CtrlItems.Add ctrl
Set ctrl = Controls.Add("VB.TextBox", "Two")
ctrl.Move 100, 800, 1000, 300
ctrl.Visible = True
CtrlItems.Add ctrl
End Sub
Private Sub CtrlItems_Validate(CtrlItem As ControlItem, Cancel As Boolean)
If CtrlItem.ctrl.Text = "" Then Cancel = True
End Sub