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


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

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

  1. #1
    don Guest

    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??

    don



  2. #2
    Michael Culley Guest

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


    Don,

    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

    Private Function ResolveObjPtr(ByVal Pointer as Long) as Object
    Dim objTemp as Object
    CopyMemory objTemp,Pointer,4
    Set ResolveObjPtr = objTemp
    CopyMemory objTemp,0&,4
    End Function



    In UserControl:

    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
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center