Solved

New Objects at runtime!!!

Posted on 2001-06-25
5
142 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copy a row 12 75
Hide vba in gp 7 125
Visual Studio 2005 text editor 10 54
Fastest way to find and count same items VB6 16 62
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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

739 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