Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / June 2004

Tip: Looking for answers? Try searching our database.

Forcing Code Serialization on a Reset<Propname> call

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Keith Hill - 16 Jun 2004 22:37 GMT
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 == m_color) return;
     m_color = value;
     m_parent.Invalidate;
   }
 }

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

 private void ResetColor() {
   this.Color = 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
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 == m_color) return;
     m_color = value;
     m_parent.Invalidate;
   }
 }
}

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

--
Keith
Andrew S \(Infragistics\) - 16 Jun 2004 23:46 GMT
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.
 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 == m_color) return;
       m_color = value;
       m_parent.Invalidate;
     }
   }

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

   private void ResetColor() {
     this.Color = 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
 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 == m_color) return;
       m_color = value;
       m_parent.Invalidate;
     }
   }
 }

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

 --
 Keith
Eric Cadwell - 17 Jun 2004 15:40 GMT
Read the section on "Playing nicely with others"

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/c
ustdsgnrdotnet.asp


HTH;
Eric Cadwell
http://www.origincontrols.com

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.