Solved

New Objects at runtime!!!

Posted on 2001-06-25
5
137 Views
Last Modified: 2010-05-02
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"
form1.controls("Text1").visible=true
form1.controls("Text1").top=1
form1.controls("Text1").left=10
end sub

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

public function TrueorFalse(byval int as integer) as Boolean

if int>10 then
 TrueOrFalse=false
else
TrueorFalse=true
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...!!  
0
Comment
Question by:shankarkrupa
  • 3
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
WalterM earned 40 total points
Comment Utility
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.

Example:

--- 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,

Michel
0
 
LVL 3

Author Comment

by:shankarkrupa
Comment Utility
Thank you very very, very, very, much...I do not know how to thank you now...Thank you sooooooooooooooo much...!!!
0
 
LVL 2

Expert Comment

by:WalterM
Comment Utility
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!

Michel
0
 
LVL 3

Author Comment

by:shankarkrupa
Comment Utility
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...
0
 
LVL 3

Author Comment

by:shankarkrupa
Comment Utility
"We never know what we do for others..." even if it is good, especially, here it is true at our Experts Exchange.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now