winform c#遍历全部控件自动保存读取参数,节约时间
·
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HandlerUI
{
internal class TagAllSetGet
{
static SqliteUse sqliteUse;
static Dictionary<string, string> AllParam = new Dictionary<string, string>();//全部参数
/// <summary>
/// 初始化数据库,读取数据库的全部数据到内存,否-是使用0-1代替,耗时200毫秒
/// </summary>
public static void Init()
{
if (sqliteUse == null)
{
sqliteUse = new SqliteUse();
}
AllParam = sqliteUse.GetAll();
}
/// <summary>
/// /快速读取一个double参数,从内存读取,不从数据库直接读取,否-是使用0-1代替
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static double GetDouble(string name)
{
string s1 = GetString(name);
double d1 = Convert.ToDouble(s1);
return d1;
}
/// <summary>
/// /设置一个double参数,保存到数据库,大约耗时20毫秒,否-是使用0-1代替
/// </summary>
/// <param name="name"></param>
/// <param name="d1"></param>
public static void SetDouble(string name, double d1)
{
SetString(name, d1.ToString());
}
/// <summary>
/// ///快速读取一个参数,从内存读取,不从数据库直接读取,否-是使用0-1代替
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string GetString(string name)
{
if (sqliteUse == null)
{
return "";
}
if (AllParam.Keys.Contains(name) == false)
{
return "";
}
string value = AllParam[name];
return value;
}
/// <summary>
/// 设置一个参数,保存到数据库,大约耗时20毫秒,否-是使用0-1代替
/// </summary>
public static void SetString(string name, string value)
{
if (sqliteUse == null)
{
return;
}
if (AllParam.Keys.Contains(name) == false)
{
AllParam.Add(name, value);
}
else
{
AllParam[name] = value;
}
sqliteUse.Set(name, value);
}
/// <summary>
/// 保存界面到数据库,30个参数耗时22毫秒
/// </summary>
/// <param name="control"></param>
public static void SaveSqlite(Control control)
{
if (sqliteUse == null)
{
return;
}
Dictionary<string, string> strings3 = GetControlsValues(control);
SQLiteTransaction sQLiteTransaction = sqliteUse.m_dbConnection.BeginTransaction();//批量写入开始
foreach (var item in strings3)
{
if (AllParam.Keys.Contains(item.Key) == false)
{
AllParam.Add(item.Key, item.Value);
}
sqliteUse.Set(item.Key, item.Value);
}
sQLiteTransaction.Commit();//批量写入
string s1 = "";
foreach (var item in strings3)
{
s1 += item.Key + "\r\n";
}
File.WriteAllText("界面参数.txt", s1);
}
/// <summary>
/// 读取数据库到界面,耗时几毫秒
/// </summary>
/// <param name="control"></param>
public static void ReadSqlite(Control control)
{
if (sqliteUse == null)
{
return;
}
Dictionary<string, string> strings3 = new Dictionary<string, string>();
strings3 = sqliteUse.GetAll();
SetControlsValues(control, strings3);
}
/// <summary>
/// 保存界面到csv文件
/// </summary>
/// <param name="control"></param>
/// <param name="name"></param>
static void SaveCsv(Control control, string name)
{
Dictionary<string, string> strings3 = GetControlsValues(control);
string s1 = "";
foreach (var item in strings3)
{
s1 += item.Key + "," + item.Value + "\r\n";
}
File.WriteAllText(name, s1);
}
/// <summary>
/// 读取csv文件到界面
/// </summary>
/// <param name="control"></param>
/// <param name="name"></param>
static void ReadCsv(Control control, string name)
{
Dictionary<string, string> strings3 = new Dictionary<string, string>();
if (File.Exists(name) == false)
{
return;
}
string[] strings = File.ReadAllLines(name);
foreach (var item in strings)
{
string[] s1 = item.Split(',');
string key = s1[0];
string value = s1[1];
strings3.Add(key, value);
}
SetControlsValues(control, strings3);
}
/// <summary>
/// /读取控件数值
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
static Dictionary<string, string> GetControlsValues(Control control)
{
Dictionary<string, Control> strings2 = new Dictionary<string, Control>();
Dictionary<string, string> strings3 = new Dictionary<string, string>();
GetAllTags(control, ref strings2);
foreach (var item2 in strings2)
{
string s5 = item2.Key + ",";
Control item = item2.Value;
string s6 = "";
if (item is TextBox)
{
bool b2 = true;
if (item.Tag != null)
{
if (item.Tag.ToString().Length != 0)
{
if (item.Tag.ToString()[0] == 's')
{
b2 = false;
}
}
}
bool b1 = double.TryParse(item.Text, out double d1);
if (b1 == false && b2 == true)
{
ToolTip toolTip = new ToolTip();
toolTip.Show("需要输入数字", item, 3000);
}
else
{
s6 = item.Text;
}
}
else if (item is ComboBox)
{
s6 = item.Text;
}
else if (item is CheckBox)
{
if ((item as CheckBox).Checked)
{
s6 = "1";
}
else
{
s6 = "0";
}
}
else if (item is RadioButton)
{
if ((item as RadioButton).Checked)
{
s6 = "1";
}
else
{
s6 = "0";
}
}
else
{
continue;
}
s5 += s6;
strings3.Add(item2.Key, s6);
//Console.WriteLine(s5);
}
return strings3;
}
/// <summary>
/// 设置控件数值
/// </summary>
/// <param name="control"></param>
/// <param name="strings3"></param>
static void SetControlsValues(Control control, Dictionary<string, string> strings3)
{
Dictionary<string, Control> strings2 = new Dictionary<string, Control>();
GetAllTags(control, ref strings2);
foreach (var item2 in strings2)
{
Control item = item2.Value;
//if (item.Tag != null)
{
//ToolTip toolTip = new ToolTip();
//toolTip.SetToolTip(item, item2.Key.ToString());
ContextMenuStrip toolStrip = new ContextMenuStrip();
toolStrip.Items.Clear();
toolStrip.Items.Add(item2.Key);
toolStrip.Items[0].Click += TagAllSetGet_Click;
item.ContextMenuStrip = toolStrip;
}
if (strings3.Keys.Contains(item2.Key) == false)
{
if (item is TextBox)
{
if (item.Text == "")
{
item.Text = "0";
}
}
continue;
}
string value = strings3[item2.Key].ToString();
if (item is TextBox)
{
if (value == "")
{
value = "0";
}
item.Text = value;
}
else if (item is ComboBox)
{
item.Text = value;
}
else if (item is CheckBox)
{
if (value.ToUpper() == "1")
{
(item as CheckBox).Checked = true;
}
else
{
(item as CheckBox).Checked = false;
}
}
else if (item is RadioButton)
{
if (value.ToUpper() == "1")
{
(item as RadioButton).Checked = true;
}
else
{
(item as RadioButton).Checked = false;
}
}
else if (item is TrackBar)
{
double.TryParse(value, out double d1);
if (d1 < (item as TrackBar).Minimum)
{
d1 = (item as TrackBar).Minimum;
}
if (d1 > (item as TrackBar).Maximum)
{
d1 = (item as TrackBar).Maximum;
}
(item as TrackBar).Value = (int)d1;
}
else
{
continue;
}
}
}
private static void TagAllSetGet_Click(object sender, EventArgs e)
{
Clipboard.SetText(((sender as ToolStripMenuItem)).Text);
}
/// <summary>
/// /获取全部控件和名称
/// </summary>
/// <param name="control"></param>
/// <param name="strings2"></param>
static void GetAllTags(Control control, ref Dictionary<string, Control> strings2)
{
foreach (Control item in control.Controls)
{
if (item.Controls.Count != 0)
{
GetAllTags(item, ref strings2);
}
Control control1 = item;
string s1 = "";
while (true)
{
control1 = control1.Parent;
if (control1 == null)
{
break;
}
string s2 = "";
if (control1 is UserControl == false && control1 is Form == false)
{
continue;
}
//else if (control1 is GroupBox)
//{
// s2 = control1.Text.ToString();
//}
else
{
if (control1.Tag != null)
{
s2 = control1.Tag.ToString();
}
}
s1 = s2 + "_" + s1;
}
string s3 = "";
if (item.Tag != null)
{
s3 = item.Tag.ToString();
}
else
{
s3 = "";
}
s1 += "" + s3;
if (strings2.ContainsKey(s1))
{
continue;
}
strings2.Add(s1, item);
}
}
}
}
更多推荐


所有评论(0)