dcsimg
Results 1 to 16 of 16

Thread: To write UserControl's properties at any given time?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    976

    To write UserControl's properties at any given time?

    The UserControls properties are saved in the WriteProperties event, and it fires only when the form is closed in the designer.

    The problem is that when it is used within an standard exe project, if the developer opens the Usercontrol's designer window, all the instances of the UserControl that are on forms lose their changed properties settings.

    Is there any way to force to write the properties at any given time.
    I guess not, but it doesn't cost me anything to ask.

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,516

    Re: To write UserControl's properties at any given time?

    It's also called when the form is saved to disk. Not sure if that helps though.

    If you want to see this, just right-click and "Save" any form with your UC on it (and have a debug.print in the WriteProperties event). It always fires when the form is saved.

    Also ...

    Quote Originally Posted by Eduardo- View Post
    The problem is that when it is used within an standard exe project, if the developer opens the Usercontrol's designer window, all the instances of the UserControl that are on forms lose their changed properties settings.
    ... I don't seem to have that problem. Here's code I put into a UC for testing:

    Code:
    
    Option Explicit
    '
    Dim mlASDF As Long
    '
    
    Public Property Get asdf() As Long
        asdf = mlASDF
    End Property
    
    Public Property Let asdf(l As Long)
        mlASDF = l
        PropertyChanged "asdf"
    End Property
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        mlASDF = PropBag.ReadProperty("asdf", 0&)
    
    End Sub
    
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        Debug.Print "WriteProperties"
    
        PropBag.WriteProperty "asdf", mlASDF
    End Sub
    
    
    And then, I threw that UC onto a Form1, set the asdf property to 5 in the Properties Window, and then opened the UC, messed with it, closed it, and the Form1 still showed asdf=5. So, I'm confused.

    All The Best,
    Elroy

    EDIT1: Also, my main project has tons of UCs used on it, and I do occasionally tweak them. And I've never had a problem with custom property values disappearing. In one control in particular, I do have a bit of trouble with a RTB that's on the UC, but that's really a separate issue. All works fine so long as I don't tamper with things. It's just some minor bug I've never tracked down.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    976

    Re: To write UserControl's properties at any given time?

    Quote Originally Posted by Elroy View Post
    It's also called when the form is saved to disk. Not sure if that helps though.

    If you want to see this, just right-click and "Save" any form with your UC on it (and have a debug.print in the WriteProperties event). It always fires when the form is saved.
    Sorry, but that solves nothing, Elroy.
    The developer will never remember to do that before opening the UserControl's designer window.
    BTW: I'm quite used to close the forms before opening an UserControl's designer because of that problem, but I don't always remember to do so either.

    Quote Originally Posted by Elroy View Post
    Also ...



    ... I don't seem to have that problem. Here's code I put into a UC for testing:

    Code:
    
    Option Explicit
    '
    Dim mlASDF As Long
    '
    
    Public Property Get asdf() As Long
        asdf = mlASDF
    End Property
    
    Public Property Let asdf(l As Long)
        mlASDF = l
        PropertyChanged "asdf"
    End Property
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        mlASDF = PropBag.ReadProperty("asdf", 0&)
    
    End Sub
    
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        Debug.Print "WriteProperties"
    
        PropBag.WriteProperty "asdf", mlASDF
    End Sub
    
    
    And then, I threw that UC onto a Form1, set the asdf property to 5 in the Properties Window, and then opened the UC, messed with it, closed it, and the Form1 still showed asdf=5. So, I'm confused.
    I have exactly the opposite experience.
    If I do that, to set the property to 5, then open the UserControl's designer window, close it. The 5 is gone, the property is back to its prior value.

    Also, I mean the visual designer, not the UserControl's code window.
    If you open the code window and don't change the code, there is no problem. But if you change the UserControl's code in any way, the problem also happens.

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,516

    Re: To write UserControl's properties at any given time?

    Wow, using my example above, I had my Form1 open. Then, I opened the UC (designer) and placed a new label on this UC. While doing that, it hatched-out the UC on Form1, but that's to be expected. But when I close the UC's designer window, I checked my asdf property, and it was still 5.

    I also tried it without Form1 open, with the same results. I also tried opening Form1 while the UC's designer window was open, and it opened Form1 but auto-closed the UC's designer window. But asdf still = 5.

    Could you provide an example of what you're talking about, maybe with the steps to take to see the problem?
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    976

    Re: To write UserControl's properties at any given time?

    Quote Originally Posted by Elroy View Post
    Wow, using my example above, I had my Form1 open. Then, I opened the UC (designer) and placed a new label on this UC. While doing that, it hatched-out the UC on Form1, but that's to be expected. But when I close the UC's designer window, I checked my asdf property, and it was still 5.

    I also tried it without Form1 open, with the same results. I also tried opening Form1 while the UC's designer window was open, and it opened Form1 but auto-closed the UC's designer window. But asdf still = 5.

    Could you provide an example of what you're talking about, maybe with the steps to take to see the problem?
    Please folow exactly these steps:

    Let's suppose that you already have an UserControl added to form1.

    Open form1:
    See what value does the property "asdf" have.
    If it is 5, then change it to 10, if it is 6, then change it to 10, if it is other, change it to 10.
    Remember that value, the last one that you put (it was 10).
    Keep the form open, do not close it.
    Now open the UserControl designer, and add a label if you want to.
    The UserControl in the form must have been hatched-out, as you said.
    Now close the UserControl's designer.
    The UserControl's instance on the form must be restored non hatched.
    But it loses the last value that was entered but not saved.
    At least HERE it does not keep the last value entered (10). It shows the last value that was saved the last time I closed the form.
    Please pay attention in the steps.

    If you confirm that your VB6 IDE is behaving differently, I don't know what to think...

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,516

    Re: To write UserControl's properties at any given time?

    Ahhh, interesting. Eduardo, you are correct. At least now the problem is clear (to me).

    I was thinking they got completely reset, but that's not the case. They simply get reset "to the last value from when a WriteProperties was executed", which isn't necessarily the control's default value.

    And hence, your question.

    So to summarize, property values are lost if you open the UC's designer window after changing a property but before you either close (and maybe re-open) any form using the UC, or save that form to disk; either of which will execute the WriteProperties.

    And actually, just opening the code window to the UC, and then doing enough to make it hatch any UC on an open form, does the same thing.

    It's an interesting problem. In a sense, you're almost trying to execute code (i.e., the UC's code) while you may be editing that very same code.

    You might be able to do something through an add-in, checking to see when a UC's designer window or code window was being opened, and then close any non-UC designer forms that contained the UC. Argh, I'm not a fan of add-ins, so I'll bow out and let you struggle with that one.

    At least I'm clear on the problem, and it's a good thing to be aware of.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    976

    Re: To write UserControl's properties at any given time?

    Quote Originally Posted by Elroy View Post
    So to summarize, property values are lost if you open the UC's designer window after changing a property but before you either close (and maybe re-open) any form using the UC, or save that form to disk; either of which will execute the WriteProperties.
    Exactly, and hence my question about if there is a way to save the data at any given time. When some property is changed, I would like to save the data at that time and not to wait until the form's designer is closed for it to be done.
    Perhaps sending some Windows message to force the write.

    Quote Originally Posted by Elroy View Post
    You might be able to do something through an add-in, checking to see when a UC's designer window or code window was being opened, and then close any non-UC designer forms that contained the UC. Argh, I'm not a fan of add-ins, so I'll bow out and let you struggle with that one.
    Forget about that. I've never seen an UserControl that comes with an Add-In.

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,516

    Re: To write UserControl's properties at any given time?

    Quote Originally Posted by Eduardo- View Post
    Perhaps sending some Windows message to force the write.
    I suspect all of this is deep in the bowels of the VB6 IDE. However, I didn't work it all out, but here's an idea. Maybe you can "grab" a reference to the UC's property bag. That way, you could use this reference to execute a WriteProperty method anytime you wanted. Something like this:

    Code:
    
    Option Explicit
    Dim pb As PropertyBag
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        Set pb = PropBag
    End Sub
    
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        Set pb = PropBag
    End Sub
    
    Private Sub Just_To_Illustrate()
        pb.WriteProperty "asdf", 123
    End Sub
    
    
    I'm not sure how you're going to "detect" when your UC's designer window (or code window) opens. But, if you can, I believe that'd work to save your properties.

    Best Of Luck,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    976

    Re: To write UserControl's properties at any given time?

    Good try, but I get error 425:

    Code:
    Option Explicit
    
    Dim mPB As PropertyBag
    Dim mlASDF As Long
    
    Public Property Get asdf() As Long
        asdf = mlASDF
    End Property
    
    Public Property Let asdf(l As Long)
        mlASDF = l
        PropertyChanged "asdf"
        mPB.WriteProperty "asdf", mlASDF
    End Property
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        mlASDF = PropBag.ReadProperty("asdf", 0&)
        Set mPB = PropBag
    End Sub
    
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        Debug.Print "WriteProperties"
    
        PropBag.WriteProperty "asdf", mlASDF
    End Sub

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,516

    Re: To write UserControl's properties at any given time?

    Are you sure that you've already "grabbed" the property bag? Maybe...

    Code:
    If Not mPB Is Nothing Then mPB.WriteProperty "asdf", mlASDF
    EDIT1: That's total air-code though. I'm truly spit-balling at this point.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    976

    Re: To write UserControl's properties at any given time?

    Quote Originally Posted by Elroy View Post
    Are you sure that you've already "grabbed" the property bag? Maybe...

    Code:
    If Not mPB Is Nothing Then mPB.WriteProperty "asdf", mlASDF
    EDIT1: That's total air-code though. I'm truly spit-balling at this point.
    Yes, I'm sure.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    976

    Re: To write UserControl's properties at any given time?

    No, there is no way.
    Thinking about how all this issue works.

    PS: is there any way to set a thead "Unresolved forever"?

  13. #13
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,440

    Re: To write UserControl's properties at any given time?

    I can't think of any way around the problem. In IDE design, I think there are 5 events that trigger WriteProperties

    1. Clicking the save button for the entire project
    2. Unloading the project completely
    3. Closing the form from design view
    4. Copying a control, i.e., Ctrl+C & Ctrl+V which triggers a write and read event
    5. Moving from design to runtime
    6. Refreshing the control via right click menu (if control is not hatched)

    It's just a thing usercontrol authors are kinda used to: Close all forms before editing a hosted usercontrol. Especially if you are creating a complex control. Is really a pain when you set a dozen properties for testing and forget to cause a save action before going into the code to tweak a line or two, add a comment, etc.
    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}

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    976

    Re: To write UserControl's properties at any given time?

    Quote Originally Posted by LaVolpe View Post
    I can't think of any way around the problem. In IDE design, I think there are 5 events that trigger WriteProperties

    1. Clicking the save button for the entire project
    2. Unloading the project completely
    3. Closing the form from design view
    4. Copying a control, i.e., Ctrl+C & Ctrl+V which triggers a write and read event
    5. Moving from design to runtime
    6. Refreshing the control via right click menu (if control is not hatched)

    It's just a thing usercontrol authors are kinda used to: Close all forms before editing a hosted usercontrol. Especially if you are creating a complex control. Is really a pain when you set a dozen properties for testing and forget to cause a save action before going into the code to tweak a line or two, add a comment, etc.
    I'm very used to it LaVolpe, but other people aren't.
    I recently posted a control, and the first know user is having a problem regarding this issue.

    It is really better to use it in a compiled OCX, but I know that some people want to have everything in the exe.
    If they are going to use UserControls in exe projects, they will have to know about this problem and learn how to deal with it.

    I was just exploring the possibility of a solution, in case that there was one.
    I didn't think there was one, but better to ask, who knows...

  15. #15
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,440

    Re: To write UserControl's properties at any given time?

    Yepper. If people are gonna mess with your code, they are responsible for the consequences. That's always been my personal opinion. In fact, many times I won't even help troubleshoot after someone modifies my code for their purposes. However, if they are helping me troubleshoot a bug in my code -- that's a different story.

    In any case, it is a matter of education for the user -- not your concern. Again my opinion only.
    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}

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    976

    Re: To write UserControl's properties at any given time?

    Quote Originally Posted by LaVolpe View Post
    Yepper. If people are gonna mess with your code, they are responsible for the consequences. That's always been my personal opinion. In fact, many times I won't even help troubleshoot after someone modifies my code for their purposes. However, if they are helping me troubleshoot a bug in my code -- that's a different story.

    In any case, it is a matter of education for the user -- not your concern. Again my opinion only.
    I don't think there is any bug (related to that issue).
    It simply doesn't save what control is on each tab when it gets hatched-out (it happened to me many times).
    And the control is subclassed in the IDE what make the things worse.
    I'm thinking of putting a note about using it just compiled in an OCX.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width