By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,534 Members | 865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,534 IT Pros & Developers. It's quick & easy.

Serializing booleans with ShoudSerialize or DefaultValue

P: n/a
Good morning,
sorry for the lenght of the code, but it's six months that I've this
The following code is an extreme simplification of the original code, that
is much more complex. The problem, however, occours in the following code at
all. It's easy copy the following code for reproducing the problem.

This is a class that simply draws (or not draws) a blue rectangle inside a
given Rectangle:
Public Class MyBackground
Private mVisible As Boolean = True
Public Event Paintbackground()

<Browsable(True)> _
Public Property Visible() As Boolean '
Return mVisible
End Get
Set(ByVal Value As Boolean)
mVisible = Value
RaiseEvent Paintbackground()
End Set
End Property

Friend Sub Draw(ByVal aGraphicSurface As Graphics, ByVal
aContainerRectangle As Rectangle)
If mVisible Then
aGraphicSurface.FillRectangle(Brushes.Blue, aContainerRectangle)
End If
End Sub

Public Sub ResetVisible()
Visible = True
End Sub
Public Function ShouldSerializeVisible() As Boolean
Return (mVisible = False)
End Function

End Class


And this is a component (a very simple panel) that simply make use the above
class as a property:
Public Class MyPanel
Inherits System.Windows.Forms.Panel

'Former " Component designer generated code "
Public Sub New()
SetStyle(ControlStyles.SupportsTransparentBackColo r, True)
Me.BackColor = Color.Transparent
End Sub

Private Sub InitializeComponent()
mLocalBackground.Visible = False
End Sub

Private WithEvents mLocalBackground As New MyBackground
<Browsable(True), _
DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content),
TypeConverter(GetType(ExpandableObjectConverter))> _
Public Property PaintBackground() As MyBackground
Return mLocalBackground
End Get
Set(ByVal Value As MyBackground)
mLocalBackground = Value
End Set
End Property

Protected Overrides Sub OnPaint(ByVal e As
mLocalBackground.Draw(e.Graphics, Me.ClientRectangle)
End Sub

Private Sub mLocalBackground_Paintbackground() Handles
End Sub
End Class
Now, drag MyPanel on a form and set PaintBackground.Visible = True.
The problem is that the background (MyBackground) disappears immediately
after regenerating the project, and the value is NOT serialized.
I've tried to delete ResetVisible and ShouldSerializeVisible, using
DefaultValue(True), but the result is the same: not serialized.

Please note that deleting the row
mLocalBackground.Visible = False
inside MyPanel class, all work without errors, and the Visible value is
correctly serialized.

Since MyBackground class is used in my components collection by several
components, and some has the visibility default set to True and others has
the default set to False, I need to set the visibility of MyBackground from
within the contructor of the component tha makes use of MyBackground class.
However, It's important to say that I've exprimented the same problem wit
all Boolean properties. Serialization for any other type works fine and
serialization is ok.

A drastic workaround should be deleting all Boolean defaults (i.e.: no
DefaultValue, no Resetxxx and no ShouldSerializexxx). This forces the
designer to serialize any value of the boolean properties. Obviously, I need
avoid this way of programming.

Sorry again for the lenght of this post, and please help me.


Carlo, MCP (Windows Based Applications)
Milan, Italy

Nov 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.