all groups > dotnet windows forms designtime > april 2006 >
You're in the

dotnet windows forms designtime

group:

serialization in InitializeComponents


serialization in InitializeComponents Ivan
4/12/2006 12:28:01 AM
dotnet windows forms designtime:
My custom composite control for the .NET Compact Framework will not serialize
the DesignTime DefaultValues I assign to properties. For instance, VS
serializes '0' for ints and it serializes the first value for any enumeration
types.

For example:

// I implemented this default value via the “Custom Attributes
// Form” or through DesignTimeAttributes.xmta rather.
[DefaultValue(2)]
public int ButtonCount
{
get{ return count;}
set{ count = value;}
}

Considering the code snippet above you’d think the designer would serialize
the following…

Obj.ButtonCount = 2; // I would assume this serialization because
DefaultValue(2)

But instead I get…

Obj.ButtonCount = 0;

I can change the value of the property and the code reflects the change
correctly, but the designer doesn’t initially serialize the default value to
the property in InitializeComponents. Consequently, when I change the
property value manually via the property grid to the default value, the
designer recognizes this and no longer serializes the property setting in
InitializeComponents.

Also, if I have:

Public enum PanelType
{
NO_PANEL,
BOTTOM_PANEL,
TOP_PANEL
};

[DefaultValue(BOTTOM_PANEL)]
Public PanelType FormPanelType
{
get{ return pType; }
set{ pType = value;}
}

Here the same sort of issue arises … the designer only serializes NO_PANEL
initially, and disregards the DefaultValue. Consequently, when the default
value is selected manually the default value isn’t recognized by the designer
and the default value setting is still serialized in InitializeComponents.
This differs from the ‘int’ property above…

I’ve looked at many examples, and I am still unable to find the exact
configuration settings for this issue.

Any thoughts?

thanks
Re: serialization in InitializeComponents Tim Wilson
4/12/2006 10:56:45 AM
The DefaultValue attribute is simply metadata that is used at design-time to
decide if the value of a property needs to be serialized. In other words, if
the value has not changed from the default then the value should not be
serialized. You'll need to make sure that the actual value of the field
storing the property value is the same as the value specified in the
DefaultValue attribute.

private int count = 2;

[DefaultValue(2)]
public int ButtonCount
{
get{ return count;}
set{ count = value;}
}

--
Tim Wilson
..NET Compact Framework MVP

[quoted text, click to view]

AddThis Social Bookmark Button