473,416 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Serialize form

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
3 10363
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
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> ¦b¶l¥ó
news:uU*************@TK2MSFTNGP10.phx.gbl ¤¤¼¶¼g...
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
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> ¦b¶l¥ó
news:uU*************@TK2MSFTNGP10.phx.gbl ¤¤¼¶¼g...
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: sandy | last post by:
With java servlets I can declare complex object-oriented class structures as session variables in a servlet. That means I can have a complex HTML form that submits iteratively back to the server...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
2
by: films | last post by:
I understand the concept. Serialization of a class will add all the sub-objects of the class to the stream if there are also serializible. So say I have: class Author {
5
by: objectref | last post by:
Hi to all, let's say i have a form with 2 buttons and 3 textboxes on it. Is there a way to Serialize the form so when i de-Serialize and cast it to a Form, i will get back the original form with...
3
by: Jeff Richardson | last post by:
This is a repost from the InfoPath news group. Hi, I am writing a SharePoint application that works with InfoPath forms. When a user submits a completed InfoPath form to a forms library my code...
2
by: Joe | last post by:
If I serialize an object from with the same class, any fields with the NonSerializableAttribute still get serialized but not if I serialize from outside the class. Why? This is a simple case to...
3
by: VingeFaan | last post by:
Hello. Can I serialize a UserControl directly? By that I mean the entire control itself, not by using extra inner classes or specific logic for each usercontrol? I have 50ish usercontrols which I...
1
by: donrawe42 | last post by:
Hi, I'm trying to submit a for using remote function and Form.serialize, if I render the contents of the parameter in the controller all I see is . I understand that Form.serialize creates the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.