Darren Bartrup
Well-known Member
- Joined
- Mar 13, 2006
- Messages
- 1,297
- Office Version
- 365
- Platform
- Windows
Hi all,
I'm having trouble capturing events on controls created at run-time.
I create a form and add two text boxes.
The code behind TextBox1 is:
Now, when I update the info and go to exit the control the message box is displayed.
--------------------
Next, I add a frame to the form called Frame1 and a class module called Class1.
I add this code to Class1:
Finally, I add this code to the form:
The problem is that the Double-Click event is captured and fires as expected, the Change event is captured and fires but the BeforeUpdate event and Enter events are apparently ignored.
Does anyone have any idea why these events are ignored?
Any help is greatly appreciated.
I'm having trouble capturing events on controls created at run-time.
I create a form and add two text boxes.
The code behind TextBox1 is:
Code:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
MsgBox "Before Update"
End Sub
--------------------
Next, I add a frame to the form called Frame1 and a class module called Class1.
I add this code to Class1:
Code:
Public WithEvents txtBx As MSForms.TextBox
Private fme As Frame
'Doesn't work.
Private Sub txtBx_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
MsgBox "Before Update"
End Sub
'Works.
Private Sub txtBx_Change()
MsgBox "Change"
End Sub
'Works.
Private Sub txtBx_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
MsgBox "Double Click"
End Sub
'Doesn't work.
Private Sub txtBx_Enter()
MsgBox "Enter."
End Sub
Public Property Set ContainerFrame(FrameReference As Frame)
Set fme = FrameReference
End Property
Public Sub AddNewTextBox()
Dim ctrlTextBox As Control
Set ctrlTextBox = fme.Controls.Add("Forms.TextBox.1", "MyTextBox1")
With ctrlTextBox
.Left = 10
.Height = 24
.Width = 100
.Top = 1 * (24 + 5) + 12
.Text = "Starting Value"
End With
Set txtBx = ctrlTextBox
End Sub
Finally, I add this code to the form:
Code:
Public TextBoxEvent As Collection
Private Sub UserForm_Initialize()
Dim NewTextBox As Class1
Dim NewControl As Control
Set TextBoxEvent = New Collection
Set NewTextBox = New Class1
With NewTextBox
Set .ContainerFrame = Me.Frame1
.AddNewTextBox
End With
TextBoxEvent.Add NewTextBox
End Sub
The problem is that the Double-Click event is captured and fires as expected, the Change event is captured and fires but the BeforeUpdate event and Enter events are apparently ignored.
Does anyone have any idea why these events are ignored?
Any help is greatly appreciated.