BandBarodeDialog.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using static MainForm.SQLHelper;
  11. namespace MainForm.FaForm
  12. {
  13. public partial class BandBarodeDialog : Form
  14. {
  15. public string pcbCode { get; private set; }
  16. public string _CarrierBarcode { private get; set; }
  17. public string _ProductBarcode { private get; set; }
  18. private Timer _inputTimer;
  19. public BandBarodeDialog()
  20. {
  21. InitializeComponent();
  22. //置顶
  23. this.TopMost = true;
  24. // 设置窗口相对于父窗口居中
  25. this.StartPosition = FormStartPosition.CenterParent;
  26. this.Shown += MainForm_Shown;
  27. }
  28. private void BandBarodeDialog_Load(object sender, EventArgs e)
  29. {
  30. CarrierBarcode.Text = _CarrierBarcode;
  31. ProductBarcode.Text = _ProductBarcode;
  32. PCBBarcode.Text = string.Empty;
  33. // 初始化定时器
  34. _inputTimer = new Timer();
  35. _inputTimer.Interval = 500; // 500毫秒
  36. _inputTimer.Tick += InputTimer_Tick;
  37. // 绑定 TextChanged 事件
  38. PCBBarcode.TextChanged += PCBBarcode_TextChanged;
  39. }
  40. private void button1_Click(object sender, EventArgs e)
  41. {
  42. Close();
  43. }
  44. private void PCBBarcode_TextChanged(object sender, EventArgs e)
  45. {
  46. // 每次文本变化时重置定时器
  47. _inputTimer.Stop();
  48. _inputTimer.Start();
  49. }
  50. private void InputTimer_Tick(object sender, EventArgs e)
  51. {
  52. // 定时器触发,表示输入已经完成
  53. _inputTimer.Stop();
  54. // 处理条码
  55. string pcbcode = PCBBarcode.Text.Trim();
  56. if (!string.IsNullOrEmpty(pcbcode))
  57. {
  58. var Carrierdt = Db.Queryable<CarrierBind>()
  59. .Where(x => x.CarrierCode == _CarrierBarcode)
  60. .Where(x => x.PCBBarcode == pcbcode)
  61. .OrderByDescending(x => x.ID)
  62. .Take(1)
  63. .ToList();
  64. if (Carrierdt != null && Carrierdt.Count > 0)
  65. {
  66. closeWin();
  67. }
  68. else
  69. {
  70. ErrorLab.Text=$"校验失败:PCB码[{pcbcode}]与载具码[{_CarrierBarcode}]未绑定";
  71. }
  72. }
  73. }
  74. private void closeWin() {
  75. _CarrierBarcode = string.Empty;
  76. _ProductBarcode = string.Empty;
  77. this.DialogResult= DialogResult.OK;
  78. this.Close();
  79. }
  80. private void MainForm_Shown(object sender, EventArgs e)
  81. {
  82. // 将焦点设置到 textBox1
  83. PCBBarcode.Focus();
  84. }
  85. }
  86. }