Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Help with serialization of property
 

Help with serialization of property

I have a little problem working with the ShouldSerializeMyProperty 
methods to tell the code serializer of the designer when to serialize 
my property. I need some complex conditions so I cannot use the 
DefaultValueAttribute. 

I tried the following code: 


public class MyForm : Form 

    public MyForm() 
    { 
        this.extraColor = Color.Red; 
        this.extraValue = 0; 
        this.layerData = new LayerData(); 
    } 

    public Color ExtraColor 
    { 
        get { return this.extraColor; } 
        set { this.extraColor = value; } 
    } 
    bool ShouldSerializeExtraColor() 
    { 
        Color color; 
        if (this.extraValue == 0) color = Color.Red; 
        else color = Color.Blue; 
        return !(this.extraColor == color); 
    } 

    public int ExtraValue 
    { 
        get { return this.extraValue; } 
        set { this.extraValue = value; } 
    } 
    bool ShouldSerializeExtraValue() 
    { 
        return !(this.extraValue == 0); 
    } 

    private int extraValue; 
    private Color extraColor; 



Then I inherited MyForm in the following way: 

public class Form1 : MyForm { ... } 

When I opened Form1 in the designer and changed ExtraValue to 1 (from 
0) then it gets serialized, but ExtraColor doesn't (it should 
serialize with a value of Red). Then I changed ExtraColor to 
Blue and it gets serialized (though it shouldn't). It looks like the 
ShouldSerializeMyProperty is not executed dynamically, but hardcoded 
or something. 
The most interesting of all is that I tried doing the same thing with 
a Panel (inheriting Panel to create MyPanel, and then MyPanel to 
create Panel1) and it worked as it should when I place a Panel1 on a 
Form and change the properties as explained before. 

I also tried to attach another Visual Studio as a debugger to the
serialization process. I put a breakpoint inside the
ShouldSerializeExtraColor method, and when I tried it with the Panel
version it gets called and returned the correct boolean value. When I
tried it with the Form (as in the previous code) I found out that the
ShouldSerializeExtraColor doesn't even get called !?!?!?!

I really would appreciate help with this problem. Please note that the
previous code is just an example of what I need to do.

Uriel
MigrationUser 1  Wednesday, March 19, 2003 1:18 AM
This appears to be a bug in Visual Studio.NET 1.0.  I'm not sure if there is a work around, but this is addressed in the upcoming 1.1 release.   I tried it there and it works correctly.
MigrationUser 1  Tuesday, March 25, 2003 6:18 PM

You can use google to search for other answers

Custom Search

More Threads

• Implement the ComboBox of the Properties Windows when hosting forms in run-time.
• Windows Form Designer not populating event drop-down menu
• forms designer error, vb .net 2005
• Exception of type ExceptionCollection thrown when a form is attempted open from VS2005's IDE
• BindingList with Images?
• VS2005 Beta 2 form designer problem: error while parsing EntityName
• this.Refresh(); problem in C#
• DesignSurface design question
• Binding data from a database to a DGV combo box.
• whidbey winforms: MDI: why does childForm menuStrip appear in parentForm menuStrip?