tags:

views:

1276

answers:

5

How to auto save all properties winforms when closed and auto load all properties winforms when load ? C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace SControl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < Controls.Count; i++)
            {
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(Controls[i]));
                Stream stream = File.Open("test.xml", FileMode.Create);
                x.Serialize(stream, Controls[i]);
            }
        }
    }
}
A: 

I'm not aware of any automatic method built into the Form base class, but adding it yourself isn't hard.

You could tap the window load and close events to cache off all relevant properties to a backing store and then reload them later.

Register an event handler to the Form.Load and Form.Closing event handlers. When Form.Closing occurs, save the forms state to a file or database. When Form.Load occurs, check to see if a saved state is present and if so, reload the from from the saved state.

Ryan Michela
A: 

You have to manually code which properties to be saved.

A convenient method is to svae these personalized settings to Windows Forms Application Settings.

Sample code snippet:

//save the winform position and size upon closing
private void Form1_FormClosed(
   object sender, FormClosedEventArgs e)
{
    Properties.Settings.Default.FormPosition = this.Location;
    Properties.Settings.Default.FormSize = this.Size;
    Properties.Settings.Default.Save();
}

//load the winform position and size upon loading
private void Form1_Load(object sender, EventArgs e)
{
    this.Size = Properties.Settings.Default.FormSize;
    this.Location = Properties.Settings.Default.FormPosition;
}

More references:

o.k.w
that all element ? Textbox1, Textbox2, Textbox3 ... ?
monkey_boys
Can to start using function AutoLoad();Can Exit using function SaveAll();?
monkey_boys
If you allow the users to personalize things like form control properties (e.g. dock, font etc), you can save them as well, but you'll have to code it manually for saving and loading. astander has provided a good reference for a more automated method using serialization which is another alternative. `AutoLoad` and `Exit` can be replaced with the form events `Form.Load` and `Form.Closing` like what Ryan Michela mentioned.
o.k.w
+4  A: 

Your question is a little unclear, but

If you require saving/loading of the Form Layout have a look at

Windows Forms User Settings in C#

If you require saving/loading an object/class have a look at

Load and save objects to XML using serialization

EDIT:

This will show you how to persist certain settings for form properties.

Save and Restore Setings of a .NET Form using XML

Also have a look at The Application Automation Layer - Using XML To Dynamically Generate GUI Elements--Forms And Controls

All these will guide you in the direction you need to go.

I think the main objective here is

  • Figute out when to save and when to load, and where to store/retrieve these settings.
  • Are you storing these settings per user? In a database, xml file?
  • Next you need to identify which properties you will be saving/restoring per control. Simple location/size settings might not cut it as controls will have various complexities (Button, Textbox, Gridview, ListView)
  • Now you need to figure out how to itterate ALL controls on the form. Buttons, Textboxes, Panels, Controls in Controls (controls in panels), and maybe even your User Controls. This can be done using recursion.
  • Now you need to decide on the structure of the xml file (if you opt to use xml). This should pretty much look like a tree structure, as you would look at the form, and its controls, and their controls, as a tree structure.
astander
i like xml object serialization but how to save and load ?
monkey_boys
A: 

The process of turning objects like forms into something that could be saved is called serialization. Unfortunately I don't think there's an out-of-box way to serialize forms in WinForm. I did find How to Clone/Serialize/Copy & Paste a Windows Forms Control, and since forms are also controls, you might be able to serialize the properties using the code.

eed3si9n
A: 

I have created one class to serialize any control's properties, you can add more properties what you require. http://www.bimbim.in/post/2010/09/20/Saving-Windows-Form-Controls-State-Using-Serialization-in-C-Sharp.aspx

bimbim.in