上一张我们已经实现了TCP和串口页面的数据展示,和保存控件
我们这一章,实现如何去,控制保存。
一、控件展示
CheckBox TextBox Button label
Name:
checkSaveImage checkDelete txtSaveDays
txtSaveImagePath btnSelectImagePath
checksaveData
txtSaveDataPath btnSelectDataPath
btnSave
二、根据上回创建API来实现
private static TotalMessage instance = null;
private TotalMessage() { }
private static readonly object locker = new object();
public static TotalMessage GetInstance()
{
if (instance == null)
{
lock (locker)
{
instance = new TotalMessage();
}
}
return instance;
}
public string Configpath = Directory.GetCurrentDirectory() + @"\配置文件\Config.ini";
三、声明类
#region 图片数据路径属性
public string ImageSavePath { get; set; }
public string DataSavePath { get; set; }
#endregion
#region 保存数据-图片属性
public bool IsSaveImage { get; set; }
public bool IsSaveData { get; set; }
public bool IsDelete { get; set; }
public int SaveDay { get; set; }
public string PathSaveImage { get; set; }
public string PathSaveData { get; set; }
#endregion
public void LoadConfig()
{
ImageSavePath = IniAPI.GetPrivateProfileString("路径", "ImageSavePath", @"D:\Image", Configpath);
DataSavePath = IniAPI.GetPrivateProfileString("路径", "DataSavePath", @"D:\Data", Configpath);
}
public void WriteValues(string Section, string key, string value)
{
IniAPI.INIWriteValue(Configpath, Section, key, value);
}
四、窗体框架
1.在APP.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="九点标定项目.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<userSettings>
<九点标定项目.Properties.Settings>
<setting name="IsSaveImage" serializeAs="String">
<value>True</value>
</setting>
<setting name="IsSaveData" serializeAs="String">
<value>True</value>
</setting>
<setting name="IsDelete" serializeAs="String">
<value>True</value>
</setting>
<setting name="SaveDays" serializeAs="String">
<value>7</value>
</setting>
<setting name="LastDeleteData" serializeAs="String">
<value>2023-05-11</value>
</setting>
</九点标定项目.Properties.Settings>
</userSettings>
</configuration>
2. 窗体加载
private void FormSaveDataImage_Load(object sender, EventArgs e)
{
checkSaveImage.Checked = message.IsSaveImage ? true : false;
checksaveData.Checked = message.IsSaveData ? true : false;
checkDelete.Checked = message.IsDelete ? true : false;
txtSaveDays.Text=message.SaveDay.ToString();
txtSaveDataPath.Text = message.PathSaveData;
txtSaveImagePath.Text=message.PathSaveImage;
}
3.保存数据路径
private void btnSelectImagePath_Click(object sender, EventArgs e)
{
//创建一个FolderBrowserDialog对象
FolderBrowserDialog folder=new FolderBrowserDialog();
folder.SelectedPath = message.PathSaveImage;
if(folder.ShowDialog() == DialogResult.OK)
{
txtSaveDataPath.Text=folder.SelectedPath;
message.PathSaveImage = folder.SelectedPath;
}
}
4.保存图片路径
private void btnSelectDataPath_Click(object sender, EventArgs e)
{
FolderBrowserDialog fold=new FolderBrowserDialog();
fold.SelectedPath = message.PathSaveData;
if(fold.ShowDialog() == DialogResult.OK)
{
txtSaveDataPath.Text=fold.SelectedPath;
message.PathSaveData=fold.SelectedPath;
}
}
5.this.Controls
TotalMessage message = TotalMessage.GetInstance();
public FormSaveDataImage()
{
InitializeComponent();
message.IsSaveData=Settings.Default.IsSaveData;
message.IsSaveImage=Settings.Default.IsSaveImage;
message.IsDelete=Settings.Default.IsDelete;
message.SaveDay=Settings.Default.SaveDays;
message.PathSaveImage=TotalMessage.GetInstance().ImageSavePath;
message.PathSaveData=TotalMessage.GetInstance().DataSavePath;
}
6.保存
private void btnSave_Click(object sender, EventArgs e)
{
message.IsSaveImage=checkSaveImage.Checked;
message.IsSaveData=checksaveData.Checked;
message.IsDelete=checkDelete.Checked;
message.PathSaveData=txtSaveDataPath.Text;
message.PathSaveImage=txtSaveImagePath.Text;
message.SaveDay=Convert.ToInt32(txtSaveDays.Text.Trim());
TotalMessage.GetInstance().ImageSavePath=message.PathSaveImage;
TotalMessage.GetInstance().DataSavePath=message.PathSaveData;
Settings.Default.IsSaveData = message.IsSaveData;
Settings.Default.IsSaveImage = message.IsSaveImage;
Settings.Default.IsDelete = message.IsDelete;
Settings.Default.SaveDays=message.SaveDay;
DialogResult res=MessageBox.Show("确认保存设置","保存设置",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
if(res == DialogResult.Yes)
{
Settings.Default.Save();
message.WriteValues("路径","ImageSavePath",message.PathSaveImage);
message.WriteValues("路径","DataSavePath",message.PathSaveData);
MessageBox.Show("保存成功");
}
五、ALL代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 九点标定项目.Class;
using 九点标定项目.Properties;
namespace 九点标定项目
{
public partial class FormSaveDataImage : Form
{
TotalMessage message = TotalMessage.GetInstance();
public FormSaveDataImage()
{
InitializeComponent();
message.IsSaveData=Settings.Default.IsSaveData;
message.IsSaveImage=Settings.Default.IsSaveImage;
message.IsDelete=Settings.Default.IsDelete;
message.SaveDay=Settings.Default.SaveDays;
message.PathSaveImage=TotalMessage.GetInstance().ImageSavePath;
message.PathSaveData=TotalMessage.GetInstance().DataSavePath;
}
private void FormSaveDataImage_Load(object sender, EventArgs e)
{
checkSaveImage.Checked = message.IsSaveImage ? true : false;
checksaveData.Checked = message.IsSaveData ? true : false;
checkDelete.Checked = message.IsDelete ? true : false;
txtSaveDays.Text=message.SaveDay.ToString();
txtSaveDataPath.Text = message.PathSaveData;
txtSaveImagePath.Text=message.PathSaveImage;
}
private void btnSelectImagePath_Click(object sender, EventArgs e)
{
//创建一个FolderBrowserDialog对象
FolderBrowserDialog folder=new FolderBrowserDialog();
folder.SelectedPath = message.PathSaveImage;
if(folder.ShowDialog() == DialogResult.OK)
{
txtSaveDataPath.Text=folder.SelectedPath;
message.PathSaveImage = folder.SelectedPath;
}
}
private void btnSelectDataPath_Click(object sender, EventArgs e)
{
FolderBrowserDialog fold=new FolderBrowserDialog();
fold.SelectedPath = message.PathSaveData;
if(fold.ShowDialog() == DialogResult.OK)
{
txtSaveDataPath.Text=fold.SelectedPath;
message.PathSaveData=fold.SelectedPath;
}
}
private void btnSave_Click(object sender, EventArgs e)
{
message.IsSaveImage=checkSaveImage.Checked;
message.IsSaveData=checksaveData.Checked;
message.IsDelete=checkDelete.Checked;
message.PathSaveData=txtSaveDataPath.Text;
message.PathSaveImage=txtSaveImagePath.Text;
message.SaveDay=Convert.ToInt32(txtSaveDays.Text.Trim());
TotalMessage.GetInstance().ImageSavePath=message.PathSaveImage;
TotalMessage.GetInstance().DataSavePath=message.PathSaveData;
Settings.Default.IsSaveData = message.IsSaveData;
Settings.Default.IsSaveImage = message.IsSaveImage;
Settings.Default.IsDelete = message.IsDelete;
Settings.Default.SaveDays=message.SaveDay;
DialogResult res=MessageBox.Show("确认保存设置","保存设置",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
if(res == DialogResult.Yes)
{
Settings.Default.Save();
message.WriteValues("路径","ImageSavePath",message.PathSaveImage);
message.WriteValues("路径","DataSavePath",message.PathSaveData);
MessageBox.Show("保存成功");
}
}
}
}