New Objects at runtime!!!

How can an event can be fired for an object that is loaded during runtime...?

For example,

Supposing I have a commandbutton...
When that command button is clicked, a textbox should be added, which I do like this:

private sub command1_Click()
form1.controls.add "VB.Textbox","Text1"
end sub

So, with that, I have a function....

public function TrueorFalse(byval int as integer) as Boolean

if int>10 then
end if
End Function

I want this function to be called when the user enters some text in the textbox....How can this be done?  How can we track and fire the text1_Change event in this case?

Thanks in advance...!!  
Who is Participating?
WalterMConnect With a Mentor Commented:
Use the WithEvents keyword to declare a module-level object variable. If you want to sink events for different types of controls, declare the variable As VBControlExtender, otherwise simply use As TextBox.


--- code starts here ---

Option Explicit

Private WithEvents mCtl As TextBox

Private Sub command1_Click()
    Set mCtl = Form1.Controls.Add("VB.Textbox", "Text1")
    With mCtl
        .Visible = True
        .Move 1, 10
    End With
End Sub

Private Sub mCtl_Change()
    Debug.Print "Change"
    ' Call your validation function here
End Sub

--- code ends here ---

Hope this helps,

shankarkrupaAuthor Commented:
Thank you very very, very, very, much...I do not know how to thank you now...Thank you sooooooooooooooo much...!!!
I'm a little overwhelmed by your gratitude, shankarkrupa. It really isn't such an obscure technique, you know. Just search the MSDN for the WithEvents and VBControlExtender keywords, you'll find more examples.

Anyway, glad I could help so quickly!

shankarkrupaAuthor Commented:
May be...But, all those I can do only if I know about this Withevents and vbcontrolextender keywords...Without you,I wouldnot have learnt about that...
shankarkrupaAuthor Commented:
"We never know what we do for others..." even if it is good, especially, here it is true at our Experts Exchange.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.