Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Help With CodeDomSerializer
 

Help With CodeDomSerializer

Hi,

I am using this CodeDomSerializer to serialize some datagridviewcolumns into the form designer,However it seems to have a issue when properties of the datagridviewcolumn is changed, ie backcolor or alignment. Problem being it writes duplicate constructors for the datagridviewcolumn in the designer.

Can anyone help plz.

Public

Overrides Function Serialize(ByVal manager As System.ComponentModel.Design.Serialization.IDesignerSerializationManager, ByVal value As Object) As Object

Dim baseClassSerializer As CodeDomSerializer = DirectCast(manager.GetSerializer(GetType(WWSearchDataGrid).BaseType, GetType(CodeDomSerializer)), CodeDomSerializer)

Dim codeObject As Object = baseClassSerializer.Serialize(manager, value)

If TypeOf codeObject Is CodeStatementCollection Then

Dim statements As CodeStatementCollection = DirectCast(codeObject, CodeStatementCollection)

Dim commentText As String = "This comment was added to this object by a custom serializer"

Dim comment As CodeCommentStatement = New CodeCommentStatement(commentText)

statements.Insert(0, comment)

If TypeOf value Is WWSearchDataGrid Then

Dim columns As DataGridViewColumnCollection = TryCast(value, WWSearchDataGrid).Columns

Dim parameter_list As List(Of CodeVariableReferenceExpression) = New List(Of CodeVariableReferenceExpression)

Dim createArray As CodeArrayCreateExpression = Nothing

Dim methodcall As CodeMethodInvokeExpression = Nothing

Dim cmf As CodeMemberField = Nothing

Dim cmfs As List(Of CodeMemberField) = New List(Of CodeMemberField)

Dim i As Integer = 1

Dim col_Statements As CodeStatementCollection = Nothing

For Each col As DataGridViewColumn In columns

col_Statements =

New CodeStatementCollection

cmf =

New CodeMemberField(col.GetType(), col.Name)

cmf.Attributes = MemberAttributes.Assembly

Dim col_ObjectCreate As CodeObjectCreateExpression = New CodeObjectCreateExpression(col.GetType())

Dim col_Assign_Create As CodeAssignStatement = New CodeAssignStatement(New CodeVariableReferenceExpression(col.Name), col_ObjectCreate)

Dim col_Assign_DataPropertyName As CodeAssignStatement = New CodeAssignStatement(New CodeVariableReferenceExpression(col.Name + ".DataPropertyName"), New CodePrimitiveExpression(col.DataPropertyName))

Dim col_Assign_Name As CodeAssignStatement = New CodeAssignStatement(New CodeVariableReferenceExpression(col.Name + ".Name"), New CodePrimitiveExpression(col.Name))

Dim col_Assign_HeaderText As CodeAssignStatement = New CodeAssignStatement(New CodeVariableReferenceExpression(col.Name + ".HeaderText"), New CodePrimitiveExpression(col.HeaderText))

Dim col_Assign_ThreeState As CodeAssignStatement = New CodeAssignStatement(New CodeVariableReferenceExpression(col.Name + ".ThreeState"), New CodePrimitiveExpression(True))

If Me.GetExpression(manager, col) Is Nothing Then

cmfs.Add(cmf)

col_Statements.Add(col_Assign_Create)

col_Statements.Add(col_Assign_DataPropertyName)

col_Statements.Add(col_Assign_Name)

col_Statements.Add(col_Assign_HeaderText)

If TypeOf col Is DataGridViewCheckBoxColumn Then

col_Statements.Add(col_Assign_ThreeState)

End If

parameter_list.Add(

New CodeVariableReferenceExpression(col.Name))

End If

statements.AddRange(col_Statements)

i += 1

Next

Dim classcode As CodeTypeDeclaration = DirectCast(manager.GetService(GetType(CodeTypeDeclaration)), CodeTypeDeclaration)

If classcode IsNot Nothing Then

classcode.Members.AddRange(cmfs.ToArray)

End If

Dim target As CodeExpression = Me.GetExpression(manager, value)

If target IsNot Nothing And parameter_list.Count > 0 Then

createArray =

New CodeArrayCreateExpression(GetType(DataGridViewColumn), parameter_list.ToArray())

Dim colProp_Columns As CodePropertyReferenceExpression = New CodePropertyReferenceExpression(target, "Columns")

