Serialize form

M

MAY

Hi,
I have a problem about serialize the form controls. I wrote a test program
to test serialize a from but fail (->An unhandled exception of type
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll) . Thx in advance. Here is the part of the code:

Regards

MAY


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Test_Serializable
{

[Serializable]
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;

public Form2 form2=new Form2();

public Form1()
{
InitializeComponent();
}

[STAThread()]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
IFormatter formatter=new BinaryFormatter();
Stream stream=new FileStream("kaka.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, form2);
stream.Close();
}

private void button2_Click(object sender, System.EventArgs e)
{
IFormatter formatter=new BinaryFormatter();
Stream stream=new FileStream("kaka.bin", FileMode.Open , FileAccess.Read,
FileShare.None);
Form2 ok=(Form2)formatter.Deserialize(stream);
stream.Close();
Console.Write(ok.teststring.ToString());
}

private void button3_Click(object sender, System.EventArgs e)
{
form2.ShowDialog();
}
}
}



//////////////////////////form2

[Serializable()]
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;

public string teststring="no value";


private void button2_Click(object sender, System.EventArgs e)
{
this.teststring=this.textBox1.Text;
this.Dispose();
}

private void button1_Click(object sender, System.EventArgs e)
{
this.teststring="ooooooooooooooooooopppsss";
this.Dispose();
}
}
}
 
P

Peter Rilling

I don't know what the specific error means, but I would recommend against
trying to serialize something as complex as a form. The reason being that
the serializing engine will walk through the member variables and when you
try to serialize objects that maybe were not meant to be serialized (i.e. a
Button), you risk having conflicts. All nested object must also be
serializable for this to work.

What is the purpose for trying to serialize a Form object? Is it so that
you can easily recreate the form's state later? You might want to consider
creating a data structure that holds all the form state information which
you could then serialize.


MAY said:
Hi,
I have a problem about serialize the form controls. I wrote a test program
to test serialize a from but fail (->An unhandled exception of type
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll) . Thx in advance. Here is the part of the code:

Regards

MAY


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Test_Serializable
{

[Serializable]
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;

public Form2 form2=new Form2();

public Form1()
{
InitializeComponent();
}

[STAThread()]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
IFormatter formatter=new BinaryFormatter();
Stream stream=new FileStream("kaka.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, form2);
stream.Close();
}

private void button2_Click(object sender, System.EventArgs e)
{
IFormatter formatter=new BinaryFormatter();
Stream stream=new FileStream("kaka.bin", FileMode.Open , FileAccess.Read,
FileShare.None);
Form2 ok=(Form2)formatter.Deserialize(stream);
stream.Close();
Console.Write(ok.teststring.ToString());
}

private void button3_Click(object sender, System.EventArgs e)
{
form2.ShowDialog();
}
}
}



//////////////////////////form2

[Serializable()]
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;

public string teststring="no value";


private void button2_Click(object sender, System.EventArgs e)
{
this.teststring=this.textBox1.Text;
this.Dispose();
}

private void button1_Click(object sender, System.EventArgs e)
{
this.teststring="ooooooooooooooooooopppsss";
this.Dispose();
}
}
}
 
M

MAY

Hi Peter,

Thx for reply. Actually, i want to serialize a static collectionbase class
that store few customized buttons which have location, bitmap, and some
controls properties. These button was added to collectionbase class by the
users in runtime. Can i do it in that way? Thx again.

MAY


Peter Rilling said:
I don't know what the specific error means, but I would recommend against
trying to serialize something as complex as a form. The reason being that
the serializing engine will walk through the member variables and when you
try to serialize objects that maybe were not meant to be serialized (i.e. a
Button), you risk having conflicts. All nested object must also be
serializable for this to work.

What is the purpose for trying to serialize a Form object? Is it so that
you can easily recreate the form's state later? You might want to consider
creating a data structure that holds all the form state information which
you could then serialize.


