/// /// Copyright (c) 2013-2015 Sensus Metering Systems /// using System; using System.Collections.Generic; using System.Windows.Forms; using log4net; using TBF.Rig.GenericDevices; using TBF.Resources; namespace TBF.Rig.DataEntry.Combined { public partial class TestStartEndForm : Form, GenericDevices.IHasCompleted { private static readonly ILog log = LogManager.GetLogger(typeof(TestStartEndForm)); // Set to 'true' when the form closes public bool Completed { get { return completed; } } bool completed; /// Number of water meters public readonly int WaterMetersCount; /// Main + Aux water meter count as one (combined) water meter readonly IRegReader[] regReaders; readonly bool[] disabled; // To be retrieved after the form closes public double[] MainStartState; public double[] AuxStartState; // To be retrieved after the form closes public readonly string[] MainStartStateStr; public readonly string[] AuxStartStateStr; // To be retrieved after the form closes public double[] MainEndState; public double[] AuxEndState; bool endStates; /// false=start states, true=end states double refVolume; double warningLimLo; /// limit to display exclamation mark, typically 2x errLimLo double warningLimHi; /// limit to display exclamation mark, typically 2x errLimHi int TextBoxesCount; /// Label[] mainLabels; Label[] auxLabels; TextBox[] mainStartTextBoxes; TextBox[] auxStartTextBoxes; TextBox[] mainEndTextBoxes; TextBox[] auxEndTextBoxes; IList enabledTextBoxes; /// /// Parameterless constructor initializing common part for start and endstate form /// public TestStartEndForm() { InitializeComponent(); this.Icon = Properties.Resources.TBF_icon; ControlBox = false; TextBoxesCount = 3; endStates = false; mainLabels = new Label[] { main1Label, main2Label, main3Label, }; auxLabels = new Label[] { aux1Label, aux2Label, aux3Label, }; mainStartTextBoxes = new TextBox[] { main1StartTextBox, main2StartTextBox, main3StartTextBox, }; auxStartTextBoxes = new TextBox[] { aux1StartTextBox, aux2StartTextBox, aux3StartTextBox, }; mainEndTextBoxes = new TextBox[] { main1EndTextBox, main2EndTextBox, main3EndTextBox, }; auxEndTextBoxes = new TextBox[] { aux1EndTextBox, aux2EndTextBox, aux3EndTextBox, }; enabledTextBoxes = new List(); completed = false; StartForceCloseHandler(); } /// /// Constructor to fill in start states /// /// Number of water meters /// Information from CycleBeginningForm about availability of water meters public TestStartEndForm(int waterMetersCount, IRegReader[] regReaders, bool[] disabled) : this() { endStates = false; this.WaterMetersCount = waterMetersCount; this.regReaders = regReaders; this.disabled = disabled; ShuffleTextBoxes(); MainStartState = new double[waterMetersCount]; AuxStartState = new double[waterMetersCount]; MainStartStateStr = new string[waterMetersCount]; AuxStartStateStr = new string[waterMetersCount]; } /// /// Constructor to fill in end states /// /// Number of water meters /// Information entered on test start (main) /// Information entered on test start (aux) /// Information from CycleBeginningForm about availability of water meters /// Reference volume, it is used to verify the end state, show/hide exclamation if necessary /// Error limit low, it is used to verify the end state, show/hide exclamation if necessary /// Error limit high, it is used to verify the end state, show/hide exclamation if necessary public TestStartEndForm(int waterMetersCount, IRegReader[] regReaders, string[] mainStartStateStr, string[] auxStartStateStr, bool[] disabled, double refVolume, double errLimLo, double errLimHi) : this() { endStates = true; this.WaterMetersCount = waterMetersCount; this.regReaders = regReaders; if (mainStartStateStr == null || mainStartStateStr.Length != waterMetersCount) throw (new Exception("Invalid 'mainStartStateStr' argument")); this.MainStartStateStr = mainStartStateStr; if (auxStartStateStr == null || auxStartStateStr.Length != waterMetersCount) throw (new Exception("Invalid 'auxStartStateStr' argument")); this.AuxStartStateStr = auxStartStateStr; this.disabled = disabled; this.refVolume = refVolume; this.warningLimLo = 2 * errLimLo; this.warningLimHi = 2 * errLimHi; ShuffleTextBoxes(); MainEndState = new double[waterMetersCount]; AuxEndState = new double[waterMetersCount]; } /// /// Make sure the layout of labels/text boxes on the screen /// corresponds to the layout of watermeters of the test bench. /// void ShuffleTextBoxes() { if (TBF.Data.CompoundWMsCount < TextBoxesCount) { TextBoxesCount = TBF.Data.CompoundWMsCount; } } private void CycleEndForm_Load(object sender, EventArgs e) { Localize(); //Height = 115 + 90 * WaterMetersCount; for (int i = 0; i < TextBoxesCount; i++) { if (i < WaterMetersCount) { mainLabels[i].Visible = mainStartTextBoxes[i].Visible = mainEndTextBoxes[i].Visible = true; auxLabels[i].Visible = auxStartTextBoxes[i].Visible = auxEndTextBoxes[i].Visible = true; } } if (TextBoxesCount >= 1) groupBox1.Visible = true; if (TextBoxesCount >= 2) groupBox2.Visible = true; if (TextBoxesCount >= 3) groupBox3.Visible = true; if (endStates) { for (int i = 0; i < TextBoxesCount; i++) { if (MainStartStateStr != null && i < MainStartStateStr.Length) { mainStartTextBoxes[i].Text = MainStartStateStr[i]; } if (AuxStartStateStr != null && i < AuxStartStateStr.Length) { auxStartTextBoxes[i].Text = AuxStartStateStr[i]; } if (disabled != null && i < disabled.Length && disabled[i]) { mainEndTextBoxes[i].Enabled = false; auxEndTextBoxes[i].Enabled = false; } else { mainEndTextBoxes[i].Enabled = true; auxEndTextBoxes[i].Enabled = true; enabledTextBoxes.Add(mainEndTextBoxes[i]); enabledTextBoxes.Add(auxEndTextBoxes[i]); } } } else { for (int i = 0; i < TextBoxesCount; i++) { if (disabled != null && i < disabled.Length && disabled[i]) { mainStartTextBoxes[i].Enabled = false; auxStartTextBoxes[i].Enabled = false; } else { mainStartTextBoxes[i].Enabled = true; auxStartTextBoxes[i].Enabled = true; enabledTextBoxes.Add(mainStartTextBoxes[i]); enabledTextBoxes.Add(auxStartTextBoxes[i]); } } } } void Localize() { Text = Strings.Water_Meter_States; groupBox1.Text = Strings.Water_Meter + " 1"; groupBox2.Text = Strings.Water_Meter + " 2"; groupBox3.Text = Strings.Water_Meter + " 3"; for (int i = 0; i < TextBoxesCount; i++) { mainLabels[i].Text = Strings.Main_; auxLabels[i].Text = Strings.Aux_; } startLabel.Text = Strings.Start; endLabel.Text = Strings.End; okButton.Text = Strings.OkBtnText; } private void okButton_Click(object sender, EventArgs eArgs) { try { if (endStates) { for (int i = 0; i < Math.Min(WaterMetersCount, TextBoxesCount); i++) { if (mainEndTextBoxes[i].Visible && mainEndTextBoxes[i].Enabled) { MainEndState[i] = Utils.ParseUDouble(mainEndTextBoxes[i].Text); } if (auxEndTextBoxes[i].Visible && auxEndTextBoxes[i].Enabled) { AuxEndState[i] = Utils.ParseUDouble(auxEndTextBoxes[i].Text); } } } else { for (int i = 0; i < Math.Min(WaterMetersCount, TextBoxesCount); i++) { if (mainStartTextBoxes[i].Visible && mainStartTextBoxes[i].Enabled) { MainStartStateStr[i] = mainStartTextBoxes[i].Text; MainStartState[i] = Utils.ParseUDouble(mainStartTextBoxes[i].Text); } if (auxStartTextBoxes[i].Visible && auxStartTextBoxes[i].Enabled) { AuxStartStateStr[i] = auxStartTextBoxes[i].Text; AuxStartState[i] = Utils.ParseUDouble(auxStartTextBoxes[i].Text); } } } } catch (Exception e) { log.ErrorFormat("Exception: {0}", e.Message); return; } completed = true; Close(); } #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 /// /// Calculate the approximate error and verify whether it is within limits. /// Make an exclamation mark visible if out of range. /// Similar event handler is implemented for all main/auxEndTextBoxes (1..3). /// private void main1EndTextBox_TextChanged(object sender, EventArgs e) { //double err = 0; //try //{ // double endState = Utils.ParseUDouble(main1EndTextBox.Text); // double startState = Utils.ParseUDouble(main1StartTextBox.Text); // err = 100.0 * (endState - startState - refVolume) / refVolume; //} //catch { }; //main1Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err); } private void aux1EndTextBox_TextChanged(object sender, EventArgs e) { //double err = 0; //try //{ // double endState = Utils.ParseUDouble(aux1EndTextBox.Text); // double startState = Utils.ParseUDouble(aux1StartTextBox.Text); // err = 100.0 * (endState - startState - refVolume) / refVolume; //} //catch { err = 1000.0f; }; //aux1Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err); } private void main2EndTextBox_TextChanged(object sender, EventArgs e) { //double err = 0; //try //{ // double endState = Utils.ParseUDouble(main2EndTextBox.Text); // double startState = Utils.ParseUDouble(main2StartTextBox.Text); // err = 100.0 * (endState - startState - refVolume) / refVolume; //} //catch { err = 1000.0f; }; //main2Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err); } private void aux2EndTextBox_TextChanged(object sender, EventArgs e) { //double err = 0; //try //{ // double endState = Utils.ParseUDouble(aux2EndTextBox.Text); // double startState = Utils.ParseUDouble(aux2StartTextBox.Text); // err = 100.0 * (endState - startState - refVolume) / refVolume; //} //catch { err = 1000.0f; }; //aux2Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err); } private void main3EndTextBox_TextChanged(object sender, EventArgs e) { //double err = 0; //try //{ // double endState = Utils.ParseUDouble(main3EndTextBox.Text); // double startState = Utils.ParseUDouble(main3StartTextBox.Text); // err = 100.0 * (endState - startState - refVolume) / refVolume; //} //catch { err = 1000.0f; }; //main3Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err); } private void aux3EndTextBox_TextChanged(object sender, EventArgs e) { //double err = 0; //try //{ // double endState = Utils.ParseUDouble(aux3EndTextBox.Text); // double startState = Utils.ParseUDouble(aux3StartTextBox.Text); // err = 100.0 * (endState - startState - refVolume) / refVolume; //} //catch { err = 1000.0f; }; //aux3Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err); } /// /// Process a key press within any enabled text boxe /// /// /// /// TextBox control which received the event private void ProcKeyPress(object sender, KeyPressEventArgs e, TextBox currentFocusOwner) { if (e.KeyChar == '+') { /// Process '+' key ..... Form completed okButton_Click(sender, e); } if (e.KeyChar == '\r') { /// Process key ..... Next text field if (enabledTextBoxes.Count < 2) return; /// There is just one enabled textbox for (int i = 0; i < enabledTextBoxes.Count; i++) { if (enabledTextBoxes[i] == currentFocusOwner) { /// Change focus to the next enabled text box enabledTextBoxes[(i + 1) % enabledTextBoxes.Count].Focus(); return; } } } } private void main1StartTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, main1StartTextBox); } private void main2StartTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, main2StartTextBox); } private void main3StartTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, main3StartTextBox); } private void aux1StartTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, aux1StartTextBox); } private void aux2StartTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, aux2StartTextBox); } private void aux3StartTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, aux3StartTextBox); } private void main1EndTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, main1EndTextBox); } private void main2EndTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, main2EndTextBox); } private void main3EndTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, main3EndTextBox); } private void aux1EndTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, aux1EndTextBox); } private void aux2EndTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, aux2EndTextBox); } private void aux3EndTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, aux3EndTextBox); } } }