Click here to Skip to main content
15,922,584 members
Home / Discussions / C#
   

C#

 
GeneralRe: Saving form or panel as an object Pin
Naveed72721-Jan-09 22:54
Naveed72721-Jan-09 22:54 
GeneralRe: Saving form or panel as an object Pin
Dave Kreskowiak22-Jan-09 2:06
mveDave Kreskowiak22-Jan-09 2:06 
AnswerRe: Saving form or panel as an object Pin
DaveyM6921-Jan-09 23:15
professionalDaveyM6921-Jan-09 23:15 
GeneralRe: Saving form or panel as an object Pin
Naveed72721-Jan-09 23:19
Naveed72721-Jan-09 23:19 
GeneralRe: Saving form or panel as an object Pin
Tom Deketelaere21-Jan-09 23:27
professionalTom Deketelaere21-Jan-09 23:27 
GeneralRe: Saving form or panel as an object Pin
Naveed72721-Jan-09 23:35
Naveed72721-Jan-09 23:35 
GeneralRe: Saving form or panel as an object Pin
Tom Deketelaere21-Jan-09 23:59
professionalTom Deketelaere21-Jan-09 23:59 
GeneralRe: Saving form or panel as an object Pin
DaveyM6921-Jan-09 23:48
professionalDaveyM6921-Jan-09 23:48 
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)

GeneralRe: Saving form or panel as an object Pin
Naveed72722-Jan-09 0:13
Naveed72722-Jan-09 0:13 
GeneralRe: Saving form or panel as an object Pin
DaveyM6922-Jan-09 0:40
professionalDaveyM6922-Jan-09 0:40 
AnswerRe: Saving form or panel as an object [modified] Pin
moon_stick22-Jan-09 0:11
moon_stick22-Jan-09 0:11 
GeneralRe: Saving form or panel as an object Pin
Naveed72722-Jan-09 1:20
Naveed72722-Jan-09 1:20 
QuestionHow to avoid stack overflow ? Pin
Spunky Coder21-Jan-09 22:13
Spunky Coder21-Jan-09 22:13 
AnswerRe: How to avoid stack overflow ? Pin
Pete O'Hanlon21-Jan-09 22:40
mvePete O'Hanlon21-Jan-09 22:40 
AnswerRe: How to avoid stack overflow ? Pin
#realJSOP22-Jan-09 0:14
professional#realJSOP22-Jan-09 0:14 
GeneralRe: How to avoid stack overflow ? Pin
S. Senthil Kumar22-Jan-09 3:05
S. Senthil Kumar22-Jan-09 3:05 
AnswerRe: How to avoid stack overflow ? Pin
Guffa22-Jan-09 0:35
Guffa22-Jan-09 0:35 
GeneralRe: How to avoid stack overflow ? Pin
Spunky Coder26-Jan-09 22:34
Spunky Coder26-Jan-09 22:34 
GeneralRe: How to avoid stack overflow ? Pin
Guffa27-Jan-09 1:22
Guffa27-Jan-09 1:22 
AnswerRe: How to avoid stack overflow ? Pin
Ben Fair22-Jan-09 4:43
Ben Fair22-Jan-09 4:43 
GeneralRe: How to avoid stack overflow ? Pin
Spunky Coder26-Jan-09 23:05
Spunky Coder26-Jan-09 23:05 
GeneralRe: How to avoid stack overflow ? Pin
Guffa27-Jan-09 1:27
Guffa27-Jan-09 1:27 
GeneralRe: How to avoid stack overflow ? Pin
Ben Fair27-Jan-09 4:11
Ben Fair27-Jan-09 4:11 
QuestionSave Listview data to SQL database Pin
Tuntgerhuu21-Jan-09 22:06
Tuntgerhuu21-Jan-09 22:06 
AnswerRe: Save Listview data to SQL database Pin
CPallini21-Jan-09 23:22
mveCPallini21-Jan-09 23:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.