Object Serializatiion
Would welcone some help here please.
I have read a few tutorials on serilization and seems OK BUT I do not seem to be able to serialize any Form Objects eg Buttons, TextBoxes etc, which is the very reason I want to use it ;-))
I manage to compileOK but the farthest I get is the following
"
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
Additional information: The type System.Windows.Forms.Button in Assembly System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 is not marked as serializable.
"
However the Class they are in is marked, and if I try to mark the objects themselves it says I cant do it!
Would really appreciate any help out there.
Many Thanks
Alan
Mahesh ChandPosted Jul 14, 2005, 9:40 AM
AlanPosted Jul 14, 2005, 8:47 AM
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
FormClass{
///{
public System.Windows.Forms.Label label1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button btnSave; private System.Windows.Forms.Button btnOpen; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.TextBox textBox4; ///{
// // Required for Windows Form Designer support //InitializeComponent();
// // TODO: Add any constructor code after InitializeComponent call //}
///{
if( disposing ){
if (components != null){
components.Dispose();
}
}
base.Dispose( disposing );}
#region
Windows Form Designer generated code ///{
this.label1 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.btnSave = new System.Windows.Forms.Button(); this.btnOpen = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox3 = new System.Windows.Forms.TextBox(); this.textBox4 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(8, 8); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(264, 23); this.label1.TabIndex = 0; this.label1.Text = "label1"; // // button1 // this.button1.Location = new System.Drawing.Point(96, 40); this.button1.Name = "button1"; this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); // // btnSave // this.btnSave.Location = new System.Drawing.Point(96, 160); this.btnSave.Name = "btnSave"; this.btnSave.TabIndex = 2; this.btnSave.Text = "Save"; this.btnSave.Click += new System.EventHandler(this.btnSave_Click); // // btnOpen // this.btnOpen.Location = new System.Drawing.Point(96, 208); this.btnOpen.Name = "btnOpen"; this.btnOpen.TabIndex = 3; this.btnOpen.Text = "Open"; this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(8, 80); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 4; this.textBox1.Text = "textBox1"; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(184, 80); this.textBox2.Name = "textBox2"; this.textBox2.TabIndex = 5; this.textBox2.Text = "textBox2"; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(8, 120); this.textBox3.Name = "textBox3"; this.textBox3.TabIndex = 6; this.textBox3.Text = "textBox3"; // // textBox4 // this.textBox4.Location = new System.Drawing.Point(184, 120); this.textBox4.Name = "textBox4"; this.textBox4.TabIndex = 7; this.textBox4.Text = "textBox4"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 262); this.Controls.Add(this.textBox4); this.Controls.Add(this.textBox3); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.btnOpen); this.Controls.Add(this.btnSave); this.Controls.Add(this.button1); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false);}
#endregion
///[STAThread]
static void Main(){
Application.Run(
new Form1());}
private void button1_Click(object sender, System.EventArgs e){
textBox1.BackColor = Color.AliceBlue;
textBox2.ForeColor = Color.Blue;
}
private void btnSave_Click(object sender, System.EventArgs e){
// SerializeClass1 myClass =
new Class1(); foreach ( object item in this.Controls ){
if ( item.GetType() == typeof(TextBox)){
myClass.saveplus.Add(item);
}
}
BinaryFormatter bf =
new BinaryFormatter();Stream stream = File.Open("Myserial.dat",FileMode.Create );
bf.Serialize(stream, myClass);
stream.Close();
}
private void btnOpen_Click(object sender, System.EventArgs e){
// De-SerializeBinaryFormatter bf =
new BinaryFormatter();Stream stream = File.Open("Myserial.dat",FileMode.Open);
Class1 myClass = (Class1) bf.Deserialize(stream);
stream.Close();
int i = 0; foreach ( Control item in this.Controls ){
if ( item.GetType() == typeof(TextBox)){
item.Text = (
string)myClass.saveplus[i];item.BackColor = (Color)myClass.saveplus[i+1];
item.ForeColor = (Color)myClass.saveplus[i+2];
i += 3;
}
}
}
}
}
-------------------------------------------------------------
using
System;using
System.Drawing;using
System.Windows.Forms;using
System.Runtime.Serialization;using
System.Runtime.Serialization.Formatters.Binary;using
System.Collections;namespace
FormClass{
///[Serializable()]
public class Class1 : ISerializable{
public ArrayList saveplus = new ArrayList(); public Class1(){
}
#region
ISerializable Members public void GetObjectData(SerializationInfo info, StreamingContext context){
// SerializeTextBox tb;
foreach ( TextBox item in saveplus ){
tb = (TextBox)item;
info.AddValue(tb.Name, tb.Text);
info.AddValue(tb.Name + "a", tb.BackColor);
info.AddValue(tb.Name + "b", tb.ForeColor);
}
}
public Class1(SerializationInfo info, StreamingContext context){
// De Serialize for ( int i = 4; i > 0; i -= 1 ){
// tb = (TextBox) item;saveplus.Add((
string)info.GetValue("textBox" + i, typeof(string)));saveplus.Add((Color)info.GetValue("textBox" + i + "a",
typeof(Color)));saveplus.Add((Color)info.GetValue("textBox" + i + "b",
typeof(Color)));}
}
#endregion
}
}