Results 1 to 11 of 11

Thread: Private vs. Public User Control?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    182

    Private vs. Public User Control?

    Ok not sure if the title is the most descriptive one, but I have this problem. In a User Control (UC) I have the following
    Code:
    'UC Declaration section
    Private WithEvents mFrm As Form
    ...
    Private Sub UserControl_InitProperties()
    ..
    Set mFrm = Extender.Parent
    ...
    End Sub
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    ...
    Set mFrm = Extender.Parent
    ...
    End Sub
    But it causes the exception
    This component doesn't support this set of events (Error 459)
    Not every component supports client sinking of events. This error has the following cause and solution:

    • You tried to use a WithEvents variable with a component that can't work as an event source for the specified set of events. For example, you may be sinking events of an object, then create another object that Implements the first object. Although you might think you could sink the events from the implemented object, that isn't automatically the case. Implements only implements an interface for methods and properties.

      You can't sink events for a component that doesn't source events.
    • WithEvents isn't supported for Private UserControls, because the type-info needed to raise the ObjectEvent isn't available at runtime.
    I suspect it's the 2nd reason that applies here, although not sure. The idea is to hook or embed some of the form events inside the UC to run some code. Also, the idea is that the form shouldn't need to now if the UC is sited on it or not. Furthermore, the same code would need to run on any form UC is sited on, so this removes the need to add this code to each form UC is sited on.

    Question is though, can this be done at all? And if it's a question of Private vs. Public scope/instance of UC, how can Public be acheived? I also would like to add that, although I have done VB6 basically since its release I have never come to work with UCs until now, so maybe it's obvious in a way I don't see because my mind is trying to look at too many things at one time. But this UC is in an ocx project, is that what makes it private? Would putting it directly in the StandardEXE project achieve what I want?

    Greatful for any input or deriction to where I can find more and deeper info. I tried to google but didn't get anything relevant back.

    /Joakim
    M$ vs. VB6 = The biggest betrayal and strategic mistake of the century!?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Private vs. Public User Control?

    The problem is that the form isn't sourcing events. There are no form events while in design-mode, i.e., Form_MouseMove doesn't fire. Events are triggered during runtime. To avoid the error during design-time, you could test the usercontrol's Ambient.UserMode property, but that probably defeats your intent?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Private vs. Public User Control?

    BTW, a UserControl already has a Parent property that returns a reference to the Form/MDIForm on which it is sited, so there's no need to retrieve the Parent via the Extender property. A UserControl also has the ParentControls property which returns returns a collection of the other controls in the parent Form/MDIForm, so again, going through the longer route via the Extender property is unnecessary.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    182

    Re: Private vs. Public User Control?

    Quote Originally Posted by LaVolpe View Post
    The problem is that the form isn't sourcing events. There are no form events while in design-mode, i.e., Form_MouseMove doesn't fire. Events are triggered during runtime. To avoid the error during design-time, you could test the usercontrol's Ambient.UserMode property, but that probably defeats your intent?
    Yes that defeats my intent :-( So there is no way to detect if the form is resizing or has resized during design-time, not even using subclassing?
    M$ vs. VB6 = The biggest betrayal and strategic mistake of the century!?

  5. #5
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Private vs. Public User Control?

    Quote Originally Posted by 7edm View Post
    ... not even using subclassing?
    Subclassing the Form while in design mode is possible; I've just tested it.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    182

    Re: Private vs. Public User Control?

    Quote Originally Posted by Bonnie West View Post
    BTW, a UserControl already has a Parent property that returns a reference to the Form/MDIForm on which it is sited, so there's no need to retrieve the Parent via the Extender property. A UserControl also has the ParentControls property which returns returns a collection of the other controls in the parent Form/MDIForm, so again, going through the longer route via the Extender property is unnecessary.
    jiize you are right, I never reflected on that as the docs kept talking about the extender all the time... I sure will take that shortcut instead - Thanks!
    M$ vs. VB6 = The biggest betrayal and strategic mistake of the century!?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    182

    Re: Private vs. Public User Control?

    Quote Originally Posted by Bonnie West View Post
    Subclassing the Form while in design mode is possible; I've just tested it.
    Ok thanks for letting me know, I though it would be possible but don't want to start adding subclassing before I got the basics working as it will be much harder to debug then.
    M$ vs. VB6 = The biggest betrayal and strategic mistake of the century!?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    182

    Re: Private vs. Public User Control?

    Quote Originally Posted by Bonnie West View Post
    A UserControl also has the ParentControls property which returns returns a collection of the other controls in the parent Form/MDIForm.
    Just want to improve on the information here for the topic. While ParentControls is a property available to the UC it doesn't seems to work as expected at design-time or by use of 'For Each ctrl In UserControl.ParentControls' which triggers an exception 13, but 'For Each ctrl In Parent.Controls' works fine though. Might be a useful reward for someone using the site's search feature ;-)
    M$ vs. VB6 = The biggest betrayal and strategic mistake of the century!?

  9. #9
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Private vs. Public User Control?

    Quote Originally Posted by 7edm View Post
    While ParentControls is a property available to the UC it doesn't seems to work as expected at design-time or by use of 'For Each ctrl In UserControl.ParentControls' which triggers an exception 13, ...
    A "Type mismatch" error suggests that your ctrl variable is not of the correct data type. It should be dimensioned As Object:

    Code:
    Option Explicit 'In a new UserControl
    
    Private Sub UserControl_InitProperties()
        Dim Ctrl As Object
    
        For Each Ctrl In ParentControls
            Debug.Print """"; TypeName(Ctrl); """", """"; Ctrl.Name; """"
        Next
    
    
        Dim i As Long       'For...Next loop works too, but is less efficient
    
        With ParentControls
            For i = 0& To .Count - 1&
                Debug.Print """"; TypeName(.Item(i)); """", """"; .Item(i).Name; """"
            Next
        End With
    End Sub
    Note that the ParentControls "collection functions in a similar manner to the Controls collection on the form, but will also contain the form itself." If you don't want the parent Form to be included while enumerating its controls, you'll have to use the Parent.Controls collection instead.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    182

    Re: Private vs. Public User Control?

    Yes, I figured that out, and unless (as you say) you don't need the form, Parent.Controls is better imo as you can dimension the iterate variable As Control, which doesn't work with ParentControls in which case you have to dimension it As Object, which may be slower - but I haven't tested that as Parent.Controls with ctrl As Controls seems to be very fast ( 172 controls in < 1 ms on my machine).
    M$ vs. VB6 = The biggest betrayal and strategic mistake of the century!?

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Private vs. Public User Control?

    Quote Originally Posted by 7edm View Post
    And if it's a question of Private vs. Public scope/instance of UC, how can Public be acheived?
    While this doesn't help your situation it might help future readers lured in by your thread title:

    "Public" in this sense means "published." In practical terms this means compiled into an ActiveX server (DLL/OCX or EXE) and registered so that it has a discoverable type library. But it can also include cases where there is no external server as long as the UserControl Implements a published interface.

    The key being that registered "type" information exists.

    None of it has anything to do with the smaller use of Public vs. Private (i.e. scope decorations, e.g. Public Sub X, Friend Function Y, Private Z As Byte).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width