Solved

New Objects at runtime!!!

Posted on 2001-06-25
5
139 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
ID: 6224344
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
ID: 6224414
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
ID: 6230547
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
ID: 6238811
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
ID: 6238813
"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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

785 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