?
Solved

Referencing Class Objects

Posted on 1999-11-06
10
Medium Priority
?
234 Views
Last Modified: 2013-11-23
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.
0
Comment
Question by:julianpointer
  • 3
  • 2
  • 2
  • +2
10 Comments
 

Expert Comment

by:datacop
ID: 2189161
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.

0
 
LVL 5

Expert Comment

by:AnswerTheMan
ID: 2189711
if you need it for a TextBox - why not use ACtiveX Control or UserControl ?
you can add EVENTS for that too.
0
 

Expert Comment

by:datacop
ID: 2189836
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...
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 5

Expert Comment

by:AnswerTheMan
ID: 2189844
ohmygod
0
 
LVL 3

Author Comment

by:julianpointer
ID: 2189901
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.

0
 

Expert Comment

by:twuyts
ID: 2190978

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.
0
 
LVL 3

Author Comment

by:julianpointer
ID: 2239346
No, this is for functions only.... I need to be able to check if an object exists in the class.
0
 

Accepted Solution

by:
twuyts earned 400 total points
ID: 2239817
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
0
 
LVL 3

Author Comment

by:julianpointer
ID: 2241818
Thanks twuyst,  works a treat... just what i wanted.
0
 

Expert Comment

by:PIBM
ID: 2698653
should it work in VB5 ?

-> cExtender is not defined.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…
Suggested Courses

601 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