When I set up a property of custom/collection type of a custom control to be editable at design-time via Property Pages, the host project's design-time changes to the property are lost on project's run. WHY?!

Robert Gustafson 606 Reputation points
2022-08-01T03:19:43.827+00:00

WHAT I HAVE:
Visual Basic 2019, .NET Framework 4.0+/.NET Core, WinForms

MY PROBLEM:
I've been trying my hand at defining properties of custom (class with sub-properties) and/or aggregate (array, list, dictionary, etc.) type for my custom controls and defining my own property page form and supporting editor classes to allow a programmer who drops an instance of the control on a host project to modify the property at design-time. The value of the property (or its contents) is to be set to custom defaults when a control instance is first dropped onto a control, and any changes the programmer makes at design time are supposed to endure when the project is run or reloaded. The problem is, every time the project/form is run, the property reverts to my initial defaults instead of the programmer's host-program specified values when modified at design-time! When I reload the host project, the custom values for the property set at design-time are retained, but not reflected upon run.

I initialize the variable that the property references in the declaration section or the Sub New procedure of my control's class, and specify, before the Property procedure that references the variable, the attributes for my custom property editor. I'm doing everything that previous respondents to my previous posts have recommended, and even tried changing the type for the property and for its underlying variable, to no avail.

On the other hand, design-time changes to non-custom, non-aggregate property types which don't have property pages set up for them properly endure on project run/reload. What am I missing?

Please give any advice and/or code in VB.NET and as simply as possible. (Mind you, I'm not using a pre-existing property editor like CollectionEditor; I'm defining my own form and editor for my own property page.)

Example:

<Serializable()> _  
Public Class MyType  
   Public Property SubProperty1() As Type1  
   Public Property SubProperty2() As Type2  
   Public Property SubProperty3() AS Type3  
   '   other sub-properties follow  
  
  
   '   This method may or may not follow  
   Public Sub ToString() As String  
   '    code follows  
  
   Return DisplayStringForItem  
   End Sub  
End Class  
  
  
  
Public Class MyControl 'inherits from existing control or UserControl  
   '   property variables MyTypeVar and MyTypeGroupVar are initialized in either declaration section or Sub New procedure of MyControl  
  
  
   <Editor(MyEditor, GetType(UITypeEditor))> _  
   Public Property MyProperty() As MyType  
   Get  
      Return MyTypeVar  
   End Get  
   Set (value As MyType)  
      MyTypeVar = value  
   End Set  
   End Property  
  
   <Editor(MyGroupEditor, GetType(UITypeEditor))> _  
   Public Property MyGroupProperty() As MyGroupType  
   '   MyGroupType is (or ultimately derives from) an array, collection, list, dictionary, etc.--  
   '   or is designed with equivalent functionality--with constituent elements effectively of MyType  
   Get  
      Return MyTypeGroupVar  
   End Get  
   Set (value As MyGroupType)  
      MyGroupTypeVar = value  
   End Set  
   End Property  
  
   '   other properties, methods, events  
  
End Class  

Now I built the control project to create the DLL.

Then I swap out the control project and create/load a host project, add the control and its DLL to References/Toolbox, add/display a form, and finally place an instance of MyControl on it. When I use the Properties window to invoke the property page for 1 of the above properties, the property will have the initial values the control wishes it to have (good). Then I modify the property's sub-properties and/or elements using the page. Each next time I invoke the page again, it shows the new values (good).

However, when I run the program, the starting run-time values for the property are the initial defaults, not the new values specified at design-time (bad). When I re-enter design mode and invoke the property page again, the values are again the changes I last specified in design mode (good). If I swap out and then reload the project, the design-time property values the changes I specified at run time (good), but on run they revert to the initial defaults (bad)!

What am I doing wrong?!

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,821 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,117 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Robert Gustafson 606 Reputation points
    2022-08-02T05:58:02.457+00:00

    The trick is to make a copy of the property, both when invoking a PP form or dialog in the editor class, and when setting in the Property procedure.

    0 comments No comments