Form2.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using LabelManager2;
  2. using System;
  3. using System.Drawing.Printing;
  4. using System.Windows.Forms;
  5. namespace MaterialPrint
  6. {
  7. public partial class Form2 : Form
  8. {
  9. ApplicationClass lbl;
  10. Document doc;
  11. DataHelper dh = new DataHelper();
  12. public Form2()
  13. {
  14. InitializeComponent();
  15. StartPosition = FormStartPosition.CenterScreen;
  16. }
  17. private void Print_Click(object sender, EventArgs e)
  18. {
  19. try
  20. {
  21. //根据箱数和箱内容量计算打印数
  22. Num.Text = (int.Parse(OutBoxNum.Text) * int.Parse(InnerBoxNum.Text)).ToString();
  23. //外箱流水
  24. int OutBoxSerial = int.Parse(OutBoxCode.Text.Substring(OutBoxCode.Text.Length - 6, 6));
  25. //外箱前缀
  26. string OutBoxPrefix = OutBoxCode.Text.Substring(0, 8);
  27. //内箱数量
  28. int InnerBox = int.Parse(InnerBoxNum.Text);
  29. //打印的最后一个流水
  30. int LastNum = (int.Parse(Rn.Text) + int.Parse(Num.Text));
  31. //打印的总数
  32. int PrintNum = int.Parse(Num.Text);
  33. //当前箱的打印数量
  34. int BoxCount = 0;
  35. for (int i = int.Parse(Rn.Text); i < LastNum; i = i + 8)
  36. {
  37. int temp = i;
  38. for (int j = 0; j < ((LastNum - i) / 8 > 0 ? 8 : (LastNum - i)); j++)
  39. {
  40. BoxCount = BoxCount + 1;
  41. string outboxbarcode = OutBoxPrefix + OutBoxSerial;
  42. switch (doc.Variables.FormVariables.Item(j + 1).Name)
  43. {
  44. case "RN1":
  45. doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp).ToString()));
  46. dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert");
  47. break;
  48. case "RN2":
  49. doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 1).ToString()));
  50. dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert");
  51. break;
  52. case "RN3":
  53. doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 2).ToString()));
  54. dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert");
  55. break;
  56. case "RN4":
  57. doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 3).ToString()));
  58. dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert");
  59. break;
  60. case "RN5":
  61. doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 4).ToString()));
  62. dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert");
  63. break;
  64. case "RN6":
  65. doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 5).ToString()));
  66. dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert");
  67. break;
  68. case "RN7":
  69. doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 6).ToString()));
  70. dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert");
  71. break;
  72. case "RN8":
  73. doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 7).ToString()));
  74. dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert");
  75. break;
  76. default:
  77. break;
  78. }
  79. if (BoxCount % InnerBox == 0)
  80. {
  81. BoxCount = 0;
  82. OutBoxSerial = OutBoxSerial + 1;
  83. }
  84. }
  85. //doc.Printer.SwitchTo(PrinterList.Text);
  86. //doc.PrintDocument(1);
  87. }
  88. }
  89. catch (Exception ex)
  90. {
  91. MessageBox.Show(ex.Message);
  92. }
  93. }
  94. private static string lpad(int length, string number)
  95. {
  96. while (number.Length < length)
  97. {
  98. number = "0" + number;
  99. }
  100. number = number.Substring(number.Length - length, length);
  101. return number;
  102. }
  103. private void Form1_Load(object sender, EventArgs e)
  104. {
  105. Console.WriteLine("4181231".Substring(0, 3));
  106. return;
  107. try
  108. {
  109. lbl = new ApplicationClass();
  110. doc = lbl.Documents.Open(System.Windows.Forms.Application.StartupPath + @"\SN.lab");
  111. PrintDocument print = new PrintDocument();
  112. string sDefault = print.PrinterSettings.PrinterName;//默认打印机名
  113. foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
  114. {
  115. PrinterList.Items.Add(sPrint);
  116. if (sPrint == sDefault)
  117. PrinterList.SelectedIndex = PrinterList.Items.IndexOf(sPrint);
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. MessageBox.Show(ex.Message);
  123. }
  124. }
  125. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  126. {
  127. System.Diagnostics.Process[] pro = System.Diagnostics.Process.GetProcessesByName("lppa");
  128. for (int i = 0; i < pro.Length; i++)
  129. {
  130. pro[i].Kill();
  131. }
  132. }
  133. }
  134. }