Form1.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. namespace crusherScanner
  2. {
  3. public partial class Form1 : Form
  4. {
  5. private static ProgramFunctions? dataCore;
  6. private static int counter = 0;
  7. public Form1()
  8. {
  9. InitializeComponent();
  10. }
  11. private void Form1_Load(object sender, EventArgs e)
  12. {
  13. TextBox1.Enabled = false;
  14. toolStripStatusLabel5.Text = "";
  15. toolStripStatusLabel2.Text = "0";
  16. label8.Text = "";
  17. label7.Text = "";
  18. label6.Text = "";
  19. label5.Hide();
  20. label4.Hide();
  21. label3.Hide();
  22. label2.Hide();
  23. label1.Hide();
  24. WindowState = FormWindowState.Maximized;
  25. //TopMost = true;
  26. dataCore = new ProgramFunctions();
  27. TextBox1.Enabled = true;
  28. TextBox1.Focus();
  29. }
  30. private void TextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
  31. {
  32. Scan scanned = new();
  33. scanned.BufferCount = -1;
  34. counter = 2;
  35. scanned.barcode = TextBox1.Text.Trim();
  36. if (e.KeyValue == 13 && dataCore != null)
  37. {
  38. scanned = ProgramFunctions.StartChecks(scanned);
  39. if(scanned.sampleData.Job != null)
  40. {
  41. backgroundWorker1.RunWorkerAsync(scanned);
  42. }
  43. TextBox1.Text = "";
  44. if (scanned.sampleData.HammerOil)
  45. {
  46. label5.Text = $"Sample {scanned.barcode} contains Hammer Oil";
  47. BackColor = Color.Maroon;
  48. label5.Show();
  49. }
  50. else
  51. {
  52. label5.Hide();
  53. }
  54. if (scanned.sampleData.Manganese)
  55. {
  56. label4.Text = $"Sample {scanned.barcode} contains Manganese";
  57. BackColor = Color.Blue;
  58. label4.Show();
  59. }
  60. else
  61. {
  62. label4.Hide();
  63. }
  64. if (scanned.sampleData.Plastic)
  65. {
  66. label3.Text = $"Sample {scanned.barcode} contains Plastic";
  67. BackColor = Color.Orange;
  68. label3.Show();
  69. }
  70. else
  71. {
  72. label3.Hide();
  73. }
  74. if (scanned.sampleData.Pyrite)
  75. {
  76. label2.Text = $"Sample {scanned.barcode} contains Pyrite";
  77. BackColor = Color.Fuchsia;
  78. label2.Show();
  79. }
  80. else
  81. {
  82. label2.Hide();
  83. }
  84. if (!scanned.Contaminated)
  85. {
  86. label1.Text = scanned.message;
  87. label1.Show();
  88. }
  89. else
  90. {
  91. label1.Hide();
  92. }
  93. if (scanned.sampleData.Job != null)
  94. {
  95. label8.Text = $"Job in progress : {scanned.sampleData.Job}";
  96. }
  97. else
  98. {
  99. label8.Text = "";
  100. }
  101. if (scanned.BufferCount != -1)
  102. {
  103. toolStripStatusLabel2.Text = scanned.BufferCount.ToString();
  104. }
  105. }
  106. }
  107. private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
  108. {
  109. this.Close();
  110. }
  111. #region SettingsHandlers
  112. private void EditToolStripMenuItem_Click(object sender, EventArgs e)
  113. {
  114. SettingsDialog dlg = new();
  115. dlg.ShowDialog();
  116. dlg.Dispose();
  117. }
  118. private void PurgeToolStripMenuItem1_Click(object sender, EventArgs e)
  119. {
  120. DialogResult result = MessageBox.Show("Are you sure you would like to purge all setings?", "Clear settings.", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
  121. if(result == DialogResult.Yes)
  122. {
  123. Properties.Settings.Default.Reset();
  124. MessageBox.Show("Settings purged.", "Clear settings.");
  125. }
  126. }
  127. /// <summary>
  128. /// Import settings from JSON file.
  129. /// </summary>
  130. private void ImportToolStripMenuItem_Click(object sender, EventArgs e)
  131. {
  132. openFileDialog1.DefaultExt = "json";
  133. openFileDialog1.Filter = "Json files (*.json)|*.json";
  134. openFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
  135. openFileDialog1.ShowDialog();
  136. string configFile = openFileDialog1.FileName;
  137. try
  138. {
  139. if(configFile=="" && File.Exists(configFile) && dataCore != null)
  140. {
  141. ProgramFunctions.ImportConfigFile();
  142. }
  143. else if(File.Exists(configFile) && dataCore != null)
  144. {
  145. ProgramFunctions.ImportConfigFile(configFile);
  146. }
  147. }
  148. catch (Exception ex)
  149. {
  150. MessageBox.Show("An error occurred." + Environment.NewLine + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  151. }
  152. }
  153. /// <summary>
  154. /// Export settings to JSON file.
  155. /// </summary>
  156. private void ExportToolStripMenuItem_Click(object sender, EventArgs e)
  157. {
  158. saveFileDialog1.DefaultExt = "json";
  159. saveFileDialog1.Filter = "Json files (*.json)|*.json";
  160. saveFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
  161. saveFileDialog1.ShowDialog();
  162. string configFile = saveFileDialog1.FileName;
  163. try
  164. {
  165. if(configFile == "" && dataCore != null)
  166. {
  167. ProgramFunctions.ExportConfigFile();
  168. }
  169. else if (dataCore != null)
  170. {
  171. ProgramFunctions.ExportConfigFile(configFile);
  172. }
  173. }
  174. catch (Exception ex)
  175. {
  176. MessageBox.Show("An error occurred." + Environment.NewLine + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
  177. }
  178. }
  179. #endregion
  180. private void ReinitializeSettingsToolStripMenuItem_Click(object sender, EventArgs e)
  181. {
  182. Application.Restart();
  183. }
  184. private void Timer1_Tick(object sender, EventArgs e)
  185. {
  186. DateTime now = DateTime.Now;
  187. label6.Text = now.ToShortTimeString();
  188. label7.Text = now.ToShortDateString();
  189. if (counter <= 0)
  190. {
  191. BackColor = SystemColors.Control;
  192. counter = 2;
  193. }
  194. else if(BackColor.Name != "Control")
  195. {
  196. counter--;
  197. }
  198. }
  199. private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
  200. {
  201. if (e.Argument!=null)
  202. {
  203. WorkingDirControl.UpdateWorkingDir(e);
  204. }
  205. }
  206. private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
  207. {
  208. Scan scan;
  209. if (e.Result != null)
  210. {
  211. scan = (Scan)e.Result;
  212. toolStripProgressBar1.Maximum = scan.JobTotal;
  213. toolStripProgressBar1.Value = scan.JobCount;
  214. toolStripStatusLabel5.Text = (scan.JobTotal - (int)scan.JobCount).ToString();
  215. }
  216. }
  217. }
  218. }