且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何自动保存和自动负载的WinForms C#中的所有属性?

更新时间:2023-02-16 14:32:33

你的问题是有点不清楚,但



如果您需要保存/在加载窗体布局看看



Windows窗体用户设置在C#



如果您需要保存/加载对象/类看看



加载和保存对象到XML使用序列



编辑:



这将告诉您如何为表单属性仍然存在一定的设置。



保存并恢复使用XML 一个.NET形式的Setings



也有看的应用程序自动化层 - 使用XML动态生成GUI元素 - 窗体和控件



所有这些都将引导你,你需要去的方向。



我觉得这里的主要目的是




  • 图出来的时候救,什么时候
    负载,以及在哪里存储/检索
    这些设置。

  • 您存储每
    用户的这些设置吗?在一个数据库?在一个xml文件?

  • 接下来,您需要确定哪些
    属性,你将是
    保存/每控制恢复。简单的
    位置/大小设置可能不会削减
    作为控制将有不同的
    复杂(按钮,文本框,
    的GridView,ListView控件)

  • 现在你需要弄清楚如何
    遍历窗体上的所有控件。
    按钮,文本框,面板控制的控制(在面板控制)

    也许甚至你的用户控件。这种
    可以使用递归来完成。

  • 现在,您需要在XML文件中的
    结构决定(如果你选择
    使用XML) 。这应该差不多
    看起来像一个树形结构,因为你
    会看的形式,它的
    控制,它们的控制,为
    树结构。


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

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

  • Figure 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? In a 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 iterate 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.