Referencing Class Objects

I have a class which declares controls as

Public WithEvents txtName As TextBox

I need to be able to check that the class actually does have this control object. I will use many of these classes.

Ideally I want to be able to do is reference the classes controls like class("txtName") or something similar.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ok..  Your sort of on the right track and sort of not.. this is a very confusing matter that fried my mind when I first started with it..

Let me explain like this..

In your class module (call it MyClass):

public withevents txtname as TextBox

' from this point on, you can access it
' just like if you were to place a
' control on a form..

private sub txtname_GotFocus()
   txtname.selstart = 0
   txtname.sellength = len(txtname.text)
end sub

Now, on the form that your going to use this new class. create a new textbox (name it text1)

dim cMyClass as MyClass

private sub form_load()
   set cMyClass = new MyClass
       set cMyClass.txtName = Text1

At this point, all the event code that you have in your class module will now be "inherited" by the text1 textbox.

I have a standard textbox class that I use that I have written a "style" property into.. based on the value of that property, it will only allow upper case, lower case, or numeric only input.

if you need it for a TextBox - why not use ACtiveX Control or UserControl ?
you can add EVENTS for that too.
You can do it with a user control no problem.. but if you write and compile a seperate ActiveX control, than that's just one more thing to distribute with your application...
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

julianpointerAuthor Commented:
Hi datacop, thanks for your help but what im after is a way to assign dynamically controls that are created at run time to a class with the controls events.

All the controls are created dynamically on an instance of a child form. I then set the child form's controls it's class control(only if it exists in the class).
What I need to do is check that in the class there is an object called txtName that I will then set to the txtName created on the child form.


Try the CallByName function in VB6.
If the Property you are calling does not exist, you'll get a runtime error which you can trap.
julianpointerAuthor Commented:
No, this is for functions only.... I need to be able to check if an object exists in the class.
CallByName is not only for functions (RTFM!)

The way I see it, you have declared your object as a property of your class. Even though you declare it as a public variable, VB will translate this into Property Get and Property Set functions.

CallByName allows you to use Set, Get Let on Properties of a class as well as simply call its Functions.

Again, if the property does not exist in the class, you get a trappable runtime error.

Try this:
put a textbox on your form

Option Explicit
Public moExt As cExtender
Private Sub Form_Load()
    Set moExt = New cExtender
    CallByName moExt, "ExtTextBox", _
          VbSet, Text1
End Sub

Your class module looks like this:
Option Explicit

Public WithEvents ExtTextbox As TextBox

Private Sub ExtTextbox_Click()
    MsgBox "you clicked"
End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
julianpointerAuthor Commented:
Thanks twuyst,  works a treat... just what i wanted.
should it work in VB5 ?

-> cExtender is not defined.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.