Hi all,
To repro the problem I am having:
1. Create a class that derives from System.Windows. Forms.Form
2. Add some controls such as buttons and labels (wherever you want). The default values must be set appropriately.
3. Add some public properties to the form that directly adjust the exposed private Control properties:
public class BaseForm : Form
{
[Category("Appea rance"), DefaultValue(tr ue)]
public bool ButtonVisible { get { return btn.Visible; } set { btn.Visible = value; } }
[Category("Appea rance"), DefaultValue(tr ue)]
public bool LabelVisible { get { return lbl.Visible; } set { lbl.Visible = value; } }
// initialized by the designer:
private Button btn;
private Label lbl;
// I removed some stuff for clarity
}
4. Create a class that derives from the newly created class above
(I used VS.NET 2003 and selected "Add New Item --> Inherited Form")
public class DerivedForm : BaseForm
{
// I removed some stuff for clarity
private System.Componen tModel.IContain er components = null;
#region Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
components = new System.Componen tModel.Containe r();
}
#endregion
}
5. In the designer, set focus to the "DerivedFor m" and try to set one of the properties such as ButtonVisible to "false"
6. Look in the InitializeCompo nent method. This is what mine says:
{
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 269);
this.LabelVisib le = true;
this.Name = "DerivedFor m";
}
Ok, so the default value of ButtonVisible is "true". When I set it to false, the designer serialized LabelVisible = true along with
some other property values. But, it didn't serialize ButtonVisible at all.
At this point, the designer reflects the correct change, although the code-behind doesn't. Closing and reloading the class in the
designer retains those changes I made, yet the code-behind is still incorrect so the Button remains visible at runtime, yet
invisible in the designer.
Any changes to the base class reset all changes that I made to the designer.
The only work-around I found is to not use the designer and set those property values in the constructor, after the call to
InitializeCompo nent.
Just FYI, here are the things I've tried that didn't have any effect.
I wrote the following for all properties that I declared:
[Category("Appea rance")] // no default value specified
public bool ButtonVisible { get { return btn.Visible; } set { btn.Visible = value; } }
private bool ShouldSerialize ButtonVisible()
{
return !btn.Visible;
}
private void ResetButtonVisi ble()
{
btn.Visible = true;
}
public event EventHandler ButtonVisibleCh anged
{
add { btn.VisibleChan ged+= value; }
remove { btn.VisibleChan ged-= value; }
}
Also tried the following, and none had any effect:
I tried switching around the return values by removing the negation (!).
I tried adding DefaultValue(fa lse) just to see what would happen.
I also tried removing all "default" serialization code and Attributes.
I tried adding NotifyParentPro perty(true) and (false).
I tried a few combinations of all of the above.
Why would this happen and how can I fix it?
I'd like to avoid making a custom Designer if possible. It seems like DefaultValueAtt ribute should have done the trick.
--
Dave Sexton
dave@www..jwaon line..com
-----------------------------------------------------------------------