using System; using System.Windows.Forms; namespace FileWatcher { public partial class AutoSMTRXYSetting : Form { /// /// 设置参数解锁密码 /// private const string SettingPassword = "1234"; /// /// 是否已通过密码解锁 /// private bool UnLocked = false; public AutoSMTRXYSetting() { InitializeComponent(); StartPosition = FormStartPosition.CenterScreen; } private void AutoSMTRXYSetting_Load(object sender, EventArgs e) { ComPort.Text = GetCacheString("ComPort"); BaudRate.Text = GetCacheString("BaudRate"); Read.Text = GetCacheString("Read"); Write.Text = GetCacheString("Write"); Stop.Text = GetCacheString("Stop"); ToZero.Text = GetCacheString("ToZero"); UseScan.Text = GetCacheString("UseScan"); CountType.Text = GetCacheString("CountType"); if (BaudRate.Text == "") { BaudRate.Text = "9600"; } if (CountType.Text == "") { CountType.Text = "PLC"; } LockControls(); } private string GetCacheString(string ParamName) { object value = BaseUtil.GetCacheData(ParamName); return value == null ? "" : value.ToString(); } /// /// 锁定设置项(未输入密码前不可编辑) /// private void LockControls() { UnLocked = false; ComPort.Enabled = false; BaudRate.Enabled = false; Read.Enabled = false; Write.Enabled = false; Stop.Enabled = false; ToZero.Enabled = false; UseScan.Enabled = false; CountType.Enabled = false; Password.Text = ""; Confirm.Text = "确定"; } /// /// 密码正确后解锁设置项 /// private void UnLockControls() { UnLocked = true; ComPort.Enabled = true; BaudRate.Enabled = true; Read.Enabled = true; Write.Enabled = true; Stop.Enabled = true; ToZero.Enabled = true; UseScan.Enabled = true; CountType.Enabled = true; Password.Text = ""; Confirm.Text = "保存"; } private void Confirm_Click(object sender, EventArgs e) { if (!UnLocked) { //未解锁时先校验密码 if (Password.Text == SettingPassword) { UnLockControls(); } else { MessageBox.Show(this, "密码不对", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } return; } //解锁状态下点击则保存并关闭 BaseUtil.SetCacheData("ComPort", ComPort.Text); BaseUtil.SetCacheData("BaudRate", BaudRate.Text); BaseUtil.SetCacheData("Read", Read.Text); BaseUtil.SetCacheData("Write", Write.Text); BaseUtil.SetCacheData("Stop", Stop.Text); BaseUtil.SetCacheData("ToZero", ToZero.Text); BaseUtil.SetCacheData("UseScan", UseScan.Text); BaseUtil.SetCacheData("CountType", CountType.Text); MessageBox.Show(this, "保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } private void Cancel_Click(object sender, EventArgs e) { Close(); } } }