Click here to Skip to main content
15,891,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys,

I have question on serialization.
I have made a form object and added some controls in it dynamically.
So, I want to save that form along with the controls in it.
That's why I decided to serialize the form object.
But when I call serialize the form object, an exception occurred.

BinaryFormatter objBinaryFormatter = new BinaryFormatter();
FileStream objFileStream = new FileStream(strCompleteFilePath, FileMode.OpenOrCreate);
objBinaryFormatter.Serialize(objFileStream, formObject);


Type 'System.Windows.Forms.Form' in Assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

Can anybody help me in this.
Cant we serialize a form object.
If we can, then please suggest some answers.

Thanks in advnace
Posted

1 solution

UI objects like Forms etc. are not meant to be serialized in this way, you are better off creating a "state" object within your form which will keep useful information and serialize that object instead.
 
Share this answer
 
Comments
leonidasvijay 17-Sep-13 0:26am    
Thanks for reply. But if I only serialize the state and object type then at the time of deserialize I have to recreate a new object for every tool. That will be a hectic work.
Mehdi Gholam 17-Sep-13 2:37am    
No one said programming was easy...
leonidasvijay 18-Sep-13 1:43am    
I didn't mean that. I wanted to say that at the time of deserialzation I have to create every tool object again and set their state in a new form. So, that will not be a god programming practices. If I used an image in a picturebox and serialize only its state then the image will lost at the time of deserialzation.
leonidasvijay 17-Sep-13 4:26am    
I didn't mean that. I wanted to say that at the time of deserialzation I have to create every tool object again and set their state in a new form. So, that will not be a god programming practices. If I used an image in a picturebox and serialize only its state then the image will lost at the time of deserialzation.
Mehdi Gholam 18-Sep-13 1:49am    
You save all the information you require in your state object (that may include your image files).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900