all groups > dotnet windows forms designtime > june 2004 >
You're in the

dotnet windows forms designtime

group:

Forcing Code Serialization on a Reset<Propname> call



Forcing Code Serialization on a Reset<Propname> call Keith Hill
6/16/2004 3:37:23 PM
dotnet windows forms designtime: I have one class, say AcmeWidget that contains a complex property call =
it
legend defined like so:

public class AcmeWidget : Control {
...
=
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)=
]
public Legend Legend {
get { return m_legend; }
}
}

[TypeConverter(ExpandableObjectConverter)]
public class Legend {
...
public Color Color {
get { ... }
set {
if (value =3D=3D m_color) return;
m_color =3D value;
m_parent.Invalidate;
}
}

private bool ShouldSerializeColor() {
return (m_color !=3D c_defaultColor);
}

private void ResetColor() {
this.Color =3D c_defaultColor;
}
}

This seems to work fine and dandy until I use the "Reset" context menu
item on the nested property "AcmeWidget.Legend.Color" in the =
PropertyGrid. This isn't
enough to trigger code serialization so the previous value stays in the
InitializeComponent method. If I Reset the color and then tweak =
something else=20
that forces a code serialization then I get the proper "reset" color =
serialized out to
InitializeComponent. What do I do to force the designer call to
ResetColor() to cause the internal dirty flag to be set that indicates
that code serialization is required.

After some debugging it appears that the code is doing what I told it. =
I
hit the Reset in the property grid, the ResetColor method gets called.
Now m_color is set equal to c_defaultColor. Then the designer calls my
ShouldSerializeColor method and of course the two are equal and it tells
the designer "oh no, you don't need to serialize this property". That
just seems broken. If I have "Reset" a color that should set a dirty =
flag
somewhere in the designer that says code serialization needs to be done.
I'm still left wondering how to "tickle" the designer to get it to code
serialize on a Reset* method.

Finally, why doesn't this work?

[TypeConverter(ExpandableObjectConverter)]
public class Legend {
...
[DefaultValue(typeof(Color), "Color.Black")]
public Color Color {
get { ... }
set {
if (value =3D=3D m_color) return;
m_color =3D value;
m_parent.Invalidate;
}
}
}

Then I wouldn't need the ShouldSerializeColor or the ResetColor methods.

--
Re: Forcing Code Serialization on a Reset<Propname> call Andrew S (Infragistics)
6/16/2004 6:46:47 PM
You need to either get the IComponentChangeService and notify it of the =
changes (OnComponentChanging/ed) or instead of changing the property =
directly, you need to get the propertydescriptor for that property (see =
TypeDescriptor.GetProperties) and change the property value using that =
method.
"Keith Hill" <r_keith_hill@no.spam.thank.u.hotmail.com> wrote in =
message news:uueido%23UEHA.3336@TK2MSFTNGP11.phx.gbl...
I have one class, say AcmeWidget that contains a complex property call =
it
legend defined like so:

public class AcmeWidget : Control {
...
=
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)=
]
public Legend Legend {
get { return m_legend; }
}
}

[TypeConverter(ExpandableObjectConverter)]
public class Legend {
...
public Color Color {
get { ... }
set {
if (value =3D=3D m_color) return;
m_color =3D value;
m_parent.Invalidate;
}
}

private bool ShouldSerializeColor() {
return (m_color !=3D c_defaultColor);
}

private void ResetColor() {
this.Color =3D c_defaultColor;
}
}

This seems to work fine and dandy until I use the "Reset" context menu
item on the nested property "AcmeWidget.Legend.Color" in the =
PropertyGrid. This isn't
enough to trigger code serialization so the previous value stays in =
the
InitializeComponent method. If I Reset the color and then tweak =
something else=20
that forces a code serialization then I get the proper "reset" color =
serialized out to
InitializeComponent. What do I do to force the designer call to
ResetColor() to cause the internal dirty flag to be set that indicates
that code serialization is required.

After some debugging it appears that the code is doing what I told it. =
I
hit the Reset in the property grid, the ResetColor method gets called.
Now m_color is set equal to c_defaultColor. Then the designer calls =
my
ShouldSerializeColor method and of course the two are equal and it =
tells
the designer "oh no, you don't need to serialize this property". That
just seems broken. If I have "Reset" a color that should set a dirty =
flag
somewhere in the designer that says code serialization needs to be =
done.
I'm still left wondering how to "tickle" the designer to get it to =
code
serialize on a Reset* method.

Finally, why doesn't this work?

[TypeConverter(ExpandableObjectConverter)]
public class Legend {
...
[DefaultValue(typeof(Color), "Color.Black")]
public Color Color {
get { ... }
set {
if (value =3D=3D m_color) return;
m_color =3D value;
m_parent.Invalidate;
}
}
}

Then I wouldn't need the ShouldSerializeColor or the ResetColor =
methods.

--
Re: Forcing Code Serialization on a Reset<Propname> call Eric Cadwell
6/17/2004 7:40:44 AM
Read the section on "Playing nicely with others"

http://msdn.microsoft.com/library/default.asp?url=3D/library/en-us/dndotn=
et/html/custdsgnrdotnet.asp

HTH;
Eric Cadwell
AddThis Social Bookmark Button