If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

A UserControl makes an object of a Class, how can object ..?

In an ActiveX Control project, I have a working UserControl, say MyControl.
Because the code is complex, I would rather take most of it out of the UserControl
code window and make some classes, say Class1. So now MyControl makes an
instance of that class (set mrMyClass = New Class1). Now the problem. I
can't get the object, mrMyClass, to be able to talk back to the user control.
For instance, suppose I want a method in MyClass to change the size of a
constituent picture box.

Public Sub DoSize()
UserControl.picPic1.Width = 50
End Sub

This code does not work, neither does trying to pass the pic as a parameter,
neither does a zillion other things. Does anyone know how my object can
talk to my UserControl??

Re: A UserControl makes an object of a Class, how can object ..?

Using classes this way is a good idea. You will have to pass a reference
to your user control into the class:

In class

Public property Set Owner(RHS as MyControl)
Set mOwner=rhs
end sub

In Usercontrol

Sub UserControl_Initialize
Set mClass = New Class1
Set mClass.Owner=Me
End Sub

This will work, but it causes the problem of circular references - the class
has a reference to the usercontrol and the user control has a reference to
the class - so neither will fire the terminate even until the app closes.
To get around this store a non-counted reference to the usercontrol in the
class:

In the class:

Public Property Set Owner(ByVal RHS as MyControl)
mlngOwner = ObjPtr(RHS)
End Sub

Public Property Get Owner() as MyControl
Set Owner=ResolveObjPtr(mlngOwner)
End Property

Sub UserControl_Initialize
Set mClass = New Class1
Set mClass.Owner=Me
End Sub

Sub UserControl_Terminate
Set mClass.Owner=Nothing
Set mClass=Nothing
End sub

When inside the class use Owner to reference functions on the usercontrol

Owner.Repaint

UserControl:

Friend Sub Repaint()

End Sub

Hope this helps

Michael Culley

"don" <don@ottawa.com> wrote:
>
>In an ActiveX Control project, I have a working UserControl, say MyControl.
> Because the code is complex, I would rather take most of it out of the
UserControl
>code window and make some classes, say Class1. So now MyControl makes an
>instance of that class (set mrMyClass = New Class1). Now the problem.
I
>can't get the object, mrMyClass, to be able to talk back to the user control.
> For instance, suppose I want a method in MyClass to change the size of
a
>constituent picture box.
>
>Public Sub DoSize()
> UserControl.picPic1.Width = 50
>End Sub
>
>This code does not work, neither does trying to pass the pic as a parameter,
>neither does a zillion other things. Does anyone know how my object can
>talk to my UserControl??
>
>don
>
>