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();
}
}
}
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();
}
}
}