MAY said:
Hi,
I have a problem about serialize the form controls. I wrote a test program
to test serialize a from but fail (->An unhandled exception of type
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll) . Thx in advance. Here is the part of the code:

Regards

MAY


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Test_Serializable
{

[Serializable]
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;

public Form2 form2=new Form2();

public Form1()
{
InitializeComponent();
}

[STAThread()]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
IFormatter formatter=new BinaryFormatter();
Stream stream=new FileStream("kaka.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, form2);
stream.Close();
}

private void button2_Click(object sender, System.EventArgs e)
{
IFormatter formatter=new BinaryFormatter();
Stream stream=new FileStream("kaka.bin", FileMode.Open , FileAccess.Read,
FileShare.None);
Form2 ok=(Form2)formatter.Deserialize(stream);
stream.Close();
Console.Write(ok.teststring.ToString());
}

private void button3_Click(object sender, System.EventArgs e)
{
form2.ShowDialog();
}
}
}



//////////////////////////form2

[Serializable()]
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;

public string teststring="no value";


private void button2_Click(object sender, System.EventArgs e)
{
this.teststring=this.textBox1.Text;
this.Dispose();
}

private void button1_Click(object sender, System.EventArgs e)
{
this.teststring="ooooooooooooooooooopppsss";
this.Dispose();
}
}
}
 
V

Vijaye Raji

I would suggest, you write your own serialization routine for your
customized button and then, call that routine for all the form's children
from the form. The idea here is, do not depend on the Serializers for UI
elements. You will run into issues when any of these controls have
references to some other object that is not serializable, (or cicular
references - I'm not sure about this part, though).

It's easy to write a simple serializer. I would recommend using XML, to
help you debug easier. Using Reflection, gather all the properties of the
button you are interested in and then, write out xml snippets for each.

HTH

-vJ

MAY said:
Hi Peter,

Thx for reply. Actually, i want to serialize a static collectionbase class
that store few customized buttons which have location, bitmap, and some
controls properties. These button was added to collectionbase class by the
users in runtime. Can i do it in that way? Thx again.

MAY


Peter Rilling said:
I don't know what the specific error means, but I would recommend against
trying to serialize something as complex as a form. The reason being
that
the serializing engine will walk through the member variables and when
you
try to serialize objects that maybe were not meant to be serialized (i.e. a
Button), you risk having conflicts. All nested object must also be
serializable for this to work.

What is the purpose for trying to serialize a Form object? Is it so that
you can easily recreate the form's state later? You might want to consider
creating a data structure that holds all the form state information which
you could then serialize.


MAY said:
Hi,
I have a problem about serialize the form controls. I wrote a test program
to test serialize a from but fail (->An unhandled exception of type
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll) . Thx in advance. Here is the part of the code:

Regards

MAY


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Test_Serializable
{

[Serializable]
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;

public Form2 form2=new Form2();

public Form1()
{
InitializeComponent();
}

[STAThread()]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
IFormatter formatter=new BinaryFormatter();
Stream stream=new FileStream("kaka.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, form2);
stream.Close();
}

private void button2_Click(object sender, System.EventArgs e)
{
IFormatter formatter=new BinaryFormatter();
Stream stream=new FileStream("kaka.bin", FileMode.Open , FileAccess.Read,
FileShare.None);
Form2 ok=(Form2)formatter.Deserialize(stream);
stream.Close();
Console.Write(ok.teststring.ToString());
}

private void button3_Click(object sender, System.EventArgs e)
{
form2.ShowDialog();
}
}
}



//////////////////////////form2

[Serializable()]
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;

public string teststring="no value";


private void button2_Click(object sender, System.EventArgs e)
{
this.teststring=this.textBox1.Text;
this.Dispose();
}

private void button1_Click(object sender, System.EventArgs e)
{
this.teststring="ooooooooooooooooooopppsss";
this.Dispose();
}
}
}
 

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