///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using System.Windows.Forms;
using log4net;
using TBF.Resources;
namespace TBF.BenchControl.DataEntry.Standard
{
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 text boxes for serial numbers
public readonly int WaterMetersCount;
readonly bool[] disabled;
// To be retrieved after the form closes
public float[] WMStartState;
// To be retrieved after the form closes
public readonly string[] WMStartStateStr;
// To be retrieved after the form closes
public float[] WMEndState;
bool endStates; /// false=start states, true=end states
float refVolume;
float warningLimLo; /// limit to display exclamation mark, typically 2x errLimLo
float warningLimHi; /// limit to display exclamation mark, typically 2x errLimHi
///
/// Constructor to fill in start states
///
/// Number of text boxes for serial numbers
public TestStartEndForm(int waterMetersCount, bool[] disabled)
{
endStates = false;
this.WaterMetersCount = waterMetersCount;
this.disabled = disabled;
InitializeComponent();
WMStartState = new float[waterMetersCount];
WMStartStateStr = new string[waterMetersCount];
completed = false;
StartForceCloseHandler();
}
///
/// Constructor to fill in end states
///
/// Number of text boxes for serial numbers
public TestStartEndForm(int waterMetersCount, string[] wmStartStateStr, bool[] disabled,
float refVolume, float errLimLo, float errLimHi)
{
endStates = true;
this.WaterMetersCount = waterMetersCount;
this.WMStartStateStr = wmStartStateStr;
if (wmStartStateStr == null || wmStartStateStr.Length != waterMetersCount)
throw (new Exception("Invalid 'wmStartStateStr' argument"));
this.disabled = disabled;
this.refVolume = refVolume;
this.warningLimLo = 2 * errLimLo;
this.warningLimHi = 2 * errLimHi;
InitializeComponent();
WMEndState = new float[waterMetersCount];
completed = false;
StartForceCloseHandler();
}
/// Parameterless constructor for all watermeters
public TestStartEndForm()
: this(Program.WMsCount, null)
{
}
private void CycleEndForm_Load(object sender, EventArgs e)
{
Localize();
Height = 115 + 90 * WaterMetersCount;
if (WaterMetersCount < 1) groupBox1.Visible = false;
if (WaterMetersCount < 2) groupBox2.Visible = false;
if (WaterMetersCount < 3) groupBox3.Visible = false;
if (WaterMetersCount < 4) groupBox4.Visible = false;
if (WaterMetersCount < 5) groupBox5.Visible = false;
if (WaterMetersCount < 6) groupBox6.Visible = false;
if (endStates)
{
if (WaterMetersCount >= 1) wmStartStateTextBox1.Text = WMStartStateStr[0];
if (WaterMetersCount >= 2) wmStartStateTextBox2.Text = WMStartStateStr[1];
if (WaterMetersCount >= 3) wmStartStateTextBox3.Text = WMStartStateStr[2];
if (WaterMetersCount >= 4) wmStartStateTextBox4.Text = WMStartStateStr[3];
if (WaterMetersCount >= 5) wmStartStateTextBox5.Text = WMStartStateStr[4];
if (WaterMetersCount >= 6) wmStartStateTextBox6.Text = WMStartStateStr[5];
wmEndStateTextBox1.Enabled = (disabled == null || disabled.Length < 1 || !disabled[0]);
wmEndStateTextBox2.Enabled = (disabled == null || disabled.Length < 2 || !disabled[1]);
wmEndStateTextBox3.Enabled = (disabled == null || disabled.Length < 3 || !disabled[2]);
wmEndStateTextBox4.Enabled = (disabled == null || disabled.Length < 4 || !disabled[3]);
wmEndStateTextBox5.Enabled = (disabled == null || disabled.Length < 5 || !disabled[4]);
wmEndStateTextBox6.Enabled = (disabled == null || disabled.Length < 6 || !disabled[5]);
}
else
{
wmStartStateTextBox1.Enabled = (disabled == null || disabled.Length < 1 || !disabled[0]);
wmStartStateTextBox2.Enabled = (disabled == null || disabled.Length < 2 || !disabled[1]);
wmStartStateTextBox3.Enabled = (disabled == null || disabled.Length < 3 || !disabled[2]);
wmStartStateTextBox4.Enabled = (disabled == null || disabled.Length < 4 || !disabled[3]);
wmStartStateTextBox5.Enabled = (disabled == null || disabled.Length < 5 || !disabled[4]);
wmStartStateTextBox6.Enabled = (disabled == null || disabled.Length < 6 || !disabled[5]);
}
}
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";
groupBox4.Text = Strings.Water_Meter + " 4";
groupBox5.Text = Strings.Water_Meter + " 5";
groupBox6.Text = Strings.Water_Meter + " 6";
wmStateLabel1.Text = Strings.WMState;
wmStateLabel2.Text = Strings.WMState;
wmStateLabel3.Text = Strings.WMState;
wmStateLabel4.Text = Strings.WMState;
wmStateLabel5.Text = Strings.WMState;
wmStateLabel6.Text = Strings.WMState;
startLabel.Text = Strings.Start;
endLabel.Text = Strings.End;
okButton.Text = Strings.OkBtnText;
}
private void okButton_Click(object sender, EventArgs e)
{
try
{
if (endStates)
{
if (wmEndStateTextBox1.Enabled) WMEndState[0] = Utils.ParseUFloat(wmEndStateTextBox1.Text);
if (wmEndStateTextBox2.Enabled) WMEndState[1] = Utils.ParseUFloat(wmEndStateTextBox2.Text);
if (wmEndStateTextBox3.Enabled) WMEndState[2] = Utils.ParseUFloat(wmEndStateTextBox3.Text);
if (wmEndStateTextBox4.Enabled) WMEndState[3] = Utils.ParseUFloat(wmEndStateTextBox4.Text);
if (wmEndStateTextBox5.Enabled) WMEndState[4] = Utils.ParseUFloat(wmEndStateTextBox5.Text);
if (wmEndStateTextBox6.Enabled) WMEndState[5] = Utils.ParseUFloat(wmEndStateTextBox6.Text);
}
else
{
if (wmStartStateTextBox1.Enabled)
{
WMStartStateStr[0] = wmStartStateTextBox1.Text;
WMStartState[0] = Utils.ParseUFloat(wmStartStateTextBox1.Text);
}
if (wmStartStateTextBox2.Enabled)
{
WMStartStateStr[1] = wmStartStateTextBox2.Text;
WMStartState[1] = Utils.ParseUFloat(wmStartStateTextBox2.Text);
}
if (wmStartStateTextBox3.Enabled)
{
WMStartStateStr[2] = wmStartStateTextBox3.Text;
WMStartState[2] = Utils.ParseUFloat(wmStartStateTextBox3.Text);
}
if (wmStartStateTextBox4.Enabled)
{
WMStartStateStr[3] = wmStartStateTextBox4.Text;
WMStartState[3] = Utils.ParseUFloat(wmStartStateTextBox4.Text);
}
if (wmStartStateTextBox5.Enabled)
{
WMStartStateStr[4] = wmStartStateTextBox5.Text;
WMStartState[4] = Utils.ParseUFloat(wmStartStateTextBox5.Text);
}
if (wmStartStateTextBox6.Enabled)
{
WMStartStateStr[5] = wmStartStateTextBox6.Text;
WMStartState[5] = Utils.ParseUFloat(wmStartStateTextBox6.Text);
}
}
}
catch
{
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
private void wmEndStateTextBox1_TextChanged(object sender, EventArgs e)
{
float err = 0;
try
{
float endState = Utils.ParseUFloat(wmEndStateTextBox1.Text);
float startState = Utils.ParseUFloat(wmStartStateTextBox1.Text);
err = 100.0f * (endState - startState - refVolume) / refVolume;
}
catch { };
exclamation1.Visible = (err < warningLimLo) || (warningLimHi < err);
}
private void wmEndStateTextBox2_TextChanged(object sender, EventArgs e)
{
float err = 0;
try
{
float endState = Utils.ParseUFloat(wmEndStateTextBox2.Text);
float startState = Utils.ParseUFloat(wmStartStateTextBox2.Text);
err = 100.0f * (endState - startState - refVolume) / refVolume;
}
catch { err = 1000.0f; };
exclamation2.Visible = (err < warningLimLo) || (warningLimHi < err);
}
private void wmEndStateTextBox3_TextChanged(object sender, EventArgs e)
{
float err = 0;
try
{
float endState = Utils.ParseUFloat(wmEndStateTextBox3.Text);
float startState = Utils.ParseUFloat(wmStartStateTextBox3.Text);
err = 100.0f * (endState - startState - refVolume) / refVolume;
}
catch { err = 1000.0f; };
exclamation3.Visible = (err < warningLimLo) || (warningLimHi < err);
}
private void wmEndStateTextBox4_TextChanged(object sender, EventArgs e)
{
float err = 0;
try
{
float endState = Utils.ParseUFloat(wmEndStateTextBox4.Text);
float startState = Utils.ParseUFloat(wmStartStateTextBox4.Text);
err = 100.0f * (endState - startState - refVolume) / refVolume;
}
catch { err = 1000.0f; };
exclamation4.Visible = (err < warningLimLo) || (warningLimHi < err);
}
private void wmEndStateTextBox5_TextChanged(object sender, EventArgs e)
{
float err = 0;
try
{
float endState = Utils.ParseUFloat(wmEndStateTextBox5.Text);
float startState = Utils.ParseUFloat(wmStartStateTextBox5.Text);
err = 100.0f * (endState - startState - refVolume) / refVolume;
}
catch { err = 1000.0f; };
exclamation5.Visible = (err < warningLimLo) || (warningLimHi < err);
}
private void wmEndStateTextBox6_TextChanged(object sender, EventArgs e)
{
float err = 0;
try
{
float endState = Utils.ParseUFloat(wmEndStateTextBox6.Text);
float startState = Utils.ParseUFloat(wmStartStateTextBox6.Text);
err = 100.0f * (endState - startState - refVolume) / refVolume;
}
catch { err = 1000.0f; };
exclamation6.Visible = (err < warningLimLo) || (warningLimHi < err);
}
private void wmStartStateTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
if (wmStartStateTextBox2.Enabled) wmStartStateTextBox2.Focus();
else if (wmStartStateTextBox3.Enabled) wmStartStateTextBox3.Focus();
else if (wmStartStateTextBox4.Enabled) wmStartStateTextBox4.Focus();
else if (wmStartStateTextBox5.Enabled) wmStartStateTextBox5.Focus();
else if (wmStartStateTextBox6.Enabled) wmStartStateTextBox6.Focus();
}
else if (e.KeyChar == '+') okButton_Click(sender, e);
}
private void wmStartStateTextBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
if (wmStartStateTextBox3.Enabled) wmStartStateTextBox3.Focus();
else if (wmStartStateTextBox4.Enabled) wmStartStateTextBox4.Focus();
else if (wmStartStateTextBox5.Enabled) wmStartStateTextBox5.Focus();
else if (wmStartStateTextBox6.Enabled) wmStartStateTextBox6.Focus();
else if (wmStartStateTextBox1.Enabled) wmStartStateTextBox1.Focus();
}
else if (e.KeyChar == '+') okButton_Click(sender, e);
}
private void wmStartStateTextBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
if (wmStartStateTextBox4.Enabled) wmStartStateTextBox4.Focus();
else if (wmStartStateTextBox5.Enabled) wmStartStateTextBox5.Focus();
else if (wmStartStateTextBox6.Enabled) wmStartStateTextBox6.Focus();
else if (wmStartStateTextBox1.Enabled) wmStartStateTextBox1.Focus();
else if (wmStartStateTextBox2.Enabled) wmStartStateTextBox2.Focus();
}
else if (e.KeyChar == '+') okButton_Click(sender, e);
}
private void wmStartStateTextBox4_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
if (wmStartStateTextBox5.Enabled) wmStartStateTextBox5.Focus();
else if (wmStartStateTextBox6.Enabled) wmStartStateTextBox6.Focus();
else if (wmStartStateTextBox1.Enabled) wmStartStateTextBox1.Focus();
else if (wmStartStateTextBox2.Enabled) wmStartStateTextBox2.Focus();
else if (wmStartStateTextBox3.Enabled) wmStartStateTextBox3.Focus();
}
else if (e.KeyChar == '+') okButton_Click(sender, e);
}
private void wmStartStateTextBox5_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
if (wmStartStateTextBox6.Enabled) wmStartStateTextBox6.Focus();
else if (wmStartStateTextBox1.Enabled) wmStartStateTextBox1.Focus();
else if (wmStartStateTextBox2.Enabled) wmStartStateTextBox2.Focus();
else if (wmStartStateTextBox3.Enabled) wmStartStateTextBox3.Focus();
else if (wmStartStateTextBox4.Enabled) wmStartStateTextBox4.Focus();
}
else if (e.KeyChar == '+') okButton_Click(sender, e);
}
private void wmStartStateTextBox6_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
if (wmStartStateTextBox1.Enabled) wmStartStateTextBox1.Focus();
else if (wmStartStateTextBox1.Enabled) wmStartStateTextBox1.Focus();
else if (wmStartStateTextBox3.Enabled) wmStartStateTextBox3.Focus();
else if (wmStartStateTextBox4.Enabled) wmStartStateTextBox4.Focus();
else if (wmStartStateTextBox5.Enabled) wmStartStateTextBox5.Focus();
}
else if (e.KeyChar == '+') okButton_Click(sender, e);
}
private void wmEndStateTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
if (wmEndStateTextBox2.Enabled) wmEndStateTextBox2.Focus();
else if (wmEndStateTextBox3.Enabled) wmEndStateTextBox3.Focus();
else if (wmEndStateTextBox4.Enabled) wmEndStateTextBox4.Focus();
else if (wmEndStateTextBox5.Enabled) wmEndStateTextBox5.Focus();
else if (wmEndStateTextBox6.Enabled) wmEndStateTextBox6.Focus();
}
else if (e.KeyChar == '+') okButton_Click(sender, e);
}
private void wmEndStateTextBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
if (wmEndStateTextBox3.Enabled) wmEndStateTextBox3.Focus();
else if (wmEndStateTextBox4.Enabled) wmEndStateTextBox4.Focus();
else if (wmEndStateTextBox5.Enabled) wmEndStateTextBox5.Focus();
else if (wmEndStateTextBox6.Enabled) wmEndStateTextBox6.Focus();
else if (wmEndStateTextBox1.Enabled) wmEndStateTextBox1.Focus();
}
else if (e.KeyChar == '+') okButton_Click(sender, e);
}
private void wmEndStateTextBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
if (wmEndStateTextBox4.Enabled) wmEndStateTextBox4.Focus();
else if (wmEndStateTextBox5.Enabled) wmEndStateTextBox5.Focus();
else if (wmEndStateTextBox6.Enabled) wmEndStateTextBox6.Focus();
else if (wmEndStateTextBox1.Enabled) wmEndStateTextBox1.Focus();
else if (wmEndStateTextBox2.Enabled) wmEndStateTextBox2.Focus();
}
else if (e.KeyChar == '+') okButton_Click(sender, e);
}
private void wmEndStateTextBox4_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
if (wmEndStateTextBox5.Enabled) wmEndStateTextBox5.Focus();
else if (wmEndStateTextBox6.Enabled) wmEndStateTextBox6.Focus();
else if (wmEndStateTextBox1.Enabled) wmEndStateTextBox1.Focus();
else if (wmEndStateTextBox2.Enabled) wmEndStateTextBox2.Focus();
else if (wmEndStateTextBox3.Enabled) wmEndStateTextBox3.Focus();
}
else if (e.KeyChar == '+') okButton_Click(sender, e);
}
private void wmEndStateTextBox5_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
if (wmEndStateTextBox6.Enabled) wmEndStateTextBox6.Focus();
else if (wmEndStateTextBox1.Enabled) wmEndStateTextBox1.Focus();
else if (wmEndStateTextBox2.Enabled) wmEndStateTextBox2.Focus();
else if (wmEndStateTextBox3.Enabled) wmEndStateTextBox3.Focus();
else if (wmEndStateTextBox4.Enabled) wmEndStateTextBox4.Focus();
}
else if (e.KeyChar == '+') okButton_Click(sender, e);
}
private void wmEndStateTextBox6_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
if (wmEndStateTextBox1.Enabled) wmEndStateTextBox1.Focus();
else if (wmEndStateTextBox1.Enabled) wmEndStateTextBox1.Focus();
else if (wmEndStateTextBox3.Enabled) wmEndStateTextBox3.Focus();
else if (wmEndStateTextBox4.Enabled) wmEndStateTextBox4.Focus();
else if (wmEndStateTextBox5.Enabled) wmEndStateTextBox5.Focus();
}
else if (e.KeyChar == '+') okButton_Click(sender, e);
}
}
}