By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,680 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,680 IT Pros & Developers. It's quick & easy.

Serialize form

P: n/a
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.SerializationExcepti on' 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();
}
}
}

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <pa****@hotmail.com> wrote in message
news:Or**************@TK2MSFTNGP09.phx.gbl...
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.SerializationExcepti on' 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();
}
}
}

Nov 16 '05 #2

P: n/a
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" <pe***@nospam.rilling.net> 在郵件
news:uU*************@TK2MSFTNGP10.phx.gbl 中撰寫...
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" <pa****@hotmail.com> wrote in message
news:Or**************@TK2MSFTNGP09.phx.gbl...
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.SerializationExcepti on' 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();
}
}
}


Nov 16 '05 #3

P: n/a
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" <pa****@hotmail.com> wrote in message
news:e7**************@TK2MSFTNGP12.phx.gbl...
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" <pe***@nospam.rilling.net> 在郵件
news:uU*************@TK2MSFTNGP10.phx.gbl 中撰寫...
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" <pa****@hotmail.com> wrote in message
news:Or**************@TK2MSFTNGP09.phx.gbl...
> 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.SerializationExcepti on' 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();
> }
> }
> }
>
>
>



Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.