如何自动保存和自动负载的WinForms C#中的所有属性? [英] How to auto save and auto load all properties in winforms C#?

查看:115
本文介绍了如何自动保存和自动负载的WinForms C#中的所有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关闭时

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



 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Text;使用System.Windows.Forms的
;
:使用System.IO;
命名空间SControl
{
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent();
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
的for(int i = 0; I< Controls.Count;我++)
{
System.Xml.Serialization.XmlSerializer X =新System.Xml.Serialization.XmlSerializer(typeof运算(控制[I]));
流流= File.Open(的test.xml,FileMode.Create);
x.Serialize(流控制[I]);
}
}
}
}


解决方案

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



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



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.

这篇关于如何自动保存和自动负载的WinForms C#中的所有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