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. Saving form or panel as an object

Saving form or panel as an object

Scheduled Pinned Locked Moved C#
question
14 Posts 6 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.
  • N Offline
    N Offline
    Naveed727
    wrote on last edited by
    #1

    Hello, I want to save my form object. I am not succeeding and some one told me that it is impossible to save form object. So I am asking is it possible to save form object or panel object. If it is impossible then what should I do. What is good way to serialize an object and save It. Thanks in advance.

    M D M 3 Replies Last reply
    0
    • N Naveed727

      Hello, I want to save my form object. I am not succeeding and some one told me that it is impossible to save form object. So I am asking is it possible to save form object or panel object. If it is impossible then what should I do. What is good way to serialize an object and save It. Thanks in advance.

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      what do you mean by saving form object?

      N 1 Reply Last reply
      0
      • M musefan

        what do you mean by saving form object?

        N Offline
        N Offline
        Naveed727
        wrote on last edited by
        #3

        I want to save The same form which contain all the panels , text boxes, and Labels. I want to save this form object to a file what should I do.

        D 1 Reply Last reply
        0
        • N Naveed727

          Hello, I want to save my form object. I am not succeeding and some one told me that it is impossible to save form object. So I am asking is it possible to save form object or panel object. If it is impossible then what should I do. What is good way to serialize an object and save It. Thanks in advance.

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          Serializing objects that contain other objects is not trivial. A Google search[^] turns up many results including some CP articles.

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

          N 1 Reply Last reply
          0
          • D DaveyM69

            Serializing objects that contain other objects is not trivial. A Google search[^] turns up many results including some CP articles.

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

            N Offline
            N Offline
            Naveed727
            wrote on last edited by
            #5

            Yes I did Google search but there problem. I am not asking only object serialization. I want to ask is it possible to serialize the object of the form in which I am working. and when I open this form again I want to get the saved object of my form. Please give me guild line.

            T D 2 Replies Last reply
            0
            • N Naveed727

              Yes I did Google search but there problem. I am not asking only object serialization. I want to ask is it possible to serialize the object of the form in which I am working. and when I open this form again I want to get the saved object of my form. Please give me guild line.

              T Offline
              T Offline
              Tom Deketelaere
              wrote on last edited by
              #6

              As davey said you have to use serialization which isn't an easy thing to do. I would start with something simpler (if you'v never done it before) and work my way up to a form. (start with just a control or something like that) Per definition you can serialize everything (as far as I know) so yes it's possible to serialize a form, but probably not going to be easy.

              N 1 Reply Last reply
              0
              • T Tom Deketelaere

                As davey said you have to use serialization which isn't an easy thing to do. I would start with something simpler (if you'v never done it before) and work my way up to a form. (start with just a control or something like that) Per definition you can serialize everything (as far as I know) so yes it's possible to serialize a form, but probably not going to be easy.

                N Offline
                N Offline
                Naveed727
                wrote on last edited by
                #7

                Yes I did serialization before but I never save Controls or form. Now I have to save the full form object or I will save all the form controls and get there properties and then put them to another serialize class then I will save it(It is very lengthy process). But I am in search of short cut. Like this.

                SaveObject(this);

                MyClass clss =Retriveobject();

                Is it possible. If yes then how I am stucked.

                T 1 Reply Last reply
                0
                • N Naveed727

                  Yes I did Google search but there problem. I am not asking only object serialization. I want to ask is it possible to serialize the object of the form in which I am working. and when I open this form again I want to get the saved object of my form. Please give me guild line.

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  It can be done but it's going to be slow! You'll need to use reflection to go through every property that you want to serialize. When you get to properties like Control, you'll have to do the same for every property of each control in the collection... There may be a better method but that's all I can think of. This little example will get you started. I've hard coded a few properties, but to make it a complete serialization you'll need to use reflection to get the properties/values instead as I said above.

                  using System;
                  using System.IO;
                  using System.Runtime.Serialization;
                  using System.Runtime.Serialization.Formatters.Binary;
                  using System.Windows.Forms;

                  namespace FormSerializationDemo
                  {
                  [Serializable]
                  public partial class Form1 : Form, ISerializable
                  {
                  public Form1()
                  {
                  InitializeComponent();
                  using (MemoryStream stream = new MemoryStream())
                  {
                  SerializeMe(stream);
                  // Do what you like with the stream here...
                  }
                  }

                      private void SerializeMe(Stream stream)
                      {
                          BinaryFormatter formatter = new BinaryFormatter();
                          formatter.Serialize(stream, this);
                      }
                  
                      public void GetObjectData(SerializationInfo info, StreamingContext context)
                      {
                          info.AddValue("FormBorderStyle", FormBorderStyle);
                          info.AddValue("Location", Location);
                          info.AddValue("Name", Name);
                          info.AddValue("Size", Size);
                          info.AddValue("Text", Text);
                      }
                  }
                  

                  }

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                  N 1 Reply Last reply
                  0
                  • N Naveed727

                    Yes I did serialization before but I never save Controls or form. Now I have to save the full form object or I will save all the form controls and get there properties and then put them to another serialize class then I will save it(It is very lengthy process). But I am in search of short cut. Like this.

                    SaveObject(this);

                    MyClass clss =Retriveobject();

                    Is it possible. If yes then how I am stucked.

                    T Offline
                    T Offline
                    Tom Deketelaere
                    wrote on last edited by
                    #9

                    it is possible but you'll have to serialize every object on the form, then serialize the form itself. I have some code at home that does something like this I'll check it when I get home. But don't count on it being as easy as you typed there, not going to be possible to do in 2 lines of code :)

                    1 Reply Last reply
                    0
                    • N Naveed727

                      Hello, I want to save my form object. I am not succeeding and some one told me that it is impossible to save form object. So I am asking is it possible to save form object or panel object. If it is impossible then what should I do. What is good way to serialize an object and save It. Thanks in advance.

                      M Offline
                      M Offline
                      moon_stick
                      wrote on last edited by
                      #10

                      I've not tested it but you might be able to modify the following code; this basically does a deep copy of an object using serialization. Some minor modifications should allow to to write to a FileStream rather than a MemoryStream and then you'd extrapolate two functions (get and set). public static T DeepCopy(T obj) { object result = null; using (var ms = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); ms.Position = 0; result = (T)formatter.Deserialize(ms); ms.Close(); } return (T)result; } I haven't got time to work through the solution fully so I've left it as an exercise for you.

                      It definitely isn't definatley

                      modified on Thursday, January 22, 2009 6:18 AM

                      N 1 Reply Last reply
                      0
                      • D DaveyM69

                        It can be done but it's going to be slow! You'll need to use reflection to go through every property that you want to serialize. When you get to properties like Control, you'll have to do the same for every property of each control in the collection... There may be a better method but that's all I can think of. This little example will get you started. I've hard coded a few properties, but to make it a complete serialization you'll need to use reflection to get the properties/values instead as I said above.

                        using System;
                        using System.IO;
                        using System.Runtime.Serialization;
                        using System.Runtime.Serialization.Formatters.Binary;
                        using System.Windows.Forms;

                        namespace FormSerializationDemo
                        {
                        [Serializable]
                        public partial class Form1 : Form, ISerializable
                        {
                        public Form1()
                        {
                        InitializeComponent();
                        using (MemoryStream stream = new MemoryStream())
                        {
                        SerializeMe(stream);
                        // Do what you like with the stream here...
                        }
                        }

                            private void SerializeMe(Stream stream)
                            {
                                BinaryFormatter formatter = new BinaryFormatter();
                                formatter.Serialize(stream, this);
                            }
                        
                            public void GetObjectData(SerializationInfo info, StreamingContext context)
                            {
                                info.AddValue("FormBorderStyle", FormBorderStyle);
                                info.AddValue("Location", Location);
                                info.AddValue("Name", Name);
                                info.AddValue("Size", Size);
                                info.AddValue("Text", Text);
                            }
                        }
                        

                        }

                        Dave
                        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                        N Offline
                        N Offline
                        Naveed727
                        wrote on last edited by
                        #11

                        Thanks for your reply and providing code. It is a good example but I think it will not solve my problem. Because I have an array of panels. These panels contain Four type of controls but number of controls is vary in each panel. with the your technique I think I cant add different panels with different controls. Any way thank you very much.

                        D 1 Reply Last reply
                        0
                        • N Naveed727

                          Thanks for your reply and providing code. It is a good example but I think it will not solve my problem. Because I have an array of panels. These panels contain Four type of controls but number of controls is vary in each panel. with the your technique I think I cant add different panels with different controls. Any way thank you very much.

                          D Offline
                          D Offline
                          DaveyM69
                          wrote on last edited by
                          #12

                          If you read the text and not the code the solution is in there. Reflection and recursion.

                          Dave
                          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                          1 Reply Last reply
                          0
                          • M moon_stick

                            I've not tested it but you might be able to modify the following code; this basically does a deep copy of an object using serialization. Some minor modifications should allow to to write to a FileStream rather than a MemoryStream and then you'd extrapolate two functions (get and set). public static T DeepCopy(T obj) { object result = null; using (var ms = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); ms.Position = 0; result = (T)formatter.Deserialize(ms); ms.Close(); } return (T)result; } I haven't got time to work through the solution fully so I've left it as an exercise for you.

                            It definitely isn't definatley

                            modified on Thursday, January 22, 2009 6:18 AM

                            N Offline
                            N Offline
                            Naveed727
                            wrote on last edited by
                            #13

                            Thanks for your code but the line

                            formatter.Serialize(ms, obj);

                            gives the following exception. I marked the class as [Serializable]. Type 'System.Windows.Forms.Form' in Assembly 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

                            1 Reply Last reply
                            0
                            • N Naveed727

                              I want to save The same form which contain all the panels , text boxes, and Labels. I want to save this form object to a file what should I do.

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #14

                              You can't do this. A Form is not serializable, as well as the controls on it may not all be serializable either. You have to write your own serializer to do something like this, where you provide the code to save whatever property values of the form, and all the child controls, required to recreate the objects using just that data.

                              A guide to posting questions on CodeProject[^]
                              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                   2006, 2007, 2008

                              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