Files
FullAutoWaterCheck/全自动水压检测仪/ScanImport.cs
2026-02-04 20:16:27 +08:00

322 lines
10 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
{
public partial class ScanImport : UIForm
{
private NormalTemperatureMode _normalTemperatureMode;
private ConductivityRepository _repository;
public ScanImport()
{
InitializeComponent();
_repository = new ConductivityRepository();
uiDataGridView1.AutoGenerateColumns = false;
}
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}");
}
}
private void save_Click(object sender, EventArgs e)
{
SafeInvoke(() =>
{
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 = "高温模式";
}
// 获取联络单号和件号
string contactNumber = uiTextBox2?.Text?.Trim() ?? "";
string itemNumber = uiTextBox11?.Text?.Trim() ?? "";
if (string.IsNullOrEmpty(contactNumber))
{
MessageBox.Show("请输入联络单号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(itemNumber))
{
MessageBox.Show("请输入件号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 组合条码用于兼容性(可选)
string barcode = $"{contactNumber}-{itemNumber}";
try
{
_repository.InsertScanItems(new ScanData
{
barcode = barcode,
ContactNumber = contactNumber,
ItemNumber = itemNumber,
diffpressure = diffpressure,
exit_temperature = exit_temperature,
dwelltime = dwelltime,
IsHighMode = temperature > 0,
temperature = temperature,
TemperatureMode = temperatureMode,
//新增
jh = string.Empty,
kzh = string.Empty,
lldh = string.Empty,
pressuresetting = 0,
quantity = 0,
standarderror = 0,
});
// 重新加载数据
LoadData();
// 可选:清空输入框
ClearInputFields();
}
catch (Exception ex)
{
MessageBox.Show($"保存失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
});
}
private void ClearInputFields()
{
if (uiTextBox1 != null) uiTextBox1.Text = "";
if (uiTextBox2 != null) uiTextBox2.Text = "";
if (uiTextBox11 != null) uiTextBox11.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;
}
private void ScanImport_Load(object sender, EventArgs e)
{
SafeInvoke(() =>
{
LoadData();
});
}
// 设置行号的方法
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();
}
});
}
private void LoadData()
{
try
{
// 将数据查询操作移到后台线程避免UI卡顿
System.Threading.Tasks.Task.Run(() =>
{
var data = _repository.GetScanData();
// 安全更新DataGridView
SafeInvoke(() =>
{
// 避免重复绑定导致的异常
if (uiDataGridView1 != null && !uiDataGridView1.IsDisposed)
{
// 先解绑再重新绑定
uiDataGridView1.DataSource = null;
uiDataGridView1.DataSource = data;
if (uiDataGridView1.Columns.Contains("Column1"))
{
uiDataGridView1.Columns["Column1"].Visible = false;
}
SetRowNumbers();
uiDataGridView1.Refresh();
}
});
});
}
catch (Exception ex)
{
SafeInvoke(() =>
{
MessageBox.Show($"加载数据失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
});
}
}
//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
{
// 需要将 windowInstance 声明为局部变量
T localInstance = windowInstance;
// 复用窗口实例
if (localInstance == null || localInstance.IsDisposed)
{
localInstance = createFunc();
localInstance.FormClosed += (s, e) =>
{
SafeInvoke(() =>
{
this.Show();
this.Activate();
});
};
}
else
{
SafeInvoke(() => localInstance.Activate());
return;
}
SafeInvoke(() =>
{
this.Hide();
localInstance.Show();
});
// 如果需要更新外部引用,可以返回实例
// return localInstance;
}
private void uiButton1_Click(object sender, EventArgs e)
{
SwitchWindow(_normalTemperatureMode, () => new NormalTemperatureMode());
}
//清除
private void uiButton2_Click(object sender, EventArgs e)
{
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();
}
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();
}
private void uiButton3_Click(object sender, EventArgs e)
{
Application.Restart();
// 关闭当前窗口
Environment.Exit(0);
}
}
}