Link to home
Start Free TrialLog in
Avatar of julianpointer
julianpointer

asked on

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.
Avatar of datacop
datacop
Flag of United States of America image

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.

Avatar of AnswerTheMan
AnswerTheMan

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...
ohmygod
Avatar of julianpointer

ASKER

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.
No, this is for functions only.... I need to be able to check if an object exists in the class.
ASKER CERTIFIED SOLUTION
Avatar of twuyts
twuyts

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks twuyst,  works a treat... just what i wanted.
should it work in VB5 ?

-> cExtender is not defined.