如何在关闭时自动保存 winforms 的所有属性并在加载时自动加载 winforms 的所有属性? 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]);
}
}
}
}
最佳答案
你的问题有点不清楚,但是
如果您需要保存/加载表单布局,请查看
Windows Forms User Settings in C#
如果您需要保存/加载对象/类,请查看
Load and save objects to XML using serialization
编辑:
这将向您展示如何保留表单属性的某些设置。
Save and Restore Setings of a .NET Form using XML
所有这些都将引导你朝着你需要去的方向前进。
我认为这里的主要目标是
- 确定何时保存以及何时保存 加载,以及存储/检索的位置 这些设置。
- 您是否将这些设置存储为 用户?在数据库中?在 xml 文件中?
- 接下来您需要确定哪个 你将拥有的属性 每个控件保存/恢复。简单的 位置/大小设置可能无法剪切 它作为控件将有各种 复杂性(按钮、文本框、 GridView 、 ListView )
- 现在你需要弄清楚如何 迭代表单上的所有控件。 按钮、文本框、面板、控件 在控件中(面板中的控件),以及 甚至你的用户控件。这 可以使用递归完成。
- 现在你需要决定 xml文件的结构(如果你选择 使用 xml)。这个应该差不多 看起来像树结构,因为你 会看表格,它的 控件,以及它们的控件,作为 树结构。
关于c# - 如何自动保存和自动加载 winforms C# 中的所有属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2076259/