Saving usercontrol design-time properties
-
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.
-
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.
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 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.
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 isVisible
, and should be persisted in initialization code, Hidden, and should not be persisted in initialization code, or consists ofContent
, 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
. TheString
type doesn't have any public properties which can be set, so no initialization code will be generated. Either set it toDesignerSerializationVisibility.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
-
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 isVisible
, and should be persisted in initialization code, Hidden, and should not be persisted in initialization code, or consists ofContent
, 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
. TheString
type doesn't have any public properties which can be set, so no initialization code will be generated. Either set it toDesignerSerializationVisibility.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
thank you. It is working now.