/// /// Copyright (c) 2013-2015 Sensus Metering Systems /// using System; using System.Collections.Generic; using System.Windows.Forms; using log4net; using TBF.Resources; #pragma warning disable 162 /// Disables 'Unreachable code detected' warning namespace TBF.BenchControl.DataEntry.Combined { public partial class CycleBeginningForm : Form, GenericDevices.IHasCompleted { private static readonly ILog log = LogManager.GetLogger(typeof(CycleBeginningForm)); /// Number of water meters public readonly int WaterMetersCount; // To be retrieved after the form closes public string PurchaseOrder; public string[] MainSNText; public string[] AuxSNText; public bool[] Disabled; // Set to 'true' when the form closes public bool Completed { get { return completed; } } bool completed; /// Number of text boxes for serial numbers int TextBoxesCount; Label[] mainLabels; Label[] auxLabels; ComboBox[] mainComboBoxes; ComboBox[] auxComboBoxes; CheckBox[] checkBoxes; IList enabledComboBoxes; public CycleBeginningForm() { InitializeComponent(); ControlBox = false; TextBoxesCount = 3; mainLabels = new Label[] { mainSN1Label, mainSN2Label, mainSN3Label }; auxLabels = new Label[] { auxSN1Label, auxSN2Label, auxSN3Label }; mainComboBoxes = new ComboBox[] { main1ComboBox, main2ComboBox, main3ComboBox }; auxComboBoxes = new ComboBox[] { aux1ComboBox, aux2ComboBox, aux3ComboBox }; checkBoxes = new CheckBox[] { checkBox1, checkBox2, checkBox3 }; enabledComboBoxes = new List(); this.WaterMetersCount = Config.Data.CompoundWMsCount; /// Default number of compound water meters completed = false; StartForceCloseHandler(); } /// /// Constructor /// /// Number of text boxes for serial numbers public CycleBeginningForm(int compoundWaterMetersCount) : this() { this.WaterMetersCount = compoundWaterMetersCount; /// = Program.CompondWMsCount MainSNText = new string[compoundWaterMetersCount]; AuxSNText = new string[compoundWaterMetersCount]; Disabled = new bool[compoundWaterMetersCount]; ShuffleTextBoxes(compoundWaterMetersCount); Height = 110 + WaterMetersCount * 157; } /// /// Make sure the layout of labels/text boxes on the screen /// corresponds to the layout of watermeters of the test bench. /// /// Numebr of combined watermeters void ShuffleTextBoxes(int compoundWMsCount) { if (compoundWMsCount < TextBoxesCount) TextBoxesCount = compoundWMsCount; } private void CycleBeginningForm_Load(object sender, EventArgs e) { Localize(); PrepareOrderCombo(orderComboBox); if (WaterMetersCount >= 1) groupBox1.Visible = true; if (WaterMetersCount >= 2) groupBox2.Visible = true; if (WaterMetersCount >= 3) groupBox3.Visible = true; for (int i = 0; i < TextBoxesCount; i++) { mainLabels[i].Visible = mainComboBoxes[i].Visible = true; auxLabels[i].Visible = auxComboBoxes[i].Visible = true; checkBoxes[i].Visible = true; enabledComboBoxes.Add(mainComboBoxes[i]); enabledComboBoxes.Add(auxComboBoxes[i]); if (Program.LocalSettings.LastMainSNTextsCount == TextBoxesCount && Program.LocalSettings.LastAuxSNTextsCount == TextBoxesCount) { mainComboBoxes[i].Items.Add(Program.LocalSettings.LastMainSNTexts[i]); auxComboBoxes[i].Items.Add(Program.LocalSettings.LastAuxSNTexts[i]); } } } void Localize() { Text = Strings.Data; orderGroupBox.Text = Strings.Purchase_order; groupBox1.Text = Strings.Water_Meter + " 1"; groupBox2.Text = Strings.Water_Meter + " 2"; groupBox3.Text = Strings.Water_Meter + " 3"; mainSN1Label.Text = Strings.Main_WM_SerialNr; mainSN2Label.Text = Strings.Main_WM_SerialNr; mainSN3Label.Text = Strings.Main_WM_SerialNr; auxSN1Label.Text = Strings.Aux_WM_SerialNr; auxSN2Label.Text = Strings.Aux_WM_SerialNr; auxSN3Label.Text = Strings.Aux_WM_SerialNr; okButton.Text = Strings.OkBtnText; clearButton.Text = Strings.ClearBtnText; } /// /// Load Purchase order combo box items from the LocalSettings PurchaseOrderHistory array /// /// Puchase order ComboBox void PrepareOrderCombo(ComboBox orderComboBox) { for (int i = 0; i < Program.LocalSettings.PurchaseOrderHistoryCount; i++) { orderComboBox.Items.Add(Program.LocalSettings.PurchaseOrderHistory[i]); } if (orderComboBox.Items.Count > 0) { orderComboBox.Text = orderComboBox.Items[0].ToString(); } } private void okButton_Click(object sender, EventArgs e) { PurchaseOrder = orderComboBox.Text; Program.LocalSettings.UpdateHistory(orderComboBox.Text, ref Program.LocalSettings.PurchaseOrderHistory #if FUZHOU150 || FUZHOU300 , 20 #endif ); Program.LocalSettings.LastMainSNTexts = MainSNText; Program.LocalSettings.LastAuxSNTexts = AuxSNText; for (int i = 0; i < TextBoxesCount; i++) { MainSNText[i] = mainComboBoxes[i].Text; AuxSNText[i] = auxComboBoxes[i].Text; Disabled[i] = !checkBoxes[i].Checked; } completed = true; Close(); } private void clearButton_Click(object sender, EventArgs e) { for (int i = 0; i < TextBoxesCount; i++) { mainComboBoxes[i].Text = string.Empty; auxComboBoxes[i].Text = string.Empty; checkBoxes[i].Checked = false; } } /// /// When one combo box is updated using drop-down menu, all combo boxes /// are updated by this function. /// private void UpdateAllCombos() { for (int i = 0; i < TextBoxesCount; i++) { mainComboBoxes[i].Text = Program.LocalSettings.LastMainSNTexts[i]; auxComboBoxes[i].Text = Program.LocalSettings.LastAuxSNTexts[i]; checkBoxes[i].Checked = !string.IsNullOrEmpty(mainComboBoxes[i].Text) || !string.IsNullOrEmpty(auxComboBoxes[i].Text); } } private void main1ComboBox_TextChanged(object sender, EventArgs e) { checkBox1.Checked = true; } private void main1ComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (Program.LocalSettings.LastMainSNTextsCount == WaterMetersCount) { if (main1ComboBox.Text.Equals(main1ComboBox.Items[0])) UpdateAllCombos(); } } private void aux1ComboBox_TextChanged(object sender, EventArgs e) { checkBox1.Checked = true; } private void aux1ComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (Program.LocalSettings.LastAuxSNTextsCount == WaterMetersCount) { if (aux1ComboBox.Text.Equals(aux1ComboBox.Items[0])) UpdateAllCombos(); } } private void main2ComboBox_TextChanged(object sender, EventArgs e) { checkBox2.Checked = true; } private void main2ComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (Program.LocalSettings.LastMainSNTextsCount == WaterMetersCount) { if (main2ComboBox.Text.Equals(main2ComboBox.Items[0])) UpdateAllCombos(); } } private void aux2ComboBox_TextChanged(object sender, EventArgs e) { checkBox2.Checked = true; } private void aux2ComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (Program.LocalSettings.LastAuxSNTextsCount == WaterMetersCount) { if (aux2ComboBox.Text.Equals(aux2ComboBox.Items[0])) UpdateAllCombos(); } } private void main3ComboBox_TextChanged(object sender, EventArgs e) { checkBox3.Checked = true; } private void main3ComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (Program.LocalSettings.LastMainSNTextsCount == WaterMetersCount) { if (main3ComboBox.Text.Equals(main3ComboBox.Items[0])) UpdateAllCombos(); } } private void aux3ComboBox_TextChanged(object sender, EventArgs e) { checkBox3.Checked = true; } private void aux3ComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (Program.LocalSettings.LastAuxSNTextsCount == WaterMetersCount) { if (aux3ComboBox.Text.Equals(aux3ComboBox.Items[0])) UpdateAllCombos(); } } #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); }; } void OnForceClose(object sender, EventArgs args) { DialogResult = DialogResult.Cancel; Close(); } #endregion /// /// Process a key press within any enabled text boxe /// /// /// /// TextBox control which received the event private void ProcKeyPress(object sender, KeyPressEventArgs e, ComboBox currentFocusOwner) { if (e.KeyChar == '+') { /// Process '+' key ..... Form completed okButton_Click(sender, e); } if (e.KeyChar == '\r') { /// Process key ..... Next text field if (enabledComboBoxes.Count < 2) return; /// There is just one enabled textbox for (int i = 0; i < enabledComboBoxes.Count; i++) { if (enabledComboBoxes[i] == currentFocusOwner) { /// Change focus to the next enabled text box enabledComboBoxes[(i + 1) % enabledComboBoxes.Count].Focus(); return; } } } } private void main1ComboBox_KeyPress(object sender, KeyPressEventArgs e) { ProcKeyPress(sender, e, main1ComboBox); } private void main2ComboBox_KeyPress(object sender, KeyPressEventArgs e) { ProcKeyPress(sender, e, main2ComboBox); } private void main3ComboBox_KeyPress(object sender, KeyPressEventArgs e) { ProcKeyPress(sender, e, main3ComboBox); } private void aux1ComboBox_KeyPress(object sender, KeyPressEventArgs e) { ProcKeyPress(sender, e, aux1ComboBox); } private void aux2ComboBox_KeyPress(object sender, KeyPressEventArgs e) { ProcKeyPress(sender, e, aux2ComboBox); } private void aux3ComboBox_KeyPress(object sender, KeyPressEventArgs e) { ProcKeyPress(sender, e, aux3ComboBox); } } }