Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I am inheriting one WinForms Form from another. I have a property in the base form:
C#
[Browsable(true)]
[Category("Appearance")]
[DefaultValue(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool ShowMeLabelVisible
{
    get { return ShowMeLabel.Visible; }
    set { ShowMeLabel.Visible = value; }
}

public bool ShouldSerializeShowMeLabelVisible ()
{
    return true;
}

The generated InitializeComponent() method in the inheriting form sets the property value only when it is true, and does not set the property value when it is false, regardless of the default value specified by the DefaultValueAttribute. This is despite the ShouldSerializeShowMeLabelVisible method indicating the property value should always be serialized. Further, when the form is first shown in the Visual Studio designer, or immediately after a rebuild (including a debug run), Visual Studio shows the property value as true in the Properties window, and the designer acts as if the property were set to true; even if the DefaultValueAttribute is constructed with false, the underlying ShowMeLabel.Visible property is false, and (due to the absence of initialization in IntializeComponent()) the run time value is false.

Can someone give me a hint as to what I’m doing wrong? I would like the property value at design time and at run time to agree, and the designer to look like the run time form.

The environment is VS 2010 version 10.0.40219.1 SP1Rel on Win 7 Pro 6.1.7601 SP1.

Thank you.
Posted
Updated 27-Mar-13 9:15am
v2
Comments
Sergey Alexandrovich Kryukov 27-Mar-13 15:05pm    
To start with, you don't show where ShouldSerializeShowMeLabelVisible is used...
—SA
Brian Hetrick 27-Mar-13 15:23pm    
ShouldSerialize[PropertyNameHere] is supposed to be called by the designer using reflection; see e.g. Defining Default Values with the ShouldSerialize and Reset Methods.
Sergey Alexandrovich Kryukov 27-Mar-13 15:28pm    
No, it is not. This is just your own method, without any attribute. Maybe, that's why you have this problem.
—SA
Brian Hetrick 27-Mar-13 15:32pm    
Perhaps you could post a short snippet of how the ShouldSerialize and Reset methods should be used, then. I would greatly appreciate it.
Sergey Alexandrovich Kryukov 27-Mar-13 15:44pm    
You know what? I found that even the MSDN sample you referenced above should not work. Worse, if it actually works :-(. It means that Microsoft uses a dirty trick: the method is found by the dirty naming conventions, when the library codes looks at all methods using Reflection and choose the one with then name matching some pattern. No one has any right to do such tricks, even Microsoft. Unfortunately, I already caught Microsoft on such dirty trick, but this one looks the very worst. Fortunately, all my findings like that are related to the ComponentModel. It is extremely lousy and must die. So, please forgive me: I'll never go into this, and would not advise you do go there.

For persistence, I only use either Microsoft Data Contract, which is very good, or my own very similar persistence system, for some extra flexibility. I hope you can also find a work around...

—SA

This turns out to be a known bug in the VS WinForms designer; see Problem with inherits form. It affects only properties which are directly backed by the Visible property of components. The solution is to use a variable to hold the intended state of the visibility attribute, manipulate it instead, and set the appropriate control’s Visible property when needed:
C#
private bool _pt1Visible = true;

[Browsable(true), Category("Appearance"), DefaultValue(true)]
public bool Pt1Visible
{
    get { return _pt1Visible; }
    set
    {
        _pt1Visible = value;
        PotentialLabel1.Visible = _pt1Visible;
    }
}

Thank you BonnieB!
 
Share this answer
 
Comments
phil.o 28-Mar-13 12:31pm    
5'd!
Thanks for sharing your solution :) Maybe it will be useful to someone else at a later time.
From the link you gave :

Note

Either apply the DefaultValueAttribute or provide ResetPropertyName and ShouldSerializePropertyName methods. Do not use both.


As Sergey stated, do not rely on this dirty trick with method names ; better use the attribute in the first place, and see how it is behaving.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Mar-13 16:39pm    
Agree! My 5.
—SA
Brian Hetrick 27-Mar-13 17:13pm    
Unfortunately, as I noted, neither the attribute nor the documented "dirty trick" with the methods named after the property name actually work. I'll try again in another question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900