Dim colProp_AddRange As CodeMethodReferenceExpression = New CodeMethodReferenceExpression(colProp_Columns, "AddRange")

Dim col_Assign_Columns As CodeMethodInvokeExpression = New CodeMethodInvokeExpression(colProp_AddRange, createArray)

statements.Add(col_Assign_Columns)

Dim colPropExp As CodePropertyReferenceExpression = New CodePropertyReferenceExpression(target, "DataGridViewSource")

Dim col_Assign_DataGridViewSource As CodeAssignStatement = New CodeAssignStatement(colPropExp, New CodeVariableReferenceExpression(value.DataGridViewSource.Name))

statements.Add(col_Assign_DataGridViewSource)

End If

End If

End If

Return codeObject

End Function

garusher  Wednesday, August 12, 2009 2:04 PM

Hi garusher,

Could you please provide me the detail steps to reproduce the error? I have created a serializer and put your code in it. Then I set the serializer of the custom DataGridView class to the custom serializer. But I did not meet the same issue: duplicate constructors for the datagridviewcolumn.

In your code snippet, the statements are added to the old collection. Based on my understanding, we need to traverse the statements in the collection and remove some of them which are related to DataGridViewColumn.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, August 14, 2009 7:20 AM
Hi Aland,


Could i just provide you with my project?

I'll give your suggestion a try,

Thanks
Gary Rusher
garusher  Friday, August 14, 2009 12:32 PM
Hi Aland,

I'm still not having any luck with this duplicates thing. Would you be willing to take a look at my project?

Thanks
Gary Rusher
garusher  Friday, August 14, 2009 7:44 PM

Hi garusher,

Sorry for the late reply. Yes, you can. This is my email: alala666888@hotmail.com.

Regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Tuesday, August 18, 2009 2:35 AM
Hi Aland,

I've sent you the project files.

Thanks
Gary Rusher
garusher  Tuesday, August 18, 2009 1:19 PM
Hi Aland Li,

I've be testing this thing and it seems to be two ways this serializer is working.

1) when you first put the control on the form, click save, change and datagridview property and click save, then the serializer works ok. you can even repeat the previous multiple time, doesn't break.

However once you close the form and reopen it. this is where the problem occurs.

Now i have tested this and the duplicates are not being added by my serializer code, i believe they are being added by the datagridview's serializer, as they are appended to the form.designer.vb in a format that is identical todatagridviewcolumns automatically added by a datagridview.

Thanks
Gary Rusher
garusher  Tuesday, August 25, 2009 9:04 PM
Hi garusher,

Sorry for the late reply. You said i believe they are being added by the datagridview's serializer. I agree with you. Actually, we can trace the start comment of the custom serializer to check whether the code is generated by it. In your code, there are two custom serializers, one for DataGridView, the other for DataGridViewColumn. I have found the DataGridView serializer works, in other words, it generates some code. But the column serializer does not work, because it does not generate any code. Based on my understanding, we need to modify some code in the DataGridView serializer to avoid the code duplicating.

I am a little busy these days, but I would try my best to squeeze time to do the further researching. It is appreciated if you could provide more information. Thanks.

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, August 26, 2009 3:10 AM
Hi Aland Li,

How do i get a handle on the datagridview seriailizer from within my code?

If i canget a handle on it, i think i should erase the existing code and then manually add codestatements to the datagridview serializer.

Thanks
Gary Rusher
garusher  Wednesday, August 26, 2009 1:23 PM
Hi garusher,

As far as I know, there is no direct way to control the serialization of a control except bind a custom serializer to it. Based on my understanding, the problem is still in the custom serializer of the DataGridView. We can traverse the code statements and remove the extra code in the Serialize method. If we want to control the serialization, we need to create a custom serializer of the Form. I did not test my ideas, so please feel free to let me know if some of them are not legal.

Regards,
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, August 27, 2009 2:09 AM

You can use google to search for other answers

Custom Search

More Threads

• windows form designer error:The class Settings can be designed, but is not the first class in the file...."
• custom designer for component that inherits from ToolStripItem
• DataColumn property in Component
• DataGridView column separator can't move beyond window limits.
• How do I retrieve current Project Name and Path for form currently open in the designer
• Hosing Form Designer and control property "Enabled" problem
• Custom Cursor refuse to work...
• ideas for composite control (is this possible?)
• Problems w/ standard windows forms controls when add custom control
• How to Change the settings of an application with User.config