xml数据库自动保存全部控件textbox,numericalupdown,checkebox的内容,包括容器panel,groupbox,tabcontrol内的控件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Windows.Forms ;
namespace wenqizhi
{
public class config
{
public string[] name = new string[100];//wenqizhivolt
public string[] value = new string[100];//wenqizhivolt
}
static class wen
{
public static config Config = new config();
}
public static class readWriteConfigParam
{
public static void Save(Control.ControlCollection ct)
{
save_controls(ct);
string strFilePath = "data.xml";
XmlSerializer serializer = new XmlSerializer(typeof(config));
FileStream fs;
fs = new FileStream(strFilePath, FileMode.Create);
serializer.Serialize(fs, wen.Config);
fs.Close();
try
{ }
catch (Exception)
{
}
}
public static void Load(RichTextBox textBox, Control.ControlCollection ct)
{
string strFilePath = "data.xml";
if (!File.Exists (strFilePath ))
{
textBox.AppendText("\r\n"+strFilePath + "打开失败,没有文件\r\n");
return;
}
XmlSerializer serializer = new XmlSerializer(typeof(config));
FileStream fs;
fs = new FileStream(strFilePath, FileMode.Open);
wen.Config = (config)serializer.Deserialize(fs);
fs.Close();
show_config_to_ct(ct);
textBox.AppendText("\r\n" + strFilePath + "打开成功,参数文件读取到\r\n");
//try
//{ }
//catch (Exception)
//{
// textBox.AppendText("\r\n"+strFilePath + "打开失败,没有文件\r\n");
//}
}
public static void save_ct_to_config(Control.ControlCollection ct,ref int index)
{
foreach (Control item in ct)
{
if (item is GroupBox)
{
save_ct_to_config(item.Controls,ref index);
}
if (item is Panel)
{
save_ct_to_config(item.Controls, ref index);
}
if (item is TabControl)
{
save_ct_to_config(item.Controls, ref index);
}
if (item is CheckBox)
{
CheckBox c = item as CheckBox;
fillTable(c.Name, c.Checked.ToString(),ref index);
}
if (item is TextBox)
{
fillTable(item.Name, item.Text,ref index);
}
//if (item is Label)
//{
// fillTable(item.Name, item.Text,ref index);
//}
if (item is NumericUpDown)
{
NumericUpDown c = item as NumericUpDown;
fillTable(c.Name, c.Value.ToString(),ref index);
}
}
} //插入一些数据
static void save_controls(Control.ControlCollection ct)
{
int i = 0;
save_ct_to_config(ct,ref i);
}
// static int index = 0;
public static void fillTable(string name, string value,ref int i)
{
wen.Config.name[i] = name;
wen.Config.value[i] = value;
if (i < wen.Config.name.Length-1)
{
i++;
}
}
static string get_value_by_name(string n)
{
if (n=="")
{
return "";
}
for (int i = 0; i < wen.Config.name.Length ; i++)
{
string nn = wen.Config.name[i];
if (nn=="")
{
return "";
}
if (n==nn)
{
return wen.Config.value[i];
}
}
return "";
}
public static void show_config_to_ct(Control.ControlCollection ct)
{
foreach (Control item in ct)
{
if (item is GroupBox)
{
show_config_to_ct(item.Controls);
}
if (item is Panel)
{
show_config_to_ct(item.Controls);
}
if (item is TabControl)
{
show_config_to_ct(item.Controls);
}
if (item is TextBox)
{
item.Text = get_value_by_name(item.Name);
}
//if (item is Label)
//{
// item.Text = get_value_by_name(item.Name);
//}
if (item is CheckBox)
{
CheckBox tx = item as CheckBox;
string value = get_value_by_name(item.Name);
if (value != "")
{
bool b = Convert.ToBoolean(value);
tx.Checked = b;
}
}
if (item is NumericUpDown)
{
NumericUpDown tx = item as NumericUpDown;
string value = get_value_by_name(item.Name);
if (value != "")
{
decimal d = Convert.ToDecimal(value);
tx.Value = d;
}
}
}
}
}
}