Results 1 to 15 of 15

Thread: Problem with InitProperties event

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Problem with InitProperties event

    My control doesn't remember the properties it was "inited" with. Lets say I have an internal property defined and initialized like this:
    Code:
    Dim MyProp As Long
    Private Sub UserControl_InitProperties()
    MyProp = 10
    End Sub
    Now when I put a breakpoint in, and add this control to my form, the instant it is added to the form I get to see it execute the InitProperties event. And get to go step-by-step as it puts the value 10 in the variable MyProp. Problem is, when I then run the program, and then check the value stored in the variable MyProp, I see it is equal to 0 (zero). It seems that when I went from Design Time to Run Time, the variable MyProp "forgot" the value that it was initialized to at design time. How do I fix this?

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,713

    Re: Problem with InitProperties event

    Well someone more familiar with UserControls might know more specifically.. but if InitProperties works for other controls with other variables, I'd say it's because outside of a procedure you shouldn't use Dim, and use Private MyProp As Long instead.

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

    Re: Problem with InitProperties event

    InitProperties is only meant to be used for setting default (initial) values when an instance of the UserControl is created at design-time.

    You need to provide handlers for the ReadProperties and WriteProperties events. It is all covered in the manual.

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

    Re: Problem with InitProperties event

    Yepper, InitProperties only occurs once in the lifetime of a usercontrol: first time it is created. Afterwards, ReadProperties is used for retrieving cached values saved during the WriteProperties event.

    If you want a value set for every instance of the control, set it in the Initialize event. That always occurs.
    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}

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: Problem with InitProperties event

    Quote Originally Posted by LaVolpe View Post
    Yepper, InitProperties only occurs once in the lifetime of a usercontrol: first time it is created.
    Indeed it does only occur once. On account of this, it is supposed to remember what it was initialized to, unless it is specifically changed by another line of code to something else later on. InitProperties is supposed to cause them to be initialized when they are placed on a form at design time, so that they will be remembered ever after that throughout both design time AND run time.

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

    Re: Problem with InitProperties event

    Quote Originally Posted by Ben321 View Post
    Indeed it does only occur once. On account of this, it is supposed to remember what it was initialized to, unless it is specifically changed by another line of code to something else later on. InitProperties is supposed to cause them to be initialized when they are placed on a form at design time, so that they will be remembered ever after that throughout both design time AND run time.
    No, that is your interpretation of what you think the event does. The properties of a usercontrol are well documented on MSDN.

    To persist properties, you save each of those during the WriteProperites event, then apply them during control loading via the ReadProperties event. Since a control, when first born, had no previous WriteProperites event, this is where InitProperites comes into play. The Write then Read pattern is constant thereafter. This is pretty straightforward stuff.
    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}

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

    Re: Problem with InitProperties event

    There is an article in the manual called something like "Understanding Control Lifetime and Key Events" in the "Control Creation Basics" section that you may want to read.

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

    Re: Problem with InitProperties event

    Quote Originally Posted by fafalone View Post
    ... I'd say it's because outside of a procedure you shouldn't use Dim, and use Private MyProp As Long instead.
    Quote Originally Posted by MSDN
    Variables Used Within a Module

    By default, a module-level variable is available to all the procedures in that module, but not to code in other modules. You create module-level variables by declaring them with the Private keyword in the Declarations section at the top of the module. For example:

    Private intTemp As Integer

    At the module level, there is no difference between Private and Dim, but Private is preferred because it readily contrasts with Public and makes your code easier to understand.
    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)

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: Problem with InitProperties event

    Quote Originally Posted by LaVolpe View Post
    No, that is your interpretation of what you think the event does. The properties of a usercontrol are well documented on MSDN.

    To persist properties, you save each of those during the WriteProperites event, then apply them during control loading via the ReadProperties event. Since a control, when first born, had no previous WriteProperites event, this is where InitProperites comes into play. The Write then Read pattern is constant thereafter. This is pretty straightforward stuff.


    So here's the code I have so far.
    Code:
    Private MyInternalProp As Long
    ' MyInternalProp is a property that is not to be changed by anything outside the user control, but may be changed internally by additional code within the user control.
    ' It is to have an initial value of 10 the moment the control is dropped on the form, and must remain 10 during all other times during both design-time and at the start of run-time, and only be changed after that if some run-time code tells it to change.
    
    Private Sub UserControl_InitProperties()
    MyInternalProp=10
    UserControl.PropertyChanged "MIP"
    End Sub
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    MyInternalProp=PropBag.ReadProperty("MIP")
    End Sub
    
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    PropBag.WriteProperty "MIP", MyInternalProp
    End Sub


    Unfortunately, while it appears to work to start with, when I add the control to the form, I end up getting an error when I try to delete the control from the form. Attempting to delete the control from the form causes "Run-time error '425': Invalid object use" in the event UserControl_WriteProperties. However it does successfully delete the control despite this error. I also get the same error (and again it's in UserControl_WriteProperties) when I try to actually run the program with this control on the form.

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

    Re: Problem with InitProperties event

    Code:
    Private Sub UserControl_InitProperties()
    MyInternalProp=10
    UserControl.PropertyChanged "MIP"
    End Sub
    PropertyChanged not needed, because the default value can be read during ReadProperites...
    Code:
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    MyInternalProp=PropBag.ReadProperty("MIP", 10)
    End Sub
    Likewise, the default value for MIP can be added to WriteProperties so that it isn't written (waste of space) if it is the default
    Code:
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    PropBag.WriteProperty "MIP", MyInternalProp, 10
    End Sub
    Though the value 10 is hardcoded, it could also be a control-level constant if you prefer, i.e., DEFAULT_MIP = 10.

    Regarding the error. I am assuming the UC WriteProperties code you posted isn't al the code you are actually using. I don't see an error there. I've never experienced such an error. Per MSDN, a reason for that specific error could be "You tried to use the PropertyBag object outside of the ReadProperties and WriteProperties events of a User Control."
    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}

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: Problem with InitProperties event

    Quote Originally Posted by LaVolpe View Post
    Code:
    Private Sub UserControl_InitProperties()
    MyInternalProp=10
    UserControl.PropertyChanged "MIP"
    End Sub
    PropertyChanged not needed, because the default value can be read during ReadProperites...
    Code:
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    MyInternalProp=PropBag.ReadProperty("MIP", 10)
    End Sub
    Likewise, the default value for MIP can be added to WriteProperties so that it isn't written (waste of space) if it is the default
    Code:
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    PropBag.WriteProperty "MIP", MyInternalProp, 10
    End Sub
    Though the value 10 is hardcoded, it could also be a control-level constant if you prefer, i.e., DEFAULT_MIP = 10.

    Regarding the error. I am assuming the UC WriteProperties code you posted isn't al the code you are actually using. I don't see an error there. I've never experienced such an error. Per MSDN, a reason for that specific error could be "You tried to use the PropertyBag object outside of the ReadProperties and WriteProperties events of a User Control."


    In notice your sample code doesn't include any use of the InitProperties event. Can you explain proper usage of that event in the example code that I've given?

    And as for it not being all the code that is used, indeed there is only a tiny bit more. I used the With statement in order to set multiple properties without having to type PropBag more than once. In write properties it looks more like:
    Code:
    With PropBag
    .WriteProperty "InternalProp1", InternalProp1
    .WriteProperty "InternalProp2", InternalProp2
    .WriteProperty "InternalProp3", InternalProp2
    End With
    In read properties, it looks like:
    Code:
    With PropBag
    InternalProp1 = .ReadProperty("InternalProp1")
    InternalProp2 = .ReadProperty("InternalProp2")
    InternalProp3 = .ReadProperty("InternalProp3")
    End With
    Unless the With statement doesn't work with property bags, then I don't see any problem with the code I'm using here.
    Last edited by Ben321; Jan 2nd, 2016 at 11:39 PM.

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

    Re: Problem with InitProperties event

    I use WITH all the time with the PropertyBag object. Error must be related to something else? Latest service pack installed? Usually a good question to ask when funky usercontrol errors pop up.

    In notice your sample code doesn't include any use of the InitProperties event. Can you explain proper usage of that event in the example code that I've given?
    Wasn't my sample code. I used what you supplied. Regarding InitProperites... Many times it isn't necessary to even code for that event. 99% of the time the default values of your usercontrol properties are standard default vartype values: zero, false, vbNullString, etc, etc. If whatever variable, exposed by your properties, should be other than the default when the control is created, set the property in that event. When I code that event, if needed, it's usually just a couple lines of code.

    For example, in one my controls, I offer a DPIAwareness property that is set from a Public Enum. The 1st enum is: 0 - NotDPIAware and the last is FullyDPIAware. Well, during creation, I want that property's value to be FullyDPIAware, so I set it in InitProperties. FullyDPIAware is the default value during the .ReadProperty & .WriteProperty calls from the PropertyBag. If I were to reorganize my Enum and make FullyDPIAware have a value of zero, I wouldn't need to set it during InitProperties

    P.S. Suggest using the default property values in your .ReadProperty & .WriteProperty calls. If the variable being written to the bag is the optional default value, then the property is not written. If you had 50 properties being written and all were default, there would be no extra data stored to the host's frx file for those properties. Without using that default option, all 50 would be written.

    And just in case it was overlooked in my 1st reply. The usercontrol's Initialize event is called every time & is a good place to set values that are 'constant' and/or create/instantiate objects. The Terminate event would be used to release/destroy those objects

    Edited: A working example of one of my WriteProperties events (under construction). Notice the usage of WITH. No problems on my end. Also included my InitProperties event
    Code:
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        With PropBag
            .WriteProperty "Settings", m_Settings, &H107000
            .WriteProperty "SettingsEx", m_Settings2, 0&
            If (Me.DPIAwareness And iexDpiAwareScreen) = 0& Then .WriteProperty "TPP", Screen.TwipsPerPixelX, 15!
            .WriteProperty "WkspcX", m_Wkspace.X, 0!
            .WriteProperty "WkspcY", m_Wkspace.Y, 0!
            .WriteProperty "OffsetsX", m_Offsets.X, 0!
            .WriteProperty "OffsetsY", m_Offsets.Y, 0!
            If Me.Aspect = iexLockedAspect Then
                .WriteProperty "ScalerX", m_LockAspect.X, 0!
                .WriteProperty "ScalerY", m_LockAspect.Y, 0!
            End If
            .WriteProperty "BdrStyle", m_Borders, 0&
            .WriteProperty "Angle", m_Angle, 0!
            .WriteProperty "BkColor", m_BackColor, vbWindowBackground Or &H64000000
            .WriteProperty "BdrColor", m_BorderColor, vbWindowFrame Or &H64000000
        End With
    End Sub
    
    Private Sub UserControl_InitProperties()
        m_BackColor = vbWindowBackground Or &H64000000
        m_BorderColor = vbWindowFrame Or &H64000000
        m_Borders = Screen.TwipsPerPixelX * &H10&
        m_Settings = &H107000   ' iexHTAll Or iexDpiFullyAware Or AlignCenter (ensure same value in Read/Write props)
    End Sub
    
    Private Sub UserControl_Initialize()
        Set m_Image = New IImage
    End Sub
    Last edited by LaVolpe; Jan 3rd, 2016 at 12:24 AM.
    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}

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: Problem with InitProperties event

    Quote Originally Posted by LaVolpe View Post
    I use WITH all the time with the PropertyBag object. Error must be related to something else? Latest service pack installed? Usually a good question to ask when funky usercontrol errors pop up.

    Wasn't my sample code. I used what you supplied. Regarding InitProperites... Many times it isn't necessary to even code for that event. 99% of the time the default values of your usercontrol properties are standard default vartype values: zero, false, vbNullString, etc, etc. If whatever variable, exposed by your properties, should be other than the default when the control is created, set the property in that event. When I code that event, if needed, it's usually just a couple lines of code.

    For example, in one my controls, I offer a DPIAwareness property that is set from a Public Enum. The 1st enum is: 0 - NotDPIAware and the last is FullyDPIAware. Well, during creation, I want that property's value to be FullyDPIAware, so I set it in InitProperties. FullyDPIAware is the default value during the .ReadProperty & .WriteProperty calls from the PropertyBag. If I were to reorganize my Enum and make FullyDPIAware have a value of zero, I wouldn't need to set it during InitProperties

    P.S. Suggest using the default property values in your .ReadProperty & .WriteProperty calls. If the variable being written to the bag is the optional default value, then the property is not written. If you had 50 properties being written and all were default, there would be no extra data stored to the host's frx file for those properties. Without using that default option, all 50 would be written.

    And just in case it was overlooked in my 1st reply. The usercontrol's Initialize event is called every time & is a good place to set values that are 'constant' and/or create/instantiate objects. The Terminate event would be used to release/destroy those objects

    Edited: A working example of one of my WriteProperties events (under construction). Notice the usage of WITH. No problems on my end. Also included my InitProperties event
    Code:
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        With PropBag
            .WriteProperty "Settings", m_Settings, &H107000
            .WriteProperty "SettingsEx", m_Settings2, 0&
            If (Me.DPIAwareness And iexDpiAwareScreen) = 0& Then .WriteProperty "TPP", Screen.TwipsPerPixelX, 15!
            .WriteProperty "WkspcX", m_Wkspace.X, 0!
            .WriteProperty "WkspcY", m_Wkspace.Y, 0!
            .WriteProperty "OffsetsX", m_Offsets.X, 0!
            .WriteProperty "OffsetsY", m_Offsets.Y, 0!
            If Me.Aspect = iexLockedAspect Then
                .WriteProperty "ScalerX", m_LockAspect.X, 0!
                .WriteProperty "ScalerY", m_LockAspect.Y, 0!
            End If
            .WriteProperty "BdrStyle", m_Borders, 0&
            .WriteProperty "Angle", m_Angle, 0!
            .WriteProperty "BkColor", m_BackColor, vbWindowBackground Or &H64000000
            .WriteProperty "BdrColor", m_BorderColor, vbWindowFrame Or &H64000000
        End With
    End Sub
    
    Private Sub UserControl_InitProperties()
        m_BackColor = vbWindowBackground Or &H64000000
        m_BorderColor = vbWindowFrame Or &H64000000
        m_Borders = Screen.TwipsPerPixelX * &H10&
        m_Settings = &H107000   ' iexHTAll Or iexDpiFullyAware Or AlignCenter (ensure same value in Read/Write props)
    End Sub
    
    Private Sub UserControl_Initialize()
        Set m_Image = New IImage
    End Sub


    Turns out that in my actual program, I was mistakenly using PropBag.WriteProperty in the ReadProperties event, and PropBag.ReadProperty in the WriteProperties event. The sample code I posted here in my opening post was not in fact copied from my actual program, but rather written from scratch in the forum, to demonstrate what I was trying to accomplish in my program. I did this to avoid putting unnecessary pieces of code from my program into my thread, which had nothing to do with the problem I was facing in my program. I had assumed my code wasn't working due to some bug in VB6 itself (maybe even a bug that's well known by other VB6 users), and that you could point me in the direction I'd need to go for a workaround to that bug. It hadn't occurred to me that I'd made an error in what I'd typed, because everything looked right in my actual program's code. However as I was visually skimming over my code, it seems that I had missed the mistake that I had in my code, a mistake that wasn't duplicated in my thread's opening post, as I actually got the code correct when I typed it from scratch for the purpose of posting in this thread.

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

    Re: Problem with InitProperties event

    One reason why it's always good to post actual code when errors are reported. Glad you got that figured out.
    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}

  15. #15
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Re: Problem with InitProperties event

    Quote Originally Posted by Ben321 View Post
    Turns out that in my actual program, I was mistakenly using PropBag.WriteProperty in the ReadProperties event, and PropBag.ReadProperty in the WriteProperties event. The sample code I posted here in my opening post was not in fact copied from my actual program, but rather written from scratch in the forum, to demonstrate what I was trying to accomplish in my program. I did this to avoid putting unnecessary pieces of code from my program into my thread, which had nothing to do with the problem I was facing in my program. I had assumed my code wasn't working due to some bug in VB6 itself (maybe even a bug that's well known by other VB6 users), and that you could point me in the direction I'd need to go for a workaround to that bug. It hadn't occurred to me that I'd made an error in what I'd typed, because everything looked right in my actual program's code. However as I was visually skimming over my code, it seems that I had missed the mistake that I had in my code, a mistake that wasn't duplicated in my thread's opening post, as I actually got the code correct when I typed it from scratch for the purpose of posting in this thread.
    ... you can always refer to Lavolpe's LaVolpe Custom Button Template [20 Feb 09] which set standard of coding.

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