Winforms designer: exclude property

A

Armin Zingler

Hi,

as I don't get an answer in vstudio.development within weeks, I try it here:

I'm referring to Designer Serialization:
http://msdn.microsoft.com/en-us/library/ms171834.aspx

I derived a class from DataGridView. In the class' ctor, I add two columns. After compilation
and putting an instance of my control from the toolbox onto a form using the designer, the
designer creates the code that adds the columns at runtime (mydgv.columns.add inside
InitializeComponent).

The problem is: If I run the application, the control now has 4 columns because the columns
are added in my ctor and additionally by the designer generated code.

So my question is: How can I tell the designer to exclude the Columns property from
serialization? In other cases, I do know how to handle this, either by attaching the
DefaultValue attribute or the DesignerSerializationVisibility attribute.

First, the DefaultValue attribute is not appropriate for this type of properties.

Second, to be able to attach the DesignerSerializationVisibility to the Columns property,
I had to shadow the base class property:

<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Shadows ReadOnly Property Columns() As DataGridViewColumnCollection
Get
Return MyBase.Columns
End Get
End Property

Still this does not prevent the designer from serializing the property.

Did I miss something? How to handle this case?
 
P

Phill W.

Armin,
The problem is: If I run the application, the control now has 4 columns because the columns
are added in my ctor and additionally by the designer generated code.

You could try making the code in your constructor conditional, using the
DesignMode property.

If Not Me.DesignMode Then
' => Run Time

' Add Columns

End If

However, this used to have some "issues" when working with inherited
Controls on inherited Forms - which is running? Which is being
designed? Anyway, these were the three favoured work-arounds:
http://geekswithblogs.net/mrnat/archive/2005/04/27/38594.aspx
Second, to be able to attach the DesignerSerializationVisibility to the Columns property,
I had to shadow the base class property:

Yes; it's a nuisance, isn't it?
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Shadows ReadOnly Property Columns() As DataGridViewColumnCollection
Get
Return MyBase.Columns
End Get
End Property

Still this does not prevent the designer from serializing the property.

This used to work in VB'2003:

Public Function *ShouldSerialize*Columns() As Boolean

Return False

End Function

Hope this Helps, (and if it doesn't, I'm in trouble come upgrade time!)
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top