When a host project changes a property of my custom control at design time, the change isn't honored at run time. Why?!

Robert Gustafson 606 Reputation points
2022-08-01T05:26:00.907+00:00

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

MY PROBLEMS:
I've created an extended custom control that uses a property page (using my own property-page form and editor classes) to allow a host project to modify a property of custom type (defined by my own class/structure) and/or group type (based on an array, list, collection, dictionary, etc.) at design time. The changes made by a host project remain intact when the project is exited and reloaded, provided the design-time value of the property in question isn't tied to the design-time value of another. However, the design-time changes are not reflected in the control when it's loaded at run time! Instead, the initial value is whatever defaults I've initialized it to in the declarations section of the control's class. How can I pre-define initial values for a property (when an instance of the control is first dropped on a host project's form), but have any host-project design-time changes endure not only on project reload, but also on project run? (BTW, I'm not using a pre-defined property editor like CollectionEditor. I'm defining my own classes for property-page form and editor.)

Basically, if my control defines a property to have an initial value of Value1, and a host project modifies the property to Value2 using my property page, Value2 is henceforth the design-time value ever after in any given session of the host project; however, whenever the project runs, the property reverts to Value1 until changed at run-time. (BTW, if the property is not of a custom/group type, and does not have a custom property page, this problem doesn't exist. The run-time value then starts as Value2.)

Please give any answer in VB.NET, and keep it as simple as possible.

Example:

Public Class MyType 'could be a structure instead  
   Public Property SubProperty1 As Type1  
   Public Property SubProperty2 As Type2  
   Public Property SubProperty3 As Type3  
   '   other sub-properties may follow  
  
   '   optional method:  
   Public Sub ToString() As String  
      '   some code  
  
      Return StringToDisplay  
   End Sub  
End Class  
  
Public Class MyControl  
   Inherits OtherControl 'could by UserControl or an existing control  
  
   Private MyTypeVar As MyType = original-value  
   Private MyGroupType As MyGroupType = original-group-value 'MyGroupType is related to an array, list, collection, dictionary or other serializable type for a bunch of items either of a standard type or of MyType  
  
  
   <Editor(MyTypeEditor, GetType(UITypeEditor))> _  
   Property MyProperty() As MyType  
      Get  
         Return MyTypeVar  
      End Get  
      Set (value As MyType)  
         MyTypeVar = value  
      End Set  
   End Property  
  
   <Editor(MyGroupTypeEditor), GetType(UITypeEditor))> _  
   Public MyGroupProperty() As MyGroupType  
      Get  
         Return MyGroupTypeVar  
      End Get  
      Set (value As MyGroupType)  
         MyGroupTypeVar = value  
      End Set  
   End Property  
  
   '   other properties/methods/events may follow  
  
End Class  

How can I ensure that the initial run-time values of MyProperty and MyGroupProperty in a host-project control instance aren't always original-value and original-group-value, respectively, if these properties are changed in the host project at design time?

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-01T10:00:52.21+00:00

    The property value needs to be cloned/copied in the property-page form/editor and in the Property procedure setter. Then the modification is honored at run time.

    0 comments No comments