/// /// Copyright (c) 2020-2021 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using log4net; using NHibernate; #if MANAGED using Oracle.ManagedDataAccess.Client; #else using Oracle.DataAccess.Client; #endif using Common; using TBF.Rig.Sequences; using TBF.Resources; namespace TBF.Rig.DataEntry.S620 { public partial class FormForMechanicalMeters : Form, ICycleBeginOrEndForm, GenericDevices.IHasCompleted { private static readonly ILog log = LogManager.GetLogger(typeof(FormForMechanicalMeters)); /// Number of water meters IList waterMeters; readonly int wmsCount; readonly int maxWMsCount; readonly char closeButton; readonly bool atTheEnd; readonly bool withProductionTracing; /// To be retrieved after the form is closed string purchaseOrder; public string PurchaseOrder { get { return purchaseOrder; } set { purchaseOrder = value; } } string[] bodyNrText; public string[] BodyNrText { get { return bodyNrText; } set { bodyNrText = value; } } int[] assignedSerialNr; public int[] AssignedSerialNr { get { return assignedSerialNr; } set { assignedSerialNr = value; } } string[] completeSerialNr; public string[] CompleteSerialNr { get { return completeSerialNr; } set { completeSerialNr = value; } } bool[] disabled; public bool[] Disabled { get { return disabled; } set { disabled = value; } } string prefix; /// S/N prefix - Loaded from Oracle DB table VT_AUFTRAG_PD public string Prefix { get { return prefix; } set { prefix = value; } } string suffix; /// S/N suffix - Loaded from Oracle DB table VT_AUFTRAG_PD public string Suffix { get { return suffix; } set { suffix = value; } } /// /// Production order related /// /// Loaded from Oracle DB table VT_AUFTRAG_PD int firstSN; /// First serial number int snDigits; /// Number of digits of the middle part of the serial number int pcsCount; /// Pieces count IList foundMeters; IList foundSNs; IList assignedSNs; /// Entered manually int snRangeFirst; /// 0 = invalid int snRangeLast; /// 0 = invalid /// /// Production tracing /// ISession dbSession; /// /// UI /// Color oriBackColor; Rectangle rect; int textBoxesCount; /// Number of text boxes for serial numbers Label[] labels; TextBox[] bodyNrBoxes; TextBox[] serialNrBoxes; CheckBox[] checkBoxes; int nextWaterMeterIx; bool loadedOnce; /// Set when this form loads for the first time public bool Completed { get { return completed; } } bool completed; /// Set when this form closes /// Parameterless constructor for common functionality public FormForMechanicalMeters() { InitializeComponent(); ControlBox = false; oriBackColor = BackColor; ManageCheckGroupBox(orderCheckBox, orderGroupBox); firstSN = 0; snDigits = 0; pcsCount = 0; prefix = null; suffix = null; foundMeters = new List(); foundSNs = new List(); assignedSNs = new List(); snRangeFirst = 0; snRangeLast = 0; textBoxesCount = 40; labels = new Label[] { label1, label2, label3, label4, label5, label6, label7, label8, label9, label10, label11, label12, label13, label14, label15, label16, label17, label18, label19, label20, label21, label22, label23, label24, label25, label26, label27, label28, label29, label30, label31, label32, label33, label34, label35, label36, label37, label38, label39, label40, }; bodyNrBoxes = new TextBox[] { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9, textBox10, textBox11, textBox12, textBox13, textBox14, textBox15, textBox16, textBox17, textBox18, textBox19, textBox20, textBox21, textBox22, textBox23, textBox24, textBox25, textBox26, textBox27, textBox28, textBox29, textBox30, textBox31, textBox32, textBox33, textBox34, textBox35, textBox36, textBox37, textBox38, textBox39, textBox40, }; serialNrBoxes = new TextBox[] { textBox41, textBox42, textBox43, textBox44, textBox45, textBox46, textBox47, textBox48, textBox49, textBox50, textBox51, textBox52, textBox53, textBox54, textBox55, textBox56, textBox57, textBox58, textBox59, textBox60, textBox61, textBox62, textBox63, textBox64, textBox65, textBox66, textBox67, textBox68, textBox69, textBox70, textBox71, textBox72, textBox73, textBox74, textBox75, textBox76, textBox77, textBox78, textBox79, textBox80, }; checkBoxes = new CheckBox[] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8, checkBox9, checkBox10, checkBox11, checkBox12, checkBox13, checkBox14, checkBox15, checkBox16, checkBox17, checkBox18, checkBox19, checkBox20, checkBox21, checkBox22, checkBox23, checkBox24, checkBox25, checkBox26, checkBox27, checkBox28, checkBox29, checkBox30, checkBox31, checkBox32, checkBox33, checkBox34, checkBox35, checkBox36, checkBox37, checkBox38, checkBox39, checkBox40, }; System.Diagnostics.Debug.Assert(labels.Length == textBoxesCount); System.Diagnostics.Debug.Assert(bodyNrBoxes.Length == textBoxesCount); System.Diagnostics.Debug.Assert(serialNrBoxes.Length == textBoxesCount); System.Diagnostics.Debug.Assert(checkBoxes.Length == textBoxesCount); rect = new Rectangle(orderGroupBox.Left, orderGroupBox.Top + orderGroupBox.Height + 10, orderGroupBox.Width, 30); nextWaterMeterIx = 0; loadedOnce = false; completed = false; ColorFlashHandler += delegate(object sender, ColorFlashEventArgs args) { if (InvokeRequired) { Invoke(new EventHandler(OnColorFlash), sender, args); } else OnColorFlash(sender, args); }; StartForceCloseHandler(); } #region Forced close handling public void StartForceCloseHandler() { UiBridge.Bridge.CloseModelessFormHandler += delegate(object sender, EventArgs args) { if (InvokeRequired) { Invoke(new EventHandler(OnForceClose), sender, args); } else OnForceClose(sender, args); }; } private void OnForceClose(object sender, EventArgs args) { DialogResult = DialogResult.Cancel; Close(); } #endregion /// /// Constructor /// /// Number of text boxes for serial numbers public FormForMechanicalMeters(IList waterMeters, int maxWMsCount, char closeButton, bool atTheEnd, bool withProductionTracing = false) : this() { this.waterMeters = waterMeters; this.maxWMsCount = maxWMsCount; this.wmsCount = Math.Min(waterMeters.Count, maxWMsCount); this.closeButton = closeButton; this.atTheEnd = atTheEnd; this.withProductionTracing = withProductionTracing; bodyNrText = new string[waterMeters.Count]; assignedSerialNr = new int[waterMeters.Count]; completeSerialNr = new string[waterMeters.Count]; disabled = new bool[waterMeters.Count]; /// Disable controls that exceed the number of water meters in case textBoxesCount > wmsCount for (int i = wmsCount; i < textBoxesCount; i++) { labels[i].Visible = bodyNrBoxes[i].Visible = serialNrBoxes[i].Visible = checkBoxes[i].Visible = false; } /// Disable water meters that exceed the maximum number of water meters for (int i = wmsCount; i < waterMeters.Count; i++) { if (waterMeters[i] != null) disabled[i] = true; } textBoxesCount = Math.Min(textBoxesCount, wmsCount); BarcodeReceivedHandler += delegate(object sndr, BarcodeReceivedEventArgs args) { if (InvokeRequired) { Invoke(new EventHandler(BarcodeReceived), sndr, args); } else { BarcodeReceived(sndr, args); } }; FocusPressedHandler += delegate(object sndr, FocusPressedEventArgs args) { if (InvokeRequired) { Invoke(new EventHandler(FocusPressed), sndr, args); } else { FocusPressed(sndr, args); } }; } private void CycleBeginningForm_Load(object sender, EventArgs e) { Localize(); /// P/O and serial number assignment PrepareOrderAndSNRangeCombos(); /// TextBox-es for (int i = 0; i < textBoxesCount; i++) { if (waterMeters[i].Disabled) { bodyNrBoxes[i].Enabled = serialNrBoxes[i].Enabled = checkBoxes[i].Enabled = false; disabled[i] = true; checkBoxes[i].Checked = false; } else if (atTheEnd) { /// At the end disabled[i] = false; bodyNrBoxes[i].Text = bodyNrText[i] = waterMeters[i].SerialNr; checkBoxes[i].Checked = true; bodyNrBoxes[i].Enabled = false; /// Do not allow body number change serialNrBoxes[i].Enabled = false; /// Do not allow serial number change from UI checkBoxes[i].Enabled = true; /// Allow 'disabled' check box change /// Highlight failed water meters if (!waterMeters[i].PassedFromTests()) { bodyNrBoxes[i].BackColor = System.Drawing.Color.Pink; serialNrBoxes[i].BackColor = System.Drawing.Color.Pink; } } else { /// At the beginning bodyNrBoxes[i].Enabled = true; serialNrBoxes[i].Enabled = false; checkBoxes[i].Checked = false; } } #if DEBUG reloadButton.Enabled = true; #else if (ProcessData.OracleDB != null && ProcessData.OracleDB.SelectedDB != null) { reloadButton.Enabled = true; } #endif if (atTheEnd) { ProcessPO(); UpdateSNFromTo(); AssignSerialNumbers(); } loadedOnce = true; } void Localize() { Text = Strings.Data; orderCheckBox.Text = "Výrobná zakázka a sériové čísla"; firstSNLabel.Text = "prvé s/n"; snDigitsLabel.Text = "číslic"; piecesCountLabel.Text = "kusov"; reloadButton.Text = "@"; fromLabel.Text = "od"; toLabel.Text = "do"; serialNumbersGroupBox.Text = "Sériové čísla"; bodyLabel.Text = bodyLabel2.Text = "Číslo púzdra"; snLabel.Text = snLabel2.Text = "Sériové číslo"; okButton.Text = Strings.OkBtnText; //clearButton.Text = Strings.ClearBtnText; } void AssignSerialNumbers() { assignedSNs.Clear(); for (int i = 0; i < textBoxesCount; i++) { if (waterMeters[i].PassedFromTests() && !string.IsNullOrEmpty(bodyNrBoxes[i].Text) && checkBoxes[i].Checked) { int nextSN = GetNextSerialNr(); serialNrBoxes[i].Text = (nextSN > 0) ? nextSN.ToString() : string.Empty; } else { serialNrBoxes[i].Text = string.Empty; } } Invalidate(rect); } private void FormForMechanicalMeters_Shown(object sender, EventArgs e) { nextWaterMeterIx = 0; FocusToNextField(); } void FocusToNextField() { (atTheEnd ? serialNrBoxes[nextWaterMeterIx] : bodyNrBoxes[nextWaterMeterIx]).Focus(); } void ClearAllTextBoxes() { foreach (var tb in bodyNrBoxes) { tb.Text = string.Empty; } foreach (var tb in serialNrBoxes) { tb.Text = string.Empty; } nextWaterMeterIx = 0; FocusToNextField(); } /// /// Load Purchase order combo box items from the LocalSettings PurchaseOrderHistory array /// /// Puchase order ComboBox void PrepareOrderAndSNRangeCombos() { LocalSettings LS = Program.LocalSettings; /// Purchase order for (int i = 0; i < LS.PurchaseOrderHistoryCount; i++) orderCB.Items.Add(LS.PurchaseOrderHistory[i]); if (orderCB.Items.Count > 0) orderCB.Text = orderCB.Items[0].ToString(); /// First S/N in selected range for (int i = 0; i < LS.FirstSNHistoryCount; i++) firstSNRangeCB.Items.Add(LS.FirstSNHistory[i]); if (firstSNRangeCB.Items.Count > 0) firstSNRangeCB.Text = firstSNRangeCB.Items[0].ToString(); /// Last S/N in selected range for (int i = 0; i < LS.LastSNHistoryCount; i++) lastSNRangeCB.Items.Add(LS.LastSNHistory[i]); if (lastSNRangeCB.Items.Count > 0) lastSNRangeCB.Text = lastSNRangeCB.Items[0].ToString(); } private void okButton_Click(object sender, EventArgs e) { if (CheckForNonUniqueness(atTheEnd ? serialNrBoxes : bodyNrBoxes, textBoxesCount)) { return; } /// Read UI data and store them to 'interface' variables int itmp; for (int i = 0; i < textBoxesCount; i++) { bodyNrText[i] = bodyNrBoxes[i].Text; assignedSerialNr[i] = int.TryParse(serialNrBoxes[i].Text, out itmp) ? itmp : 0; completeSerialNr[i] = string.Format("{0}{1}{2}", (prefix != null) ? prefix : string.Empty, assignedSerialNr[i].ToString(string.Format("D{0}", Math.Max(snDigits, 1))), (suffix != null) ? suffix : string.Empty); disabled[i] = !checkBoxes[i].Checked; } purchaseOrder = orderCB.Text; if (withProductionTracing) { SaveProductionTracingRecords(); } completed = true; Close(); } private void SaveProductionTracingRecords() { /// Do nothing } /// /// Check for uniqueness of test in an array of TextBox-es /// /// Array of TextBox-es /// Number of text boxes at the beginning of the array to check /// true when there are at leas two equal non-empty tests bool CheckForNonUniqueness(TextBox[] textBoxes, int count) { for (int ix2 = 1; ix2 < count; ix2++) { for (int ix1 = 0; ix1 < ix2; ix1++) { if (!string.IsNullOrEmpty(textBoxes[ix2].Text) && (textBoxes[ix2].Text == textBoxes[ix1].Text)) { /// Two non-empty codes are equal Color oriBackColor1 = textBoxes[ix1].BackColor; Color oriBackColor2 = textBoxes[ix2].BackColor; textBoxes[ix1].BackColor = Color.Yellow; textBoxes[ix2].BackColor = Color.Yellow; MessageBox.Show("Našli sa dva rovnaké kódy."); textBoxes[ix1].BackColor = oriBackColor1; textBoxes[ix2].BackColor = oriBackColor2; return true; } } } return false; } private void clearButton_Click(object sender, EventArgs e) { if (MessageBox.Show("Naozaj chcete všetky kódy vymazať?", "Upozonenie", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes) { for (int i = 0; i < Math.Min(wmsCount, textBoxesCount); i++) { bodyNrBoxes[i].Text = string.Empty; serialNrBoxes[i].Text = string.Empty; serialNrBoxes[i].Text = string.Empty; checkBoxes[i].Checked = false; } assignedSNs.Clear(); } } private void textBox1_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 0); } private void textBox2_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 1); } private void textBox3_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 2); } private void textBox4_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 3); } private void textBox5_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 4); } private void textBox6_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 5); } private void textBox7_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 6); } private void textBox8_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 7); } private void textBox9_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 8); } private void textBox10_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 9); } private void textBox11_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 10); } private void textBox12_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 11); } private void textBox13_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 12); } private void textBox14_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 13); } private void textBox15_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 14); } private void textBox16_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 15); } private void textBox17_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 16); } private void textBox18_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 17); } private void textBox19_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 18); } private void textBox20_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 19); } private void textBox21_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 20); } private void textBox22_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 21); } private void textBox23_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 22); } private void textBox24_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 23); } private void textBox25_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 24); } private void textBox26_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 25); } private void textBox27_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 26); } private void textBox28_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 27); } private void textBox29_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 28); } private void textBox30_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 29); } private void textBox31_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 30); } private void textBox32_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 31); } private void textBox33_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 32); } private void textBox34_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 33); } private void textBox35_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 34); } private void textBox36_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 35); } private void textBox37_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 36); } private void textBox38_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 37); } private void textBox39_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 38); } private void textBox40_KeyPress(object s, KeyPressEventArgs e) { BodyNoTextBoxKeyPress(s, e, 39); } /// /// Process a key press within any enabled combo boxe /// /// /// /// TextBox control which received the event private void BodyNoTextBoxKeyPress(object sender, KeyPressEventArgs e, int wmNr0) { if (closeButton != '\0' && e.KeyChar == closeButton) { /// Process '+' key ..... Form completed okButton_Click(sender, e); } else if (e.KeyChar == '\r') { /// Process key ..... Go to the next text field nextWaterMeterIx = (wmNr0 + 1) % textBoxesCount; bodyNrBoxes[nextWaterMeterIx].Focus(); ColorFlashAndBeep(this, new ColorFlashEventArgs(600, Color.Green, 700, 500)); } else { checkBoxes[wmNr0].Checked = true; disabled[wmNr0] = false; } } private void textBox41_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 0); } private void textBox42_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 1); } private void textBox43_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 2); } private void textBox44_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 3); } private void textBox45_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 4); } private void textBox46_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 5); } private void textBox47_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 6); } private void textBox48_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 7); } private void textBox49_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 8); } private void textBox50_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 9); } private void textBox51_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 10); } private void textBox52_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 11); } private void textBox53_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 12); } private void textBox54_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 13); } private void textBox55_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 14); } private void textBox56_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 15); } private void textBox57_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 16); } private void textBox58_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 17); } private void textBox59_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 18); } private void textBox60_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 19); } private void textBox61_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 20); } private void textBox62_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 21); } private void textBox63_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 22); } private void textBox64_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 23); } private void textBox65_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 24); } private void textBox66_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 25); } private void textBox67_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 26); } private void textBox68_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 27); } private void textBox69_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 28); } private void textBox70_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 29); } private void textBox71_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 30); } private void textBox72_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 31); } private void textBox73_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 32); } private void textBox74_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 33); } private void textBox75_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 34); } private void textBox76_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 35); } private void textBox77_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 36); } private void textBox78_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 37); } private void textBox79_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 38); } private void textBox80_KeyPress(object s, KeyPressEventArgs e) { SnTextBoxKeyPress(s, e, 39); } /// /// Process a key press within any enabled combo boxe /// /// /// /// TextBox control which received the event private void SnTextBoxKeyPress(object sender, KeyPressEventArgs e, int wmNr0) { if (closeButton != '\0' && e.KeyChar == closeButton) { /// Process '+' key ..... Form completed okButton_Click(sender, e); } else if (e.KeyChar == '\r') { /// Process key ..... Next text field nextWaterMeterIx = (wmNr0 + 1) % textBoxesCount; serialNrBoxes[nextWaterMeterIx].Focus(); } } private void checkBox1_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 0); } private void checkBox2_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 1); } private void checkBox3_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 2); } private void checkBox4_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 3); } private void checkBox5_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 4); } private void checkBox6_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 5); } private void checkBox7_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 6); } private void checkBox8_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 7); } private void checkBox9_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 8); } private void checkBox10_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 9); } private void checkBox11_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 10); } private void checkBox12_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 11); } private void checkBox13_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 12); } private void checkBox14_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 13); } private void checkBox15_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 14); } private void checkBox16_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 15); } private void checkBox17_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 16); } private void checkBox18_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 17); } private void checkBox19_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 18); } private void checkBox20_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 19); } private void checkBox21_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 20); } private void checkBox22_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 21); } private void checkBox23_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 22); } private void checkBox24_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 23); } private void checkBox25_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 24); } private void checkBox26_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 25); } private void checkBox27_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 26); } private void checkBox28_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 27); } private void checkBox29_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 28); } private void checkBox30_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 29); } private void checkBox31_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 30); } private void checkBox32_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 31); } private void checkBox33_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 32); } private void checkBox34_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 33); } private void checkBox35_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 34); } private void checkBox36_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 35); } private void checkBox37_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 36); } private void checkBox38_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 37); } private void checkBox39_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 38); } private void checkBox40_CheckedChanged(object s, EventArgs e) { ProcessCheckBoxChecked(s, e, 39); } /// void ProcessCheckBoxChecked(object sender, EventArgs e, int wmNr0) { if (atTheEnd) { AssignSerialNumbers(); } } private void orderCB_SelectedIndexChanged(object sender, EventArgs e) { if (!loadedOnce) return; ProcessPO(); FocusToNextField(); } private void reloadButton_Click(object sender, EventArgs e) { ProcessPO(); FocusToNextField(); } void ProcessPO() { if (ReadPurchaseOrderParamsFromDB(ProcessData.OracleDB.SelectedDB, orderCB.Text, out firstSN, out snDigits, out pcsCount, out prefix, out suffix)) { prefixTextBox.Text = (prefix != null) ? prefix : string.Empty; suffixTextBox.Text = (suffix != null) ? suffix : string.Empty; snDigitsTextBox.Text = snDigits.ToString(); firstSNTextBox.Text = firstSN.ToString(string.Format("D{0}", snDigits)); pcsCountTextBox.Text = pcsCount.ToString(); exclamationPOLabel.Visible = false; } else { prefixTextBox.Text = suffixTextBox.Text = string.Empty; firstSNTextBox.Text = snDigitsTextBox.Text = pcsCountTextBox.Text = string.Empty; exclamationPOLabel.Visible = true; } if (UpdateSNFromTo()) { AssignSerialNumbers(); } else { Invalidate(rect); } } bool ReadPurchaseOrderParamsFromDB(OracleConnection connection, string orderNr, out int firstSN, out int snDigits, out int piecesCount, out string prefix, out string suffix) { firstSN = 0; piecesCount = 0; snDigits = 0; prefix = null; suffix = null; #if DEBUG firstSN = 101; piecesCount = 100; snDigits = 4; prefix = "M789"; suffix = null; foundMeters.Clear(); foundSNs.Clear(); for (int sn = firstSN; sn <= 125; sn++) { foundMeters.Add(new BodySerNrResult(string.Format("681172554VQ200034{0:D3}", sn), sn, 96)); foundSNs.Add(sn); } return true; #endif if (connection == null || string.IsNullOrEmpty(orderNr)) return false; try { connection.Open(); foreach (var status in new string[] { "NEU", "AKTIV" }) { /// STATUS='NEU' OracleCommand cmd = new OracleCommand("SELECT sollstueck, erste_zaehlernr, prefix, suffix, znrlaenge FROM vt_auftrag_pd" + " WHERE produktion_anbid=:1 AND status=:2 AND produktion_usestate=:3 AND auftragsnummer=:4", connection); cmd.Parameters.Add(new OracleParameter { OracleDbType = OracleDbType.Int32, Value = Output.DB.SensusOracle.Database.ProducerAnbid }); ///:1 cmd.Parameters.Add(new OracleParameter { OracleDbType = OracleDbType.Varchar2, Value = status }); ///:2 cmd.Parameters.Add(new OracleParameter { OracleDbType = OracleDbType.Int32, Value = 0 }); ///:3 cmd.Parameters.Add(new OracleParameter { OracleDbType = OracleDbType.Varchar2, Value = orderNr }); ///:4 OracleDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { piecesCount = dr.GetInt32(0); firstSN = dr.GetInt32(1); try { prefix = dr.GetString(2); } catch { prefix = null; } try { suffix = dr.GetString(3); } catch { suffix = null; } snDigits = dr.GetInt32(4); } dr.Close(); } if (firstSN == 0 && piecesCount == 0) { string msg2 = string.Format("No PO={0} found in Oracle DB", orderNr); log.Warn(msg2); System.Diagnostics.Debug.WriteLine(msg2); connection.Close(); return false; } foundMeters.Clear(); OracleCommand cmd2 = new OracleCommand("SELECT vt_fernum, zaehlernummer, hydr_pruefung FROM vt_zaehler_pd" + " WHERE anbid=:1 AND auftragsnummer=:2 ORDER BY zaehlernummer ASC", connection); cmd2.Parameters.Add(new OracleParameter { OracleDbType = OracleDbType.Int32, Value = Output.DB.SensusOracle.Database.ProducerAnbid }); ///:1 cmd2.Parameters.Add(new OracleParameter { OracleDbType = OracleDbType.Varchar2, Value = orderNr }); ///:2 OracleDataReader dr2 = cmd2.ExecuteReader(); while (dr2.Read()) { string bodyNr = dr2.GetString(0); /// Body number int serialNr = dr2.GetInt32(1); /// Assigned serial number when OK, 0 when NOK int result = dr2.GetInt32(2); /// HYDR_PRUEFUNG: 96=OK, 224=OK after a repetition, 97=NOK if (serialNr > 0 && result != 97) { foundMeters.Add(new BodySerNrResult(bodyNr, serialNr, result)); } } dr2.Close(); string msg = string.Format("PO={0} found in Oracle DB: first S/N={1}, total pieces count={2}, pieces produced so far={3}", orderNr, firstSN, piecesCount, foundMeters.Count); log.Warn(msg); System.Diagnostics.Debug.WriteLine(msg); connection.Close(); foundSNs.Clear(); foreach (var mtr in foundMeters) { if (mtr.SerialNr > 0) foundSNs.Add(mtr.SerialNr); } return true; } catch (Exception exc) { string msg = string.Format("ERROR: ORACLE DB query select AUFTRAGSNUMMER, SOLLSTUECK from VT_AUFTRAG_PD where ... failed: {0}", exc.Message); log.Error(msg); System.Diagnostics.Debug.WriteLine(msg); firstSN = 0; piecesCount = 0; connection.Close(); return false; } } private void firstSNRangeCB_TextChanged(object s, EventArgs e) { if (!loadedOnce) return; if (UpdateSNFromTo()) AssignSerialNumbers(); else Invalidate(rect); } private void lastSNRangeCB_TextChanged(object s, EventArgs e) { if (!loadedOnce) return; if (UpdateSNFromTo()) AssignSerialNumbers(); else Invalidate(rect); } private void firstSNRangeCB_SelectedIndexChanged(object s, EventArgs e) { if (!loadedOnce) return; if (UpdateSNFromTo()) AssignSerialNumbers(); else Invalidate(rect); FocusToNextField(); } private void lastSNRangeCB_SelectedIndexChanged(object s, EventArgs e) { if (!loadedOnce) return; if (UpdateSNFromTo()) AssignSerialNumbers(); else Invalidate(rect); FocusToNextField(); } /// /// /// /// true when the range of serial numbers is valid bool UpdateSNFromTo() { bool anyError = false; int _snRangeFirst; if (int.TryParse(firstSNRangeCB.Text, out _snRangeFirst) && (_snRangeFirst >= firstSN) && (_snRangeFirst < firstSN + pcsCount)) { snRangeFirst = _snRangeFirst; exclamationFromLabel.Visible = false; } else { snRangeFirst = 0; exclamationFromLabel.Visible = true; anyError = true; } int _snRangeLast; if (int.TryParse(lastSNRangeCB.Text, out _snRangeLast) && (_snRangeLast >= firstSN) && (_snRangeLast < firstSN + pcsCount)) { snRangeLast = _snRangeLast; exclamationToLabel.Visible = false; } else { snRangeLast = 0; exclamationToLabel.Visible = true; anyError = true; } if (snRangeFirst > 0 && snRangeLast > 0 && snRangeFirst > snRangeLast) { snRangeFirst = snRangeLast = 0; exclamationFromLabel.Visible = exclamationToLabel.Visible = true; anyError = true; } return !anyError; } /// /// Returns next free serial number within specified range or 0 if no appropriate s/n is available /// /// Serial number or 0 int GetNextSerialNr() { for (int sn = snRangeFirst; sn <= snRangeLast; sn++) { if (!foundSNs.Contains(sn) && !assignedSNs.Contains(sn)) { /// A free serial number within specified range found assignedSNs.Add(sn); return sn; } } return 0; /// No free serial number found } public void OnFocusPressed(object sender, FocusPressedEventArgs data) { if (FocusPressedHandler == null) return; try { FocusPressedHandler(sender, data); } catch (Exception) { // log.Error("FocusPressedHandler(...) failed", e); } } public event EventHandler FocusPressedHandler; /// public void FocusPressed(object sender, FocusPressedEventArgs data) { } public void OnBarcodeReceived(object sender, BarcodeReceivedEventArgs data) { if (BarcodeReceivedHandler == null) return; try { BarcodeReceivedHandler(sender, data); } catch (Exception exc) { string msg = exc.Message; // log.Error("BarcodeReceivedHandler(...) failed", e); } } public event EventHandler BarcodeReceivedHandler; /// public void BarcodeReceived(object sender, BarcodeReceivedEventArgs data) { } private void ManageCheckGroupBox(CheckBox chk, GroupBox grp) { /// Make sure the CheckBox isn't in the GroupBox. This will only happen the first time. if (chk.Parent == grp) { grp.Parent.Controls.Add(chk); /// Reparent the CheckBox so it's not in the GroupBox. chk.Location = new System.Drawing.Point(chk.Left + grp.Left, chk.Top + grp.Top); /// Adjust the CheckBox's location. chk.BringToFront(); /// Move the CheckBox to the top of the stacking order. } /// Enable or disable the GroupBox. grp.Enabled = chk.Checked; } private void orderCheckBox_CheckedChanged(object sender, EventArgs e) { ManageCheckGroupBox(orderCheckBox, orderGroupBox); if (!orderCheckBox.Checked) { if (atTheEnd) { AssignSerialNumbers(); } else { FocusToNextField(); Invalidate(rect); } } } private void FormForMechanicalMeters_FormClosing(object sender, FormClosingEventArgs e) { Program.LocalSettings.UpdateHistory(orderCB.Text, ref Program.LocalSettings.PurchaseOrderHistory); Program.LocalSettings.UpdateHistory(firstSNRangeCB.Text, ref Program.LocalSettings.FirstSNHistory); Program.LocalSettings.UpdateHistory(lastSNRangeCB.Text, ref Program.LocalSettings.LastSNHistory); Program.LocalSettings.LastSNTexts = bodyNrText; if (withProductionTracing) { //UpdateAndSaveStoredCodesDictionary(currentWorkflow, palety); //if ((currentWorkflow != null) && (Program.LocalSettings.LastWorkflow != currentWorkflow.Name)) //{ // Program.LocalSettings.LastWorkflow = currentWorkflow.Name; //} //if ((currentWStep != null) && (Program.LocalSettings.LastWStep != currentWStep.Name)) //{ // Program.LocalSettings.LastWStep = currentWStep.Name; //} } Program.LocalSettings.Save(); //wplaceRegistration.UnregisterWorkplace(dbSession); } #region Color flash and beep /// /// Called from the state machine when an operation forces a modeless dialog close. /// public void ColorFlashAndBeep(object sender, ColorFlashEventArgs args) { Common.BackgroundBeep.Beep(); if (ColorFlashHandler != null) { try { ColorFlashHandler(sender, args); } catch (Exception) { } } } public event EventHandler ColorFlashHandler; private System.Windows.Forms.Timer colorFlashDurationTimer; void OnColorFlash(object sender, ColorFlashEventArgs args) { // start timer here if (colorFlashDurationTimer == null) { colorFlashDurationTimer = new System.Windows.Forms.Timer(); colorFlashDurationTimer.Tick += (s, e) => { ColorFlashAction(); }; } BackColor = args.FlashColor; colorFlashDurationTimer.Interval = args.FlashTime; colorFlashDurationTimer.Start(); } // Specify what you want to happen when the Elapsed event is // raised. private void ColorFlashAction() { BackColor = oriBackColor; colorFlashDurationTimer.Stop(); colorFlashDurationTimer.Enabled = false; } #endregion private void FormForMechanicalMeters_Paint(object sender, PaintEventArgs e) { var g = this.CreateGraphics(); PaintRect(g, rect, firstSN, pcsCount, snRangeFirst, snRangeLast, foundSNs, assignedSNs); } void PaintRect(System.Drawing.Graphics g, Rectangle r, int first, int count, int rngFrom, int rngTo, IList foundSNs, IList assignedSNs) { SolidBrush greenBrush = new SolidBrush(Color.SeaGreen); SolidBrush yellowBrush = new SolidBrush(Color.Yellow); SolidBrush whiteBrush = new SolidBrush(Color.White); SolidBrush fadedGreenBrush = new SolidBrush(Color.DarkSeaGreen); SolidBrush fadedWhiteBrush = new SolidBrush(Color.Silver); if (0 < first && first <= rngFrom && rngFrom <= rngTo && rngTo < firstSN + count) { float left = Convert.ToSingle(r.Left); float top = Convert.ToSingle(r.Top); float height = Convert.ToSingle(r.Height); float elWidth = Convert.ToSingle(r.Width) / Convert.ToSingle(count); for (int sn = first; sn < first + count; sn++) { float x = left + (sn - first) * elWidth; if (foundSNs != null && foundSNs.Contains(sn)) { if (rngFrom <= sn && sn <= rngTo) g.FillRectangle(greenBrush, x, top, elWidth, height); else g.FillRectangle(fadedGreenBrush, x, top, elWidth, height); } else if (assignedSNs != null && assignedSNs.Contains(sn)) { g.FillRectangle(yellowBrush, x, top, elWidth, height); } else { if (rngFrom <= sn && sn <= rngTo) g.FillRectangle(whiteBrush, x, top, elWidth, height); else g.FillRectangle(fadedWhiteBrush, x, top, elWidth, height); } } } else { g.FillRectangle(fadedWhiteBrush, r); } } } }