none
How do I get my UserControl's custom-property values to persist at run-time? RRS feed

  • Question

  • WHAT I HAVE:

    Visual Basic 2015, Win Forms

    MY PROBLEM:

    I created a UserControl and built it into a DLL. I then created a (scratch) WinForms project, added the UserControl to the toolbox, and then placed it onto a form. The trouble is, when I at design-time set the custom properties I created for it, the changes aren't persisted when I run the project; they revert to the default values! This problem does not exist when I set pre-existing built-in properties that are along for the ride with any UserControl--just the custom properties. What am I not doing right? 


    Robert Gustafson

    PS. Is the above problem an issue with extended (inherited) ustom controls as well?

    PPS. On an unrelated custom-control issue (this is a separate question), is there a way for a control to tell when it's design time or run time? So a  property can be, say, read-write a design time but read-only a run time?

    Friday, January 5, 2018 1:01 PM

Answers

  • Starting at the end because its an easy answer, yes, there is a DesignMode property on Component that you can check to see if the object is in design time or run time.

    The first question requires some follow-up...

    What is/are the property type(s) that you are having trouble with?  If they are properties with intrinsic types such as String or Integer then they should persist as expected.  If they are custom types then they must be marked as Serializable (and support serialization) in order for them to persist from design time to run time.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Friday, January 5, 2018 1:49 PM
    Moderator
  • Hello,

    Try the following (which is not a user control) where I declarate a property using 

    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)

    This should stop a property from resetting (de)serialization in tangent with a private backing field. Note I also set a default value for the property. Once placed on a form ShowRemoveButton is set to true, in the property window I set that property to false. Ask the value of ShowRemoveButton at runtime and it's false.

    Note the control really has no true purpose other than showing persistence of a property

    Imports System.ComponentModel
    
    Public Class MyButton
        Inherits Button
    
        <DefaultValue(True),
            DesignerSerializationVisibility(
            DesignerSerializationVisibility.Visible),
            Description("Do nothing property")>
        Public Property ShowRemoveButton() As Boolean
            Get
                Return _showRemoveButton
            End Get
            Set(ByVal value As Boolean)
                _showRemoveButton = value
            End Set
        End Property
        Private _showRemoveButton As Boolean = True
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, January 5, 2018 2:47 PM
    Moderator

All replies

  •  Did your last question (link below) get answered,  if it did then remember to go back and mark the post(s) that answered it as the answers.

     How do I determine the (horizontal) size of the "selection margin" of a rich-text box?

     

     For your "unrelated" question about determining between Design-time and Run-time,  you can use the DesignMode property such as...

            If Me.DesignMode Then
                'do something...
            End If

     

     As far as your question about the properties you added to the UserControl class being reset to the default at run-time,  i would say you might be doing something or not doing something in the UserControl class code that is causing them to be reset.

     If this is about the TextRuler control,  i have built it as a dll class library,  added it to the toolbox,  and added one to a form.  When i change the BorderColor and BaseColor custom properties,  they seem to persist when i run the application.

     PS - I just noticed in the code from that TextRuler class that the call to the InitializeComponent sub is not being called in the constructor.  I just went back and updated that in my post (link below).  You should fix that so it adjusts the size of the TextRuler correctly when one is added to a form from the toolbox.

    Creating (or finding) a Ruler Control that supports marking margins, indents, and tabs


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Friday, January 5, 2018 1:48 PM
    Friday, January 5, 2018 1:47 PM
  • Starting at the end because its an easy answer, yes, there is a DesignMode property on Component that you can check to see if the object is in design time or run time.

    The first question requires some follow-up...

    What is/are the property type(s) that you are having trouble with?  If they are properties with intrinsic types such as String or Integer then they should persist as expected.  If they are custom types then they must be marked as Serializable (and support serialization) in order for them to persist from design time to run time.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Friday, January 5, 2018 1:49 PM
    Moderator
  • Off Topic...

     Hi Reed,  good to see you around again.   8)


    If you say it can`t be done then i`ll try it

    Friday, January 5, 2018 1:51 PM
  • There are many ways to make, and use a usercontrol. 

    If I do it using the designer

    I get from this code 

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MessageBox.Show(UserControl11.ExtraTag)
        End Sub
    End Class

    This result. 

    Therefore tell what you did otherwise we get a Helpdesk kind of thread where everybody is guessing. The first question is than: "Did you plugin your power connector?


    Success Cor

    Friday, January 5, 2018 1:52 PM
  • Off Topic...

     Hi Reed,  good to see you around again.   8)


    If you say it can`t be done then i`ll try it

    Hiya Razerz, its good to be seen :D

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, January 5, 2018 1:55 PM
    Moderator
  • Hello,

    Try the following (which is not a user control) where I declarate a property using 

    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)

    This should stop a property from resetting (de)serialization in tangent with a private backing field. Note I also set a default value for the property. Once placed on a form ShowRemoveButton is set to true, in the property window I set that property to false. Ask the value of ShowRemoveButton at runtime and it's false.

    Note the control really has no true purpose other than showing persistence of a property

    Imports System.ComponentModel
    
    Public Class MyButton
        Inherits Button
    
        <DefaultValue(True),
            DesignerSerializationVisibility(
            DesignerSerializationVisibility.Visible),
            Description("Do nothing property")>
        Public Property ShowRemoveButton() As Boolean
            Get
                Return _showRemoveButton
            End Get
            Set(ByVal value As Boolean)
                _showRemoveButton = value
            End Set
        End Property
        Private _showRemoveButton As Boolean = True
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, January 5, 2018 2:47 PM
    Moderator