Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Saving usercontrol design-time properties

Saving usercontrol design-time properties

Scheduled Pinned Locked Moved C#
questiondesignalgorithmstutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Crazy Joe Devola
    wrote on last edited by
    #1

    I swear I have been searching for an answer for hours but could not find any understandable example or article :-( How do I make a property of a Winform usercontrol retain it's design time value? I added a new UserControl object. I put a label on the control. Added a property called "KeyText". I saw in few articles the attribute "DesignerSerailizationVisibility" mentioned so I added it as well.

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public string KeyText
    {
    get { return label1.Text; }
    set
    {
    label1.Text = value;
    }
    }

    In the control, the default value of label1.Text is "X". Then I added the control to a form. The property "KeyText" showed as one of the properties of the control. Great. So I changed it to "A". The design-time form showed A". Great. I compiled and ran the program - the control on the form showed "X" instead of "A". What do I need to change to make the property persisteable? In VB6 I use something called PropBag to save and read design time properties.

    OriginalGriffO Richard DeemingR 2 Replies Last reply
    0
    • C Crazy Joe Devola

      I swear I have been searching for an answer for hours but could not find any understandable example or article :-( How do I make a property of a Winform usercontrol retain it's design time value? I added a new UserControl object. I put a label on the control. Added a property called "KeyText". I saw in few articles the attribute "DesignerSerailizationVisibility" mentioned so I added it as well.

      [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
      public string KeyText
      {
      get { return label1.Text; }
      set
      {
      label1.Text = value;
      }
      }

      In the control, the default value of label1.Text is "X". Then I added the control to a form. The property "KeyText" showed as one of the properties of the control. Great. So I changed it to "A". The design-time form showed A". Great. I compiled and ran the program - the control on the form showed "X" instead of "A". What do I need to change to make the property persisteable? In VB6 I use something called PropBag to save and read design time properties.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      OK, I added a new UserControl (called MyControl) to my project, dropped a label on it called MyLabel. I changed it's Text property to "Default". I added a string property called LabelText to the control:

          public string LabelText
              {
              get
                  {
                  return MyLabel.Text;
                  }
              set 
                  {
                  MyLabel.Text = value;
                  }
              }
      

      And compiled the app. Then I dropped two instances of MyControl (MC1 and MC2) onto my form. I changed the LabelText property of MC2 to "Revised" in the designer and ran the app. MC1 had "Default", MC2 had "Revised" So what did I do that you didn't?

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • C Crazy Joe Devola

        I swear I have been searching for an answer for hours but could not find any understandable example or article :-( How do I make a property of a Winform usercontrol retain it's design time value? I added a new UserControl object. I put a label on the control. Added a property called "KeyText". I saw in few articles the attribute "DesignerSerailizationVisibility" mentioned so I added it as well.

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public string KeyText
        {
        get { return label1.Text; }
        set
        {
        label1.Text = value;
        }
        }

        In the control, the default value of label1.Text is "X". Then I added the control to a form. The property "KeyText" showed as one of the properties of the control. Great. So I changed it to "A". The design-time form showed A". Great. I compiled and ran the program - the control on the form showed "X" instead of "A". What do I need to change to make the property persisteable? In VB6 I use something called PropBag to save and read design time properties.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Crazy Joe Devola wrote:

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

        That line is the problem. The documentation isn't particularly clear, but:

        DesignerSerializationVisibilityAttribute Class (System.ComponentModel) | Microsoft Docs[^]

        With the DesignerSerializationVisibilityAttribute, you can indicate whether the value for a property is Visible, and should be persisted in initialization code, Hidden, and should not be persisted in initialization code, or consists of Content, which should have initialization code generated for each public, not hidden property of the object assigned to the property.

        The property value is a String. The String type doesn't have any public properties which can be set, so no initialization code will be generated. Either set it to DesignerSerializationVisibility.Visible, or remove the attribute.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        C 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Crazy Joe Devola wrote:

          [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

          That line is the problem. The documentation isn't particularly clear, but:

          DesignerSerializationVisibilityAttribute Class (System.ComponentModel) | Microsoft Docs[^]

          With the DesignerSerializationVisibilityAttribute, you can indicate whether the value for a property is Visible, and should be persisted in initialization code, Hidden, and should not be persisted in initialization code, or consists of Content, which should have initialization code generated for each public, not hidden property of the object assigned to the property.

          The property value is a String. The String type doesn't have any public properties which can be set, so no initialization code will be generated. Either set it to DesignerSerializationVisibility.Visible, or remove the attribute.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          C Offline
          C Offline
          Crazy Joe Devola
          wrote on last edited by
          #4

          thank you. It is working now.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups