ListBox serialization wont work! :-(
-
Hey I'm really trying to serialize a listbox in my form, but I just keep getting an exception that tells me that it is not serializable I'm writing down the code of the Main form.
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace Serialize_ListBox { [Serializable()] public class MainForm : System.Windows.Forms.Form, ISerializable { private System.Windows.Forms.ListBox lbText; private System.Windows.Forms.Button btnSave; private System.Windows.Forms.Button btnAdd; private Form ThisForm; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public MainForm() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { //initializing stuff.... } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new MainForm()); } private void btnAdd_Click(object sender, System.EventArgs e) { AddDialog dlgAddText = new AddDialog(); if(dlgAddText.ShowDialog() == DialogResult.OK) { lbText.BeginUpdate(); lbText.Items.Add(dlgAddText.txtAdd.Text); lbText.EndUpdate(); } } private void btnSave_Click(object sender, System.EventArgs e) { System.IO.Stream stream = System.IO.File.Open("listbox.bla", System.IO.FileMode.OpenOrCreate); BinaryFormatter BF = new BinaryFormatter(); BF.Serialize(stream, lbText); stream.Close(); MessageBox.Show("ListBox Saved"); } //Deserialization constructor public MainForm(SerializationInfo info, StreamingContext ctxt) { lbText = (ListBox)info.Ge
-
Hey I'm really trying to serialize a listbox in my form, but I just keep getting an exception that tells me that it is not serializable I'm writing down the code of the Main form.
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace Serialize_ListBox { [Serializable()] public class MainForm : System.Windows.Forms.Form, ISerializable { private System.Windows.Forms.ListBox lbText; private System.Windows.Forms.Button btnSave; private System.Windows.Forms.Button btnAdd; private Form ThisForm; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public MainForm() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { //initializing stuff.... } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new MainForm()); } private void btnAdd_Click(object sender, System.EventArgs e) { AddDialog dlgAddText = new AddDialog(); if(dlgAddText.ShowDialog() == DialogResult.OK) { lbText.BeginUpdate(); lbText.Items.Add(dlgAddText.txtAdd.Text); lbText.EndUpdate(); } } private void btnSave_Click(object sender, System.EventArgs e) { System.IO.Stream stream = System.IO.File.Open("listbox.bla", System.IO.FileMode.OpenOrCreate); BinaryFormatter BF = new BinaryFormatter(); BF.Serialize(stream, lbText); stream.Close(); MessageBox.Show("ListBox Saved"); } //Deserialization constructor public MainForm(SerializationInfo info, StreamingContext ctxt) { lbText = (ListBox)info.Ge
-
it doesn't work because listbox doesn't implement the ISerializable interface... try to serialize the datasource of your listbox... if it is an array of string, it can be serialized without any problem.
I'm sorry for the complete ignorance, but how do I serialize the datasource? do you mean serializing each string, one by one?
-
I'm sorry for the complete ignorance, but how do I serialize the datasource? do you mean serializing each string, one by one?
-
try this:
string[] items; items=new string[listbox.Items.Count] for(int i=0;i<listbox.Items.Count;i++) { items[i]=(string)listbox.Items[i]; } info.AddValue("List",items);
still not working... :-( I get a null exception in this line: ('cnt' in my program is the 'i' in your example) items[cnt] = (string)lbText.Items[cnt]; I get the exception in cnt=0 even though lbText.Items[0] has a string in it, and it is not null.
-
try this:
string[] items; items=new string[listbox.Items.Count] for(int i=0;i<listbox.Items.Count;i++) { items[i]=(string)listbox.Items[i]; } info.AddValue("List",items);
okay, I figured it out, and it serializes the array, but it serializes only the first item.
-
okay, I figured it out, and it serializes the array, but it serializes only the first item.
hmmm?! it should work! perhaps you don't serialize/deserialize with the right type? try this: serialize:
string[] items; ... info.AddValue("listbox",items,typeof(string[]));
deserialize:string[] items; items=(string[])info.GetValue("listbox",typeof(string[]));
-
hmmm?! it should work! perhaps you don't serialize/deserialize with the right type? try this: serialize:
string[] items; ... info.AddValue("listbox",items,typeof(string[]));
deserialize:string[] items; items=(string[])info.GetValue("listbox",typeof(string[]));
it works! :-) I had to turn the items[] into a member because thats what I need to serialize in the serialization command: BF.Serialize(stream, items); and if I just create the array in the serialize or the deserialize it doesn't exist for me to serialize. Thanks alot!!! :-) I have just two more REALLY small questions (and I think your the MAN with the answers!): 1. is there anyway to serialize an object as is? I mean, list just serializing a listbox or a combobox, and all the infomation inside will be saved? 2. I don't get exactly what the Initialize() command in the array class is doing, or when should I use it... Thanks Again! :-D
-
it works! :-) I had to turn the items[] into a member because thats what I need to serialize in the serialization command: BF.Serialize(stream, items); and if I just create the array in the serialize or the deserialize it doesn't exist for me to serialize. Thanks alot!!! :-) I have just two more REALLY small questions (and I think your the MAN with the answers!): 1. is there anyway to serialize an object as is? I mean, list just serializing a listbox or a combobox, and all the infomation inside will be saved? 2. I don't get exactly what the Initialize() command in the array class is doing, or when should I use it... Thanks Again! :-D
Green Fuze wrote: 1. is there anyway to serialize an object as is? I mean, list just serializing a listbox or a combobox, and all the infomation inside will be saved? Search for "Control.DataBindings Property" in your vs.net help or msdn. I think that's what you are looking for. Green Fuze wrote: 2. I don't get exactly what the Initialize() command in the array class is doing, or when should I use it... Search for "Array.Initialize Method" in your vs.net help or msdn too. Mostly you don't need to call this method. For example you have an array of string, the compiler sets all strings inside this array automatically to an empty string.....