Persisting a custom control’s contained components at design time

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi
I have created a custom DataGridView (named MyDataGridView) by extending the DataGridView control.
I have given MyDataGridView a custom design-time behavior by writing the following classes :
1)<span> A Designer class named MyDataGridViewDsgnr, by extending the
<span style="color:black http://msdn.microsoft.com/en-us/library/system.windows.forms.design.controldesigner ControlDesigner class, and specifying it as
MyDataGridView’s Designer through the <span style="color:black DesignerAttribute attribute.
<span style="color:black 2)<span> A Smart Tag panel named
MyDataGridViewSmartTagPnl<span style="color:black , by extending the DesignerActionList class.
3)<span> A Serializer named MyDataGridViewSrlzr, by extending the CodeDomDerializer class, and
<span style="color:black specifying it as MyDataGridView’s Serializer through the
<span style="color:black DesignerSerializerAttribute attribute.
Using these classes, I have been able to successfully populate MyDataGridView at design-time with DataGridViewColumns.
However, I am unable to persist these columns. Therefore, when a new form session is started (either design-time or run-time), MyDataGridView is not populated with the columns that had been specified in the earlier design-time session.

Persisting these columns, requires design-time emission of code similar to the following VB code, to the ‘form.Designer.vb file’ :
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Me.MyDataGridView1.Columns.AddRange(
<span style="font-size:9.5pt; color:blue; font-family:Consolas New<span style="font-size:9.5pt; font-family:Consolas System.Windows.Forms.<span style="color:#2b91af DataGridViewColumn()

<span style="font-size:9.5pt; font-family:Consolas {<span style="color:blue Me.MydgvCol1,
<span style="color:blue Me.MydgvCol2})

<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:green
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:green MydgvCol1
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:green
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Me.MydgvCol1.HeaderText = <span style="color:#a31515
"MyDgvCol1"
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Me.MydgvCol1.Name = <span style="color:#a31515
"MydgvCol1"
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:green
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:green MydgvCol2
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:green
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Me.MydgvCol2.HeaderText = <span style="color:#a31515
"MyDgvCol2"
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Me.MydgvCol2.Name = <span style="color:#a31515
"MydgvCol2"

For MyDataGridView, I have been unable to get the Designer to emit this code to the ‘form.Designer.vb’ file.
The main class declarations and relevant code responsible for Serialization are shown in brief below :
1) MyDataGridView class :
<span style="font-size:9.5pt; font-family:Consolas <DesignTimeVisible(<span style="color:blue True),
<span style="font-size:9.5pt; font-family:Consolas <span> Designer(<span style="color:blue GetType(MyDataGridViewDsgnr)),
<span style="font-size:9.5pt; font-family:Consolas <span> DesignerSerializer(<span style="color:blue GetType(MyDataGridViewSrlzr),
<span style="font-size:9.5pt; font-family:Consolas <span>
<span> <span style="color:blue GetType(CodeDomSerializer))>
<span style="font-size:9.5pt; color:blue; font-family:Consolas Public<span style="font-size:9.5pt; font-family:Consolas
<span style="color:blue Class <span style="color:#2b91af MyDataGridView
<span style="font-size:9.5pt; color:blue; font-family:Consolas <span>
Inherits DataGridView
<span style="font-size:9.5pt; color:blue; font-family:Consolas <span>

<span>End Class
<span>
2) . MyDataGridViewSrlzr class :
<span style="font-size:9.5pt; color:blue; font-family:Consolas Public<span style="font-size:9.5pt; font-family:Consolas
<span style="color:blue Class <span style="color:#2b91af MyDataGridViewSrlzr
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Inherits <span style="color:#2b91af CodeDomSerializer
<span> …
End Class
Within this class, I have overridden the Serialize method, to do the following :
a) <span style="font-size:9.5pt; color:green; font-family:Consolas Associate the MyDataGridView’s Base type (ie: DataGridView) with the serializer and get the Base class Serializer :

<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Dim baseClassSerializer <span style="color:blue
As <span style="color:#2b91af CodeDomSerializer =
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue CType(manager.GetSerializer(
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue GetType(<span style="color:#2b91af MyDataGridView).BaseType,
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue GetType(<span style="color:#2b91af CodeDomSerializer)),

<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:#2b91af CodeDomSerializer)

<span style="font-size:9.5pt; font-family:Consolas Here, ‘manager’ is the <span style="color:#2b91af
IdesignerSerializationManager received by the Serialize method, as an argument, when it is invoked.

<span style="font-size:9.5pt; color:#2b91af; font-family:Consolas b) From the
<span style="font-size:9.5pt; color:green; font-family:Consolas Base class Serializer,<span style="font-size:9.5pt; color:#2b91af; font-family:Consolas get the CodeObject that serializes the Base object :

<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Dim codeObject <span style="color:blue As
<span style="color:blue Object =
<span style="font-size:9.5pt; font-family:Consolas baseClassSerializer.Serialize(manager, value)

<span style="font-size:9.5pt; color:#2b91af; font-family:Consolas c) Manipulate the CodeObject, as required.

<span>d) Return the CodeObject.
<span>

3) I am not sure if explicit code needs to be written to serialize the contained DataGridViewColumns. For this purpose, in the MyDataGridView class, I have overridden the Columns property as follows
<span> :
<span style="font-size:9.5pt; font-family:Consolas <span>
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
<span style="font-size:9.5pt; font-family:Consolas <span>
TypeConverter(<span style="color:blue GetType(DataGridViewColumnCnvrtr))>
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Public <span style="color:blue Overloads <span style="color:blue
ReadOnly <span style="color:blue Property Columns <span style="color:blue
As DataGridViewColumnCollection
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Get
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Return <span style="color:blue MyBase.Columns
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue End <span style="color:blue Get
<span><span> <span style="color:blue End <span>
Property
<span><span>
4) I have also written the <span style="font-size:9.5pt; font-family:Consolas
DataGridViewColumnCnvrtr class specified above, by extending TypeConverter. This is responsible for converting a DataGridViewColumn to an InstanceDescriptor.



The main code in the ConvertTo method is as follows :
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue If destinationType <span style="color:blue
Is <span style="color:blue GetType(<span style="color:#2b91af InstanceDescriptor)
<span style="color:blue Then
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Dim ctor <span style="color:blue As
<span style="color:#2b91af ConstructorInfo =
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue GetType(<span style="color:#2b91af DataGridViewColumn).GetConstructor(<span style="color:blue New
<span style="color:#2b91af Type() {})

<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Dim dgvc <span style="color:blue As
<span style="color:#2b91af DataGridViewColumn = <span style="color:blue
CType(value, <span style="color:#2b91af DataGridViewColumn)
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue If ctor <span style="color:blue IsNot
<span style="color:blue Nothing <span style="color:blue Then
<span style="font-size:9.5pt; font-family:Consolas <span> <span> <span style="color:blue Return
<span style="color:blue New <span style="color:#2b91af InstanceDescriptor(ctor,
<span style="color:blue New <span style="color:blue Object() {})
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue End <span style="color:blue If
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue End <span style="color:blue If
The code in Points (3) and (4) were written on a pure R&D basis.
Can anyone kindly advise on the right procedure for ensuring that a custom control’s contained controls are persisted at design-time ? Thanks. Sam Steven
<br/>

View the full article
 
Back
Top