2026-01-24 13:49:39 +08:00
|
|
|
|
using Sunny.UI;
|
|
|
|
|
|
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 材料热传导系数;
|
|
|
|
|
|
|
|
|
|
|
|
namespace 全自动水压检测仪
|
|
|
|
|
|
{
|
2026-01-24 14:07:01 +08:00
|
|
|
|
public partial class ScanImport : UIForm
|
2026-01-24 13:49:39 +08:00
|
|
|
|
{
|
2026-01-24 16:55:48 +08:00
|
|
|
|
private NormalTemperatureMode _normalTemperatureMode;
|
|
|
|
|
|
|
2026-01-24 13:49:39 +08:00
|
|
|
|
private ConductivityRepository _repository;
|
|
|
|
|
|
public ScanImport()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
_repository = new ConductivityRepository();
|
|
|
|
|
|
uiDataGridView1.AutoGenerateColumns = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-26 15:41:42 +08:00
|
|
|
|
private void SafeInvoke(Action action)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (action == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
// 检查窗体是否已释放/句柄是否创建
|
|
|
|
|
|
if (this.IsDisposed || !this.IsHandleCreated)
|
|
|
|
|
|
{
|
|
|
|
|
|
System.Diagnostics.Debug.WriteLine("ScanImport窗体已释放或句柄未创建,跳过UI操作");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果已经在UI线程,直接执行;否则Invoke
|
|
|
|
|
|
if (this.InvokeRequired)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.Invoke(action);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
action();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (ObjectDisposedException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
System.Diagnostics.Debug.WriteLine($"ScanImport UI操作时控件已释放: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (InvalidOperationException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
System.Diagnostics.Debug.WriteLine($"ScanImport UI操作异常: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-24 13:49:39 +08:00
|
|
|
|
private void save_Click(object sender, EventArgs e)
|
|
|
|
|
|
{
|
2026-01-26 15:41:42 +08:00
|
|
|
|
SafeInvoke(() =>
|
2026-01-24 13:49:39 +08:00
|
|
|
|
{
|
2026-01-26 15:41:42 +08:00
|
|
|
|
float diffpressure = 0, exit_temperature = 0, dwelltime = 0, temperature = 0;
|
|
|
|
|
|
float.TryParse(uiTextBox4?.Text ?? "", out diffpressure);
|
|
|
|
|
|
float.TryParse(uiTextBox9?.Text ?? "", out exit_temperature);
|
|
|
|
|
|
float.TryParse(uiTextBox5?.Text ?? "", out dwelltime);
|
|
|
|
|
|
float.TryParse(uiTextBox1?.Text ?? "", out temperature);
|
|
|
|
|
|
|
|
|
|
|
|
string temperatureMode = "";
|
|
|
|
|
|
if (uiCheckBox1?.Checked == true)
|
|
|
|
|
|
{
|
|
|
|
|
|
temperatureMode = "常温模式";
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (uiCheckBox2?.Checked == true)
|
|
|
|
|
|
{
|
|
|
|
|
|
temperatureMode = "高温模式";
|
|
|
|
|
|
}
|
2026-01-24 13:59:21 +08:00
|
|
|
|
|
2026-01-26 15:41:42 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
_repository.InsertScanItems(new ScanData
|
|
|
|
|
|
{
|
|
|
|
|
|
barcode = uiTextBox2?.Text ?? "",
|
|
|
|
|
|
diffpressure = diffpressure,
|
|
|
|
|
|
exit_temperature = exit_temperature,
|
|
|
|
|
|
dwelltime = dwelltime,
|
|
|
|
|
|
IsHighMode = temperature > 0,
|
|
|
|
|
|
temperature = temperature,
|
|
|
|
|
|
TemperatureMode = temperatureMode
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 重新加载数据
|
|
|
|
|
|
LoadData();
|
|
|
|
|
|
|
|
|
|
|
|
// 可选:清空输入框
|
|
|
|
|
|
ClearInputFields();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show($"保存失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
|
}
|
2026-01-24 13:49:39 +08:00
|
|
|
|
});
|
2026-01-26 15:41:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-26 17:05:33 +08:00
|
|
|
|
|
2026-01-26 15:41:42 +08:00
|
|
|
|
private void ClearInputFields()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (uiTextBox1 != null) uiTextBox1.Text = "";
|
|
|
|
|
|
if (uiTextBox2 != null) uiTextBox2.Text = "";
|
|
|
|
|
|
if (uiTextBox4 != null) uiTextBox4.Text = "";
|
|
|
|
|
|
if (uiTextBox5 != null) uiTextBox5.Text = "";
|
|
|
|
|
|
if (uiTextBox9 != null) uiTextBox9.Text = "";
|
|
|
|
|
|
if (uiCheckBox1 != null) uiCheckBox1.Checked = false;
|
|
|
|
|
|
if (uiCheckBox2 != null) uiCheckBox2.Checked = false;
|
2026-01-24 13:49:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ScanImport_Load(object sender, EventArgs e)
|
|
|
|
|
|
{
|
2026-01-26 15:41:42 +08:00
|
|
|
|
SafeInvoke(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadData();
|
|
|
|
|
|
});
|
2026-01-24 13:49:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-26 17:05:33 +08:00
|
|
|
|
|
|
|
|
|
|
// 设置行号的方法
|
|
|
|
|
|
private void SetRowNumbers()
|
|
|
|
|
|
{
|
|
|
|
|
|
SafeInvoke(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (uiDataGridView1 == null || uiDataGridView1.IsDisposed)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// 为每一行设置序号
|
|
|
|
|
|
for (int i = 0; i < uiDataGridView1.Rows.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
uiDataGridView1.Rows[i].Cells["SerialNumber"].Value = (i + 1).ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-01-24 13:49:39 +08:00
|
|
|
|
private void LoadData()
|
|
|
|
|
|
{
|
2026-01-26 15:41:42 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// 将数据查询操作移到后台线程,避免UI卡顿
|
|
|
|
|
|
System.Threading.Tasks.Task.Run(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var data = _repository.GetScanData();
|
2026-01-24 13:49:39 +08:00
|
|
|
|
|
2026-01-26 15:41:42 +08:00
|
|
|
|
// 安全更新DataGridView
|
|
|
|
|
|
SafeInvoke(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// 避免重复绑定导致的异常
|
|
|
|
|
|
if (uiDataGridView1 != null && !uiDataGridView1.IsDisposed)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 先解绑再重新绑定
|
|
|
|
|
|
uiDataGridView1.DataSource = null;
|
|
|
|
|
|
uiDataGridView1.DataSource = data;
|
2026-01-26 17:05:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (uiDataGridView1.Columns.Contains("Column1"))
|
|
|
|
|
|
{
|
|
|
|
|
|
uiDataGridView1.Columns["Column1"].Visible = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SetRowNumbers();
|
2026-01-26 15:41:42 +08:00
|
|
|
|
uiDataGridView1.Refresh();
|
2026-01-26 17:05:33 +08:00
|
|
|
|
|
2026-01-26 15:41:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
SafeInvoke(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show($"加载数据失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-01-24 13:49:39 +08:00
|
|
|
|
}
|
2026-01-24 16:55:48 +08:00
|
|
|
|
|
2026-01-26 15:41:42 +08:00
|
|
|
|
//private void SwitchWindow<T>(ref T windowInstance, Func<T> createFunc) where T : UIForm
|
|
|
|
|
|
//{
|
|
|
|
|
|
// if (windowInstance == null || windowInstance.IsDisposed)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// windowInstance = createFunc();
|
|
|
|
|
|
// windowInstance.FormClosed += (s, e) =>
|
|
|
|
|
|
// {
|
|
|
|
|
|
// this.Invoke(new Action(() =>
|
|
|
|
|
|
// {
|
|
|
|
|
|
// this.Show();
|
|
|
|
|
|
// this.Activate();
|
|
|
|
|
|
// }));
|
|
|
|
|
|
// };
|
|
|
|
|
|
// }
|
|
|
|
|
|
// else
|
|
|
|
|
|
// {
|
|
|
|
|
|
// windowInstance.Activate();
|
|
|
|
|
|
// return;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// this.Hide();
|
|
|
|
|
|
// windowInstance.Show();
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void SwitchWindow<T>(T windowInstance, Func<T> createFunc) where T : UIForm
|
2026-01-24 16:55:48 +08:00
|
|
|
|
{
|
2026-01-26 15:41:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 需要将 windowInstance 声明为局部变量
|
|
|
|
|
|
T localInstance = windowInstance;
|
|
|
|
|
|
|
|
|
|
|
|
// 复用窗口实例
|
|
|
|
|
|
if (localInstance == null || localInstance.IsDisposed)
|
2026-01-24 16:55:48 +08:00
|
|
|
|
{
|
2026-01-26 15:41:42 +08:00
|
|
|
|
localInstance = createFunc();
|
|
|
|
|
|
localInstance.FormClosed += (s, e) =>
|
2026-01-24 16:55:48 +08:00
|
|
|
|
{
|
2026-01-26 15:41:42 +08:00
|
|
|
|
SafeInvoke(() =>
|
2026-01-24 16:55:48 +08:00
|
|
|
|
{
|
|
|
|
|
|
this.Show();
|
|
|
|
|
|
this.Activate();
|
2026-01-26 15:41:42 +08:00
|
|
|
|
});
|
2026-01-24 16:55:48 +08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2026-01-26 15:41:42 +08:00
|
|
|
|
SafeInvoke(() => localInstance.Activate());
|
2026-01-24 16:55:48 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-26 15:41:42 +08:00
|
|
|
|
SafeInvoke(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
this.Hide();
|
|
|
|
|
|
localInstance.Show();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 如果需要更新外部引用,可以返回实例
|
|
|
|
|
|
// return localInstance;
|
|
|
|
|
|
}
|
2026-01-24 16:55:48 +08:00
|
|
|
|
private void uiButton1_Click(object sender, EventArgs e)
|
|
|
|
|
|
{
|
2026-01-26 15:41:42 +08:00
|
|
|
|
SwitchWindow(_normalTemperatureMode, () => new NormalTemperatureMode());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//清除
|
|
|
|
|
|
private void uiButton2_Click(object sender, EventArgs e)
|
|
|
|
|
|
{
|
2026-01-26 15:57:13 +08:00
|
|
|
|
var ids = new List<int>();
|
|
|
|
|
|
foreach (DataGridViewRow row in uiDataGridView1.SelectedRows)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (row.DataBoundItem is ScanData scanData && scanData.Id > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
ids.Add(scanData.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
_repository.DeleteScanItems(ids[0]);
|
|
|
|
|
|
LoadData();
|
2026-01-24 16:55:48 +08:00
|
|
|
|
}
|
2026-01-26 17:05:33 +08:00
|
|
|
|
|
|
|
|
|
|
private void uiDataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 当添加新行时更新序号
|
|
|
|
|
|
SetRowNumbers();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void uiDataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 当移除行时更新序号
|
|
|
|
|
|
SetRowNumbers();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void uiDataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 当移除行时更新序号
|
|
|
|
|
|
SetRowNumbers();
|
|
|
|
|
|
}
|
2026-01-24 13:49:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|