Saving form or panel as an object
-
Hello, I want to save my form object. I am not succeeding and some one told me that it is impossible to save form object. So I am asking is it possible to save form object or panel object. If it is impossible then what should I do. What is good way to serialize an object and save It. Thanks in advance.
-
Hello, I want to save my form object. I am not succeeding and some one told me that it is impossible to save form object. So I am asking is it possible to save form object or panel object. If it is impossible then what should I do. What is good way to serialize an object and save It. Thanks in advance.
-
Hello, I want to save my form object. I am not succeeding and some one told me that it is impossible to save form object. So I am asking is it possible to save form object or panel object. If it is impossible then what should I do. What is good way to serialize an object and save It. Thanks in advance.
Serializing objects that contain other objects is not trivial. A Google search[^] turns up many results including some CP articles.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
Serializing objects that contain other objects is not trivial. A Google search[^] turns up many results including some CP articles.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)Yes I did Google search but there problem. I am not asking only object serialization. I want to ask is it possible to serialize the object of the form in which I am working. and when I open this form again I want to get the saved object of my form. Please give me guild line.
-
Yes I did Google search but there problem. I am not asking only object serialization. I want to ask is it possible to serialize the object of the form in which I am working. and when I open this form again I want to get the saved object of my form. Please give me guild line.
As davey said you have to use serialization which isn't an easy thing to do. I would start with something simpler (if you'v never done it before) and work my way up to a form. (start with just a control or something like that) Per definition you can serialize everything (as far as I know) so yes it's possible to serialize a form, but probably not going to be easy.
-
As davey said you have to use serialization which isn't an easy thing to do. I would start with something simpler (if you'v never done it before) and work my way up to a form. (start with just a control or something like that) Per definition you can serialize everything (as far as I know) so yes it's possible to serialize a form, but probably not going to be easy.
Yes I did serialization before but I never save Controls or form. Now I have to save the full form object or I will save all the form controls and get there properties and then put them to another serialize class then I will save it(It is very lengthy process). But I am in search of short cut. Like this.
SaveObject(this);
MyClass clss =Retriveobject();
Is it possible. If yes then how I am stucked.
-
Yes I did Google search but there problem. I am not asking only object serialization. I want to ask is it possible to serialize the object of the form in which I am working. and when I open this form again I want to get the saved object of my form. Please give me guild line.
It can be done but it's going to be slow! You'll need to use reflection to go through every property that you want to serialize. When you get to properties like Control, you'll have to do the same for every property of each control in the collection... There may be a better method but that's all I can think of. This little example will get you started. I've hard coded a few properties, but to make it a complete serialization you'll need to use reflection to get the properties/values instead as I said above.
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;namespace FormSerializationDemo
{
[Serializable]
public partial class Form1 : Form, ISerializable
{
public Form1()
{
InitializeComponent();
using (MemoryStream stream = new MemoryStream())
{
SerializeMe(stream);
// Do what you like with the stream here...
}
}private void SerializeMe(Stream stream) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, this); } public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("FormBorderStyle", FormBorderStyle); info.AddValue("Location", Location); info.AddValue("Name", Name); info.AddValue("Size", Size); info.AddValue("Text", Text); } }
}
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
Yes I did serialization before but I never save Controls or form. Now I have to save the full form object or I will save all the form controls and get there properties and then put them to another serialize class then I will save it(It is very lengthy process). But I am in search of short cut. Like this.
SaveObject(this);
MyClass clss =Retriveobject();
Is it possible. If yes then how I am stucked.
it is possible but you'll have to serialize every object on the form, then serialize the form itself. I have some code at home that does something like this I'll check it when I get home. But don't count on it being as easy as you typed there, not going to be possible to do in 2 lines of code :)
-
Hello, I want to save my form object. I am not succeeding and some one told me that it is impossible to save form object. So I am asking is it possible to save form object or panel object. If it is impossible then what should I do. What is good way to serialize an object and save It. Thanks in advance.
I've not tested it but you might be able to modify the following code; this basically does a deep copy of an object using serialization. Some minor modifications should allow to to write to a FileStream rather than a MemoryStream and then you'd extrapolate two functions (get and set).
public static T DeepCopy(T obj) { object result = null; using (var ms = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); ms.Position = 0; result = (T)formatter.Deserialize(ms); ms.Close(); } return (T)result; }
I haven't got time to work through the solution fully so I've left it as an exercise for you.It definitely isn't definatley
modified on Thursday, January 22, 2009 6:18 AM
-
It can be done but it's going to be slow! You'll need to use reflection to go through every property that you want to serialize. When you get to properties like Control, you'll have to do the same for every property of each control in the collection... There may be a better method but that's all I can think of. This little example will get you started. I've hard coded a few properties, but to make it a complete serialization you'll need to use reflection to get the properties/values instead as I said above.
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;namespace FormSerializationDemo
{
[Serializable]
public partial class Form1 : Form, ISerializable
{
public Form1()
{
InitializeComponent();
using (MemoryStream stream = new MemoryStream())
{
SerializeMe(stream);
// Do what you like with the stream here...
}
}private void SerializeMe(Stream stream) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, this); } public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("FormBorderStyle", FormBorderStyle); info.AddValue("Location", Location); info.AddValue("Name", Name); info.AddValue("Size", Size); info.AddValue("Text", Text); } }
}
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)Thanks for your reply and providing code. It is a good example but I think it will not solve my problem. Because I have an array of panels. These panels contain Four type of controls but number of controls is vary in each panel. with the your technique I think I cant add different panels with different controls. Any way thank you very much.
-
Thanks for your reply and providing code. It is a good example but I think it will not solve my problem. Because I have an array of panels. These panels contain Four type of controls but number of controls is vary in each panel. with the your technique I think I cant add different panels with different controls. Any way thank you very much.
If you read the text and not the code the solution is in there. Reflection and recursion.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
I've not tested it but you might be able to modify the following code; this basically does a deep copy of an object using serialization. Some minor modifications should allow to to write to a FileStream rather than a MemoryStream and then you'd extrapolate two functions (get and set).
public static T DeepCopy(T obj) { object result = null; using (var ms = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); ms.Position = 0; result = (T)formatter.Deserialize(ms); ms.Close(); } return (T)result; }
I haven't got time to work through the solution fully so I've left it as an exercise for you.It definitely isn't definatley
modified on Thursday, January 22, 2009 6:18 AM
Thanks for your code but the line
formatter.Serialize(ms, obj);
gives the following exception. I marked the class as [Serializable]. Type 'System.Windows.Forms.Form' in Assembly 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
-
I want to save The same form which contain all the panels , text boxes, and Labels. I want to save this form object to a file what should I do.
You can't do this. A Form is not serializable, as well as the controls on it may not all be serializable either. You have to write your own serializer to do something like this, where you provide the code to save whatever property values of the form, and all the child controls, required to recreate the objects using just that data.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008