Solved

New Objects at runtime!!!

Posted on 2001-06-25
5
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

691 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