assuming vb6, you'll need to create a wrapper class for the TextBox object. You'll need to initialise that class with the instance of your dynamically-created textbox, and then consume within it the TextBox control events
something like (for a wrapper class called "MyTextBoxWrapper"
Code:
Option Explicit
Private Withevents mBaseControl as Vb.TextBox
Public Sub init(Byval TextBoxControl As Vb.TextBox)
set mBaseControl= TextBoxControl
End Sub
Private sub mBaseControl_Change()
Debug.Print mBaseControl.Name & " raised the Change event"
End Sub
Private sub mBaseControl_KeyPress(keyascii As Integer)
Debug.Print mBaseControl.Name & " raised the KeyPress event [Key '" & Chr$(KeyAscii) & "' pressed]"
End Sub
Private sub mBaseControl_Validate(cancel As Boolean)
Debug.Print mBaseControl.Name & " raised the Validate event"
End Sub
and for your Forms object (e.g. "Forms1")
Code:
Option explicit
Private mTextBoxWrapper As MyTextBoxWrapper
Private Sub Form_Load()
dim tb As VB.TextBox
Set tb = Me.Controls.Add("Vb.TextBox", "txtDynamicTextbox1")
tb.Visible = True
set mTextBoxWrapper = New MyTextBoxWrapper
call mTextBoxWrapper.init(tb)
End Sub