问题描述
关闭时如何自动保存所有属性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]);
}
}
}
}
的你的问题是有点不清楚,但的
如果您需要保存/在加载窗体布局看看
的
如果您需要保存/加载对象/类看看
编辑:
这将告诉您如何为表单属性仍然存在一定的设置。
的一个.NET形式的Setings
也有看的
所有这些都将引导你,你需要去的方向。
我觉得这里的主要目的是
- 图出来的时候救,什么时候
负载,以及在哪里存储/检索
这些设置。 - 您存储每
用户的这些设置吗?在一个数据库?在一个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 toload, and where to store/retrievethese settings.
- Are you storing these settings peruser? In a database? In a xml file?
- Next you need to identify whichproperties you will besaving/restoring per control. Simplelocation/size settings might not cutit as controls will have variouscomplexities (Button, TextBox,Gridview, ListView)
- Now you need to figure out how toiterate ALL controls on the form.Buttons, Textboxes, Panels, Controlsin Controls (controls in panels), andmaybe even your User Controls. Thiscan be done using recursion.
- Now you need to decide on thestructure of the xml file (if you optto use xml). This should pretty muchlook like a tree structure, as youwould look at the form, and itscontrols, and their controls, as atree structure.
这篇关于如何自动保存和自动负载的WinForms C#中的所有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!