Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ListBox serialization wont work! :-(

ListBox serialization wont work! :-(

Scheduled Pinned Locked Moved C#
graphicsdockerjson
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Green Fuze
    wrote on last edited by
    #1

    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

    O 1 Reply Last reply
    0
    • G Green Fuze

      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

      O Offline
      O Offline
      occcy
      wrote on last edited by
      #2

      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.

      G 1 Reply Last reply
      0
      • O occcy

        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.

        G Offline
        G Offline
        Green Fuze
        wrote on last edited by
        #3

        I'm sorry for the complete ignorance, but how do I serialize the datasource? do you mean serializing each string, one by one?

        O 1 Reply Last reply
        0
        • G Green Fuze

          I'm sorry for the complete ignorance, but how do I serialize the datasource? do you mean serializing each string, one by one?

          O Offline
          O Offline
          occcy
          wrote on last edited by
          #4

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

          G 2 Replies Last reply
          0
          • O occcy

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

            G Offline
            G Offline
            Green Fuze
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • O occcy

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

              G Offline
              G Offline
              Green Fuze
              wrote on last edited by
              #6

              okay, I figured it out, and it serializes the array, but it serializes only the first item.

              O 1 Reply Last reply
              0
              • G Green Fuze

                okay, I figured it out, and it serializes the array, but it serializes only the first item.

                O Offline
                O Offline
                occcy
                wrote on last edited by
                #7

                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[]));

                G 1 Reply Last reply
                0
                • O occcy

                  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[]));

                  G Offline
                  G Offline
                  Green Fuze
                  wrote on last edited by
                  #8

                  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

                  O 1 Reply Last reply
                  0
                  • G Green Fuze

                    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

                    O Offline
                    O Offline
                    occcy
                    wrote on last edited by
                    #9

                    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.....

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups