1. DataEntry.Uni implemented, 2. TeststartEndForm with s/n-s, 3. DataEntry.Double24 and Fewa deleted, ver. 3.6.2045
This commit is contained in:
@@ -0,0 +1,892 @@
|
||||
///
|
||||
/// Copyright (c) 2023 Sensus Slovensko a.s.
|
||||
///
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
using log4net;
|
||||
using Results.Entities;
|
||||
using TBF.Resources;
|
||||
|
||||
namespace TBF.Rig.DataEntry.Uni
|
||||
{
|
||||
public partial class CycleBgEnForm : Form, GenericDevices.IHasCompleted
|
||||
{
|
||||
private static readonly ILog log = LogManager.GetLogger(typeof(CycleBgEnForm));
|
||||
|
||||
/// Arguments
|
||||
public readonly IList<WaterMeter> WaterMeters;
|
||||
readonly int lineSize;
|
||||
readonly Sz sz;
|
||||
readonly bool isLrOrder; /// true = controls obtain focus after ENTER key in left-right order, false = top-down order
|
||||
readonly DEItem item1;
|
||||
readonly DEItem item2;
|
||||
readonly DEItem item3;
|
||||
readonly IList<DEItem> colItems;
|
||||
readonly bool isEnd;
|
||||
readonly string formCloseKeys;
|
||||
|
||||
/// Derived from arguments in the constructor
|
||||
readonly WaterMeter firstValidWM;
|
||||
readonly int wmsCount;
|
||||
readonly int linesCount;
|
||||
|
||||
/// Size and layout related values
|
||||
readonly Font font;
|
||||
readonly int meterHeight;
|
||||
readonly int meterWidth;
|
||||
readonly int margin;
|
||||
readonly int spacing;
|
||||
readonly int buttonsWidth;
|
||||
readonly int hdrHeight;
|
||||
readonly int labelWid;
|
||||
readonly int checkBoxWid;
|
||||
|
||||
/// UI elements
|
||||
readonly ComboBox comboBox1; /// Combo box for the 1st common item
|
||||
readonly ComboBox comboBox2; /// Combo box for the 2nd common item
|
||||
readonly ComboBox comboBox3; /// Combo box for the 3rd common item
|
||||
readonly Label[] labels; /// Labels for water meter numbers
|
||||
readonly ComboBox[,] comboBoxes; /// Combo boxes for values in columns
|
||||
readonly CheckBox[] checkBoxes; /// Check boxes for water meter enable/disable
|
||||
|
||||
bool isHandlersEnabled; /// Initially false, set to true after ComboBoxes are filled with data
|
||||
|
||||
/// Set to 'true' when the form closes
|
||||
public bool Completed { get { return completed; } }
|
||||
bool completed;
|
||||
|
||||
|
||||
/// <summary> Parameterless constructor for common functionality </summary>
|
||||
public CycleBgEnForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
ControlBox = false;
|
||||
|
||||
completed = false;
|
||||
StartForceCloseHandler();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="wmsCount">Number of text boxes for serial numbers</param>
|
||||
public CycleBgEnForm(IList<WaterMeter> waterMeters, int lineSize, string title, Sz sz, bool isLrOrder, string formCloseKeys,
|
||||
DEItem item1, DEItem item2, DEItem item3, IList<DEItem> colItems, bool isEnd = false)
|
||||
{
|
||||
InitializeComponent();
|
||||
ControlBox = false;
|
||||
|
||||
/// Arguments
|
||||
this.WaterMeters = waterMeters;
|
||||
this.lineSize = lineSize;
|
||||
this.Text = !string.IsNullOrEmpty(title) ? title : string.Empty;
|
||||
this.sz = sz;
|
||||
this.isLrOrder = isLrOrder;
|
||||
this.formCloseKeys = !string.IsNullOrEmpty(formCloseKeys) ? formCloseKeys : string.Empty;
|
||||
this.item1 = item1;
|
||||
this.item2 = item2;
|
||||
this.item3 = item3;
|
||||
this.colItems = colItems;
|
||||
this.isEnd = isEnd;
|
||||
|
||||
/// Readonly variables derived from arguments
|
||||
wmsCount = (waterMeters == null) ? 0 : waterMeters.Count;
|
||||
linesCount = (wmsCount + Math.Max(1, lineSize) - 1) / Math.Max(1, lineSize);
|
||||
firstValidWM = (waterMeters != null) ? waterMeters.FirstOrDefault(x => (x != null && !x.Disabled)) : null;
|
||||
///
|
||||
labels = new Label[wmsCount];
|
||||
comboBoxes = new ComboBox[colItems.Count, wmsCount];
|
||||
checkBoxes = new CheckBox[wmsCount];
|
||||
|
||||
/// Layout related readonly variables derived from argument 'sz'
|
||||
switch (sz)
|
||||
{
|
||||
case Sz.S:
|
||||
font = new Font("Verdana", 12, FontStyle.Regular);
|
||||
meterHeight = 26; /// Height of a ComboBox control
|
||||
margin = 20;
|
||||
spacing = 8;
|
||||
buttonsWidth = 350;
|
||||
hdrHeight = 120;
|
||||
labelWid = 31;
|
||||
checkBoxWid = 23;
|
||||
break;
|
||||
|
||||
default:
|
||||
case Sz.M:
|
||||
font = new Font("Verdana", 14, FontStyle.Regular);
|
||||
meterHeight = 31; /// Height of a ComboBox control
|
||||
margin = 25;
|
||||
spacing = 10;
|
||||
buttonsWidth = 350;
|
||||
hdrHeight = 140;
|
||||
labelWid = 34;
|
||||
checkBoxWid = 23;
|
||||
break;
|
||||
|
||||
case Sz.L:
|
||||
font = new Font("Verdana", 18, FontStyle.Regular);
|
||||
meterHeight = 37; /// Height of a ComboBox control
|
||||
margin = 30;
|
||||
spacing = 12;
|
||||
buttonsWidth = 350;
|
||||
hdrHeight = 160;
|
||||
labelWid = 40;
|
||||
checkBoxWid = 23;
|
||||
break;
|
||||
}
|
||||
meterWidth = labelWid + checkBoxWid + spacing;
|
||||
foreach (var ci in colItems) { meterWidth += ci.Width + spacing; }
|
||||
|
||||
int tabIndex = 1;
|
||||
|
||||
///
|
||||
/// Group box with common items
|
||||
///
|
||||
int groupBoxHeight = 0;
|
||||
int groupBoxWidth = 0;
|
||||
if (item1.Content != Content.None)
|
||||
{
|
||||
var groupBox = new GroupBox();
|
||||
|
||||
int label1Width = Convert.ToInt32(Graphics.FromImage(new Bitmap(1, 1)).MeasureString(item1.Caption, font).Width);
|
||||
var label1 = new Label
|
||||
{
|
||||
Text = item1.Caption,
|
||||
Font = font,
|
||||
Location = new Point(margin, 2 * spacing),
|
||||
Size = new Size(label1Width + spacing, meterHeight),
|
||||
TextAlign = ContentAlignment.MiddleLeft,
|
||||
TabIndex = tabIndex++,
|
||||
Parent = groupBox,
|
||||
};
|
||||
groupBox.Controls.Add(label1);
|
||||
int labelsWidth = label1Width;
|
||||
|
||||
if (item2.Content != Content.None)
|
||||
{
|
||||
int label2Width = Convert.ToInt32(Graphics.FromImage(new Bitmap(1, 1)).MeasureString(item2.Caption, font).Width);
|
||||
var label2 = new Label
|
||||
{
|
||||
Text = item2.Caption,
|
||||
Font = font,
|
||||
Location = new Point(margin, 3 * spacing + meterHeight),
|
||||
Size = new Size(label2Width + spacing, meterHeight),
|
||||
TextAlign = ContentAlignment.MiddleLeft,
|
||||
TabIndex = tabIndex++,
|
||||
Parent = groupBox,
|
||||
};
|
||||
groupBox.Controls.Add(label2);
|
||||
labelsWidth = Math.Max(labelsWidth, label2Width);
|
||||
|
||||
if (item3.Content != Content.None)
|
||||
{
|
||||
int label3Width = Convert.ToInt32(Graphics.FromImage(new Bitmap(1, 1)).MeasureString(item3.Caption, font).Width);
|
||||
var label3 = new Label
|
||||
{
|
||||
Text = item3.Caption,
|
||||
Font = font,
|
||||
Location = new Point(margin, 4 * spacing + 2 * meterHeight),
|
||||
Size = new Size(label3Width + spacing, meterHeight),
|
||||
TextAlign = ContentAlignment.MiddleLeft,
|
||||
TabIndex = tabIndex++,
|
||||
Parent = groupBox,
|
||||
};
|
||||
groupBox.Controls.Add(label3);
|
||||
labelsWidth = Math.Max(labelsWidth, label3Width);
|
||||
}
|
||||
}
|
||||
|
||||
comboBox1 = new ComboBox
|
||||
{
|
||||
Name = "-1~1",
|
||||
Location = new Point(margin + spacing + labelsWidth, 2 * spacing),
|
||||
Size = new Size(item1.Width, meterHeight),
|
||||
Enabled = (item1.Action != Action.LoadReadOnly),
|
||||
Font = font,
|
||||
TabIndex = tabIndex++,
|
||||
Parent = groupBox,
|
||||
};
|
||||
|
||||
comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
|
||||
comboBox1.TextChanged += new System.EventHandler(this.comboBox_TextChanged);
|
||||
comboBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.comboBox_KeyPress);
|
||||
|
||||
AddHistoryToCombo(comboBox1, isEnd ? Program.LocalSettings.LastEnComm1 : Program.LocalSettings.LastBgComm1);
|
||||
groupBox.Controls.Add(comboBox1);
|
||||
int combosWidth = item1.Width;
|
||||
|
||||
if (item2.Content != Content.None)
|
||||
{
|
||||
comboBox2 = new ComboBox
|
||||
{
|
||||
Name = "-1~2",
|
||||
Location = new Point(margin + spacing + labelsWidth, 3 * spacing + meterHeight),
|
||||
Size = new Size(item2.Width, meterHeight),
|
||||
Enabled = (item2.Action != Action.LoadReadOnly),
|
||||
Font = font,
|
||||
TabIndex = tabIndex++,
|
||||
Parent = groupBox,
|
||||
};
|
||||
|
||||
comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
|
||||
comboBox2.TextChanged += new System.EventHandler(this.comboBox_TextChanged);
|
||||
comboBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.comboBox_KeyPress);
|
||||
|
||||
AddHistoryToCombo(comboBox2, isEnd ? Program.LocalSettings.LastEnComm2 : Program.LocalSettings.LastBgComm2);
|
||||
groupBox.Controls.Add(comboBox2);
|
||||
combosWidth = Math.Max(combosWidth, item2.Width);
|
||||
|
||||
if (item3.Content != Content.None)
|
||||
{
|
||||
comboBox3 = new ComboBox
|
||||
{
|
||||
Name = "-1~3",
|
||||
Location = new Point(margin + spacing + labelsWidth, 4 * spacing + 2 * meterHeight),
|
||||
Size = new Size(item3.Width, meterHeight),
|
||||
Enabled = (item3.Action != Action.LoadReadOnly),
|
||||
Font = font,
|
||||
TabIndex = tabIndex++,
|
||||
Parent = groupBox,
|
||||
};
|
||||
|
||||
comboBox3.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
|
||||
comboBox3.TextChanged += new System.EventHandler(this.comboBox_TextChanged);
|
||||
comboBox3.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.comboBox_KeyPress);
|
||||
|
||||
AddHistoryToCombo(comboBox3, isEnd ? Program.LocalSettings.LastEnComm3 : Program.LocalSettings.LastBgComm3);
|
||||
groupBox.Controls.Add(comboBox3);
|
||||
combosWidth = Math.Max(combosWidth, item3.Width);
|
||||
}
|
||||
}
|
||||
|
||||
groupBox.Location = new Point(margin, margin - spacing);
|
||||
groupBoxWidth = labelsWidth + combosWidth + 2 * margin + spacing;
|
||||
groupBoxHeight = (item3.Content != Content.None) ? 3 * meterHeight + 5 * spacing
|
||||
: (item2.Content != Content.None) ? 2 * meterHeight + 4 * spacing
|
||||
: meterHeight + 3 * spacing;
|
||||
groupBox.Size = new Size(groupBoxWidth, groupBoxHeight);
|
||||
groupBox.Font = font;
|
||||
groupBox.TabIndex = tabIndex++;
|
||||
groupBox.Parent = this;
|
||||
this.Controls.Add(groupBox);
|
||||
}
|
||||
|
||||
///
|
||||
/// Table
|
||||
///
|
||||
int columnsTop = Math.Max(hdrHeight, groupBoxHeight + 2 * margin + spacing);
|
||||
for (int j = 0; j < linesCount; j++)
|
||||
{
|
||||
/// Captions
|
||||
int capX = margin + j * (meterWidth + margin / 2) + labelWid + spacing;
|
||||
int capY = columnsTop - meterHeight;
|
||||
for (int k = 0; k < colItems.Count; k++)
|
||||
{
|
||||
int labelWidth = Convert.ToInt32(Graphics.FromImage(new Bitmap(1, 1)).MeasureString(colItems[k].Caption, font).Width);
|
||||
var label = new Label
|
||||
{
|
||||
Text = colItems[k].Caption,
|
||||
Location = new Point(capX, capY),
|
||||
Size = new Size(labelWidth + spacing, meterHeight),
|
||||
Font = font,
|
||||
TabIndex = tabIndex++,
|
||||
Parent = this,
|
||||
};
|
||||
capX += colItems[k].Width + spacing;
|
||||
}
|
||||
|
||||
/// Water meters
|
||||
for (int i = 0; i < lineSize; i++)
|
||||
{
|
||||
int wmPos0 = i + lineSize * j;
|
||||
if (wmPos0 > wmsCount) continue;
|
||||
|
||||
int left = margin + j * (meterWidth + margin / 2);
|
||||
int top = columnsTop + i * (meterHeight + spacing);
|
||||
|
||||
var label = new Label
|
||||
{
|
||||
Text = (wmPos0 + 1).ToString(),
|
||||
Location = new Point(left, top),
|
||||
Size = new Size(labelWid, meterHeight),
|
||||
Font = font,
|
||||
TextAlign = ContentAlignment.MiddleLeft,
|
||||
TabIndex = tabIndex++,
|
||||
Parent = this,
|
||||
};
|
||||
left += labelWid + spacing / 2;
|
||||
labels[wmPos0] = label;
|
||||
this.Controls.Add(label);
|
||||
|
||||
/// Columns
|
||||
for (int k = 0; k < colItems.Count; k++)
|
||||
{
|
||||
DEItem ci = colItems[k];
|
||||
var comboBox = new ComboBox
|
||||
{
|
||||
Name = string.Format("{0}~{1}", k, wmPos0),
|
||||
Location = new Point(left, top),
|
||||
Size = new Size(ci.Width, meterHeight),
|
||||
Enabled = (ci.Action != Action.LoadReadOnly && !waterMeters[wmPos0].Disabled),
|
||||
Font = font,
|
||||
TabIndex = tabIndex++,
|
||||
Parent = this,
|
||||
};
|
||||
|
||||
comboBox.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
|
||||
comboBox.TextChanged += new System.EventHandler(this.comboBox_TextChanged);
|
||||
comboBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.comboBox_KeyPress);
|
||||
|
||||
var lastVals = GetHistoryFromLS(isEnd, k);
|
||||
comboBox.Items.Add(lastVals.Length > wmPos0 ? lastVals[wmPos0] : string.Empty); /// History in drop-down menu
|
||||
|
||||
left += ci.Width + spacing;
|
||||
comboBoxes[k, wmPos0] = comboBox;
|
||||
this.Controls.Add(comboBox);
|
||||
}
|
||||
|
||||
var checkBox = new CheckBox
|
||||
{
|
||||
Location = new Point(left, top + 6),
|
||||
Size = new Size(checkBoxWid, 23),
|
||||
TabIndex = tabIndex++,
|
||||
Parent = this,
|
||||
};
|
||||
checkBoxes[wmPos0] = checkBox;
|
||||
this.Controls.Add(checkBox);
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Adjust window size
|
||||
///
|
||||
Rectangle screenRectangle = this.RectangleToScreen(this.ClientRectangle);
|
||||
int titleBarHeight = screenRectangle.Top - this.Top;
|
||||
Size = new Size(Math.Max(meterWidth * linesCount + margin * (linesCount + 1), margin + groupBoxWidth + buttonsWidth),
|
||||
titleBarHeight + columnsTop + lineSize * (meterHeight + spacing) + margin);
|
||||
|
||||
Localize();
|
||||
InitializeBoxes();
|
||||
|
||||
/// Set focus to the first enabled ComboBox
|
||||
if (comboBox1 != null && comboBox1.Enabled)
|
||||
{
|
||||
ActiveControl = comboBox1;
|
||||
}
|
||||
else if (comboBox2 != null && comboBox2.Enabled)
|
||||
{
|
||||
ActiveControl = comboBox2;
|
||||
}
|
||||
else if (comboBox3 != null && comboBox3.Enabled)
|
||||
{
|
||||
ActiveControl = comboBox3;
|
||||
}
|
||||
else
|
||||
{
|
||||
/// Initialize k,j so that after the 1st increment k = 0 and j = 0
|
||||
int k, j;
|
||||
if (isLrOrder) { k = -1; j = 0; }
|
||||
else { k = 0; j = -1; }
|
||||
|
||||
/// Change focus
|
||||
if (FindNextEnabledCombo(comboBoxes, ref k, ref j, isLrOrder))
|
||||
{
|
||||
ActiveControl = okButton;
|
||||
}
|
||||
else
|
||||
{
|
||||
ActiveControl = comboBoxes[k, j];
|
||||
}
|
||||
}
|
||||
|
||||
completed = false;
|
||||
StartForceCloseHandler();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add combo box items from a history stored in a string array (obtained usually from LocalSettings)
|
||||
/// </summary>
|
||||
/// <param name="comboBox">ComboBox to prepare</param>
|
||||
/// <param name="history">String array with a history</param>
|
||||
void AddHistoryToCombo(ComboBox comboBox, string[] history)
|
||||
{
|
||||
if (history != null)
|
||||
{
|
||||
for (int i = 0; i < history.Length; i++) comboBox.Items.Add(history[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an array of strings from Program.LocalSettings.
|
||||
/// Never returns null, null is converted to new string[0].
|
||||
/// </summary>
|
||||
/// <param name="isEnd">false = Beginning of cycle, true = End of cycle</param>
|
||||
/// <param name="k">Column number (0-based)</param>
|
||||
/// <returns>Array of strings</returns>
|
||||
string[] GetHistoryFromLS(bool isEnd, int k)
|
||||
{
|
||||
if (isEnd)
|
||||
{
|
||||
switch (k)
|
||||
{
|
||||
default:
|
||||
case 0:
|
||||
if (Program.LocalSettings.LastEnColA == null) Program.LocalSettings.LastEnColA = new string[0];
|
||||
return Program.LocalSettings.LastEnColA;
|
||||
case 1:
|
||||
if (Program.LocalSettings.LastEnColB == null) Program.LocalSettings.LastEnColB = new string[0];
|
||||
return Program.LocalSettings.LastEnColB;
|
||||
case 2:
|
||||
if (Program.LocalSettings.LastEnColC == null) Program.LocalSettings.LastEnColC = new string[0];
|
||||
return Program.LocalSettings.LastEnColC;
|
||||
case 3:
|
||||
if (Program.LocalSettings.LastEnColD == null) Program.LocalSettings.LastEnColD = new string[0];
|
||||
return Program.LocalSettings.LastEnColD;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (k)
|
||||
{
|
||||
default:
|
||||
case 0:
|
||||
if (Program.LocalSettings.LastBgColA == null) Program.LocalSettings.LastBgColA = new string[0];
|
||||
return Program.LocalSettings.LastBgColA;
|
||||
case 1:
|
||||
if (Program.LocalSettings.LastBgColB == null) Program.LocalSettings.LastBgColB = new string[0];
|
||||
return Program.LocalSettings.LastBgColB;
|
||||
case 2:
|
||||
if (Program.LocalSettings.LastBgColC == null) Program.LocalSettings.LastBgColC = new string[0];
|
||||
return Program.LocalSettings.LastBgColC;
|
||||
case 3:
|
||||
if (Program.LocalSettings.LastBgColD == null) Program.LocalSettings.LastBgColD = new string[0];
|
||||
return Program.LocalSettings.LastBgColD;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Localize()
|
||||
{
|
||||
okButton.Text = Strings.OkBtnText;
|
||||
clearButton.Text = Strings.ClearBtnText;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize combo boxes state defined by related 'Action'.
|
||||
/// Clear check boxes.
|
||||
/// </summary>
|
||||
void InitializeBoxes()
|
||||
{
|
||||
isHandlersEnabled = false;
|
||||
|
||||
/// Common combo boxes
|
||||
var commCBs = new ComboBox[] { comboBox1, comboBox2, comboBox3 };
|
||||
int ix = 0;
|
||||
foreach (var commItem in new DEItem[] { item1, item2, item3 })
|
||||
{
|
||||
if (commItem.Content == Content.None) break;
|
||||
|
||||
switch (commItem.Action)
|
||||
{
|
||||
default:
|
||||
case Action.Clear:
|
||||
commCBs[ix].Text = string.Empty;
|
||||
break;
|
||||
|
||||
case Action.HistoryLast:
|
||||
commCBs[ix].Text = commCBs[ix].Items.Count > 0 ? commCBs[ix].Items[0].ToString() : string.Empty;
|
||||
break;
|
||||
|
||||
case Action.Load:
|
||||
case Action.LoadReadOnly:
|
||||
commCBs[ix].Text = GetContent(commItem.Content, firstValidWM);
|
||||
break;
|
||||
}
|
||||
|
||||
ix++;
|
||||
}
|
||||
|
||||
/// Table
|
||||
for (int k = 0; k < colItems.Count; k++)
|
||||
{
|
||||
switch (colItems[k].Action)
|
||||
{
|
||||
default:
|
||||
case Action.Clear:
|
||||
for (int i = 0; i < wmsCount; i++) comboBoxes[k, i].Text = string.Empty;
|
||||
break;
|
||||
|
||||
case Action.HistoryLast:
|
||||
for (int i = 0; i < wmsCount; i++)
|
||||
{
|
||||
comboBoxes[k, i].Text = comboBoxes[k, i].Items.Count > 0 ? comboBoxes[k, i].Items[0].ToString() : string.Empty;
|
||||
}
|
||||
break;
|
||||
|
||||
case Action.Load:
|
||||
case Action.LoadReadOnly:
|
||||
for (int i = 0; i < wmsCount; i++)
|
||||
{
|
||||
comboBoxes[k, i].Text = GetContent(colItems[k].Content, WaterMeters[i]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < wmsCount; i++)
|
||||
{
|
||||
checkBoxes[i].Checked = isEnd ? (WaterMeters[i] != null && !WaterMeters[i].Disabled) : false;
|
||||
}
|
||||
|
||||
isHandlersEnabled = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When one combo box is updated using drop-down menu, all combo boxes
|
||||
/// are updated by this function.
|
||||
/// </summary>
|
||||
void UpdateWMsFromBoxes()
|
||||
{
|
||||
if (item1.Content != Content.None && firstValidWM != null)
|
||||
{
|
||||
PutContent(item1.Content, firstValidWM, comboBox1.Text);
|
||||
if (item1.Action != Action.LoadReadOnly)
|
||||
{
|
||||
if (isEnd) Program.LocalSettings.UpdateHistory(comboBox1.Text, ref Program.LocalSettings.LastEnComm1);
|
||||
else Program.LocalSettings.UpdateHistory(comboBox1.Text, ref Program.LocalSettings.LastBgComm1);
|
||||
}
|
||||
|
||||
if (item2.Content != Content.None)
|
||||
{
|
||||
PutContent(item2.Content, firstValidWM, comboBox2.Text);
|
||||
if (item2.Action != Action.LoadReadOnly)
|
||||
{
|
||||
if (isEnd) Program.LocalSettings.UpdateHistory(comboBox2.Text, ref Program.LocalSettings.LastEnComm2);
|
||||
else Program.LocalSettings.UpdateHistory(comboBox2.Text, ref Program.LocalSettings.LastBgComm2);
|
||||
}
|
||||
|
||||
if (item3.Content != Content.None)
|
||||
{
|
||||
PutContent(item3.Content, firstValidWM, comboBox3.Text);
|
||||
if (item3.Action != Action.LoadReadOnly)
|
||||
{
|
||||
if (isEnd) Program.LocalSettings.UpdateHistory(comboBox3.Text, ref Program.LocalSettings.LastEnComm3);
|
||||
else Program.LocalSettings.UpdateHistory(comboBox3.Text, ref Program.LocalSettings.LastBgComm3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Propagate common (batch) values to water meters in case of
|
||||
/// Content.BatchAndWmOrder, Content.BatchAndWmRemark, Content.YearOfCalibration and Content.ArchivePath
|
||||
///
|
||||
if (item1.Content == Content.BatchAndWmOrder ||
|
||||
(item1.Content != Content.None && item2.Content == Content.BatchAndWmOrder) ||
|
||||
(item1.Content != Content.None && item2.Content != Content.None && item2.Content == Content.BatchAndWmOrder))
|
||||
{
|
||||
foreach (var wm in WaterMeters) if (wm != null && !wm.Disabled) wm.PurchaseOrder = firstValidWM.Batch.PurchaseOrder;
|
||||
}
|
||||
else if (item1.Content == Content.BatchAndWmRemark ||
|
||||
(item1.Content != Content.None && item2.Content == Content.BatchAndWmRemark) ||
|
||||
(item1.Content != Content.None && item2.Content != Content.None && item2.Content == Content.BatchAndWmRemark))
|
||||
{
|
||||
foreach (var wm in WaterMeters) if (wm != null && !wm.Disabled) wm.Remark = firstValidWM.Batch.Remark;
|
||||
}
|
||||
else if (item1.Content == Content.YearOfCalibration ||
|
||||
(item1.Content != Content.None && item2.Content == Content.YearOfCalibration) ||
|
||||
(item1.Content != Content.None && item2.Content != Content.None && item2.Content == Content.YearOfCalibration))
|
||||
{
|
||||
foreach (var wm in WaterMeters) if (wm != null && !wm.Disabled) wm.YearOfProduction = firstValidWM.YearOfProduction;
|
||||
}
|
||||
else if (item1.Content == Content.ArchivePath ||
|
||||
(item1.Content != Content.None && item2.Content == Content.ArchivePath) ||
|
||||
(item1.Content != Content.None && item2.Content != Content.None && item2.Content == Content.ArchivePath))
|
||||
{
|
||||
foreach (var wm in WaterMeters) if (wm != null && !wm.Disabled) wm.ArchivePath = firstValidWM.ArchivePath;
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 0; k < colItems.Count; k++)
|
||||
{
|
||||
/// Store values to water meters and prepare an array for a history in the LocalSettings
|
||||
var currentVals = new string[wmsCount];
|
||||
for (int i = 0; i < wmsCount; i++)
|
||||
{
|
||||
PutContent(colItems[k].Content, WaterMeters[i], comboBoxes[k, i].Text);
|
||||
currentVals[i] = comboBoxes[k, i].Text;
|
||||
}
|
||||
|
||||
/// Update history
|
||||
if (colItems[k].Action != Action.LoadReadOnly)
|
||||
{
|
||||
if (isEnd)
|
||||
{
|
||||
switch (k)
|
||||
{
|
||||
default:
|
||||
case 0: Program.LocalSettings.LastEnColA = currentVals; break;
|
||||
case 1: Program.LocalSettings.LastEnColB = currentVals; break;
|
||||
case 2: Program.LocalSettings.LastEnColC = currentVals; break;
|
||||
case 3: Program.LocalSettings.LastEnColD = currentVals; break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (k)
|
||||
{
|
||||
default:
|
||||
case 0: Program.LocalSettings.LastBgColA = currentVals; break;
|
||||
case 1: Program.LocalSettings.LastBgColB = currentVals; break;
|
||||
case 2: Program.LocalSettings.LastBgColC = currentVals; break;
|
||||
case 3: Program.LocalSettings.LastBgColD = currentVals; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < wmsCount; i++)
|
||||
{
|
||||
if (WaterMeters[i] != null) WaterMeters[i].Disabled = !checkBoxes[i].Checked;
|
||||
}
|
||||
}
|
||||
|
||||
string GetContent(Content content, WaterMeter wm)
|
||||
{
|
||||
if (wm == null) return string.Empty;
|
||||
|
||||
switch (content)
|
||||
{
|
||||
default:
|
||||
case Content.None: return string.Empty;
|
||||
case Content.SerialNr: return (!wm.Disabled && wm.SerialNr != null) ? wm.SerialNr : string.Empty;
|
||||
case Content.SerialNrAux: return (!wm.Disabled && wm.SerialNrAux != null) ? wm.SerialNrAux : string.Empty;
|
||||
case Content.RadioAddress: return (!wm.Disabled && wm.RadioAddress != null) ? wm.RadioAddress : string.Empty;
|
||||
case Content.YearOfCalibration: return (!wm.Disabled) ? wm.YearOfProduction.ToString() : string.Empty;
|
||||
case Content.StartState: return (!wm.Disabled && wm.GetStartState() != null) ? wm.GetStartState() : string.Empty;
|
||||
case Content.EndState: return (!wm.Disabled && wm.EndState != null) ? wm.EndState : string.Empty;
|
||||
case Content.ArchivePath: return (!wm.Disabled && wm.ArchivePath != null) ? wm.ArchivePath : string.Empty;
|
||||
|
||||
case Content.WmOrder: return (!wm.Disabled && wm.PurchaseOrder != null) ? wm.PurchaseOrder : string.Empty;
|
||||
case Content.BatchOrder: return (wm.Batch != null && wm.Batch.PurchaseOrder != null) ? wm.Batch.PurchaseOrder : string.Empty;
|
||||
case Content.BatchAndWmOrder: return (wm.Batch != null && wm.Batch.PurchaseOrder != null) ? wm.Batch.PurchaseOrder : string.Empty;
|
||||
|
||||
case Content.WmRemark: return (!wm.Disabled && wm.Remark != null) ? wm.Remark : string.Empty;
|
||||
case Content.BatchRemark: return (wm.Batch != null && wm.Batch.Remark != null) ? wm.Batch.Remark : string.Empty;
|
||||
case Content.BatchAndWmRemark: return (wm.Batch != null && wm.Batch.Remark != null) ? wm.Batch.Remark : string.Empty;
|
||||
#if ORACLE_DB
|
||||
case Content.Prefix: return (!wm.Disabled && wm.Prefix != null) ? wm.Prefix : string.Empty;
|
||||
case Content.Suffix: return (!wm.Disabled && wm.Suffix != null) ? wm.Suffix : string.Empty;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void PutContent(Content content, WaterMeter wm, string value)
|
||||
{
|
||||
if (wm == null || wm.Disabled) return;
|
||||
|
||||
int year;
|
||||
|
||||
switch (content)
|
||||
{
|
||||
default:
|
||||
case Content.None: return;
|
||||
case Content.SerialNr: wm.SerialNr = value; return;
|
||||
case Content.SerialNrAux: wm.SerialNrAux = value; return;
|
||||
case Content.RadioAddress: wm.RadioAddress = value; return;
|
||||
case Content.YearOfCalibration: if (int.TryParse(value, out year)) wm.YearOfProduction = year; return;
|
||||
case Content.StartState: wm.SetStartState(value); return;
|
||||
case Content.EndState: wm.EndState = value; return;
|
||||
case Content.ArchivePath: wm.ArchivePath = value; return;
|
||||
|
||||
case Content.WmOrder:
|
||||
wm.PurchaseOrder = value;
|
||||
return;
|
||||
|
||||
case Content.BatchOrder:
|
||||
case Content.BatchAndWmOrder:
|
||||
if (wm.Batch != null) wm.Batch.PurchaseOrder = value;
|
||||
return;
|
||||
|
||||
case Content.WmRemark:
|
||||
wm.Remark = value;
|
||||
return;
|
||||
|
||||
case Content.BatchRemark:
|
||||
case Content.BatchAndWmRemark:
|
||||
if (wm.Batch != null) wm.Batch.Remark = value;
|
||||
return;
|
||||
#if ORACLE_DB
|
||||
case Content.Prefix: wm.Prefix = value; return;
|
||||
case Content.Suffix: wm.Suffix = value; return;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void clearButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
InitializeBoxes();
|
||||
}
|
||||
|
||||
private void okButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
UpdateWMsFromBoxes();
|
||||
|
||||
completed = true;
|
||||
Close();
|
||||
}
|
||||
private void comboBox_SelectedIndexChanged(object sndr, EventArgs e)
|
||||
{
|
||||
if (!isHandlersEnabled) return;
|
||||
|
||||
string[] kj = (sndr as ComboBox).Name.Split(new char[] { '~' });
|
||||
int k = int.Parse(kj[0]);
|
||||
int j = int.Parse(kj[1]);
|
||||
|
||||
isHandlersEnabled = false;
|
||||
|
||||
if (k >= 0 && comboBoxes[k, j].Text == comboBoxes[k, j].Items[0].ToString())
|
||||
{
|
||||
for (int i = 0; i < wmsCount; i++)
|
||||
{
|
||||
comboBoxes[k, i].Text = comboBoxes[k, i].Items[0].ToString();
|
||||
if (!string.IsNullOrEmpty(comboBoxes[k, i].Text)) checkBoxes[i].Checked = true;
|
||||
}
|
||||
}
|
||||
|
||||
isHandlersEnabled = true;
|
||||
}
|
||||
|
||||
private void comboBox_KeyPress(object sndr, KeyPressEventArgs e)
|
||||
{
|
||||
if (!isHandlersEnabled) return;
|
||||
|
||||
string[] kj = (sndr as ComboBox).Name.Split(new char[] { '~' });
|
||||
int k = int.Parse(kj[0]);
|
||||
int j = int.Parse(kj[1]);
|
||||
|
||||
/// Close the form if any 'form close key' was pressed
|
||||
for (int ix = 0; ix < formCloseKeys.Length; ix++)
|
||||
{
|
||||
if (e.KeyChar == formCloseKeys[ix])
|
||||
{
|
||||
okButton_Click(sndr, e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (e.KeyChar == '\r')
|
||||
{
|
||||
if (k < 0)
|
||||
{
|
||||
if (j == 1 && comboBox2 != null)
|
||||
{
|
||||
comboBox2.Focus();
|
||||
return;
|
||||
}
|
||||
else if (j == 2 && comboBox3 != null)
|
||||
{
|
||||
comboBox3.Focus();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
/// Initialize k,j so that after the 1st increment k = 0 and j = 0
|
||||
if (isLrOrder) { k = -1; j = 0; }
|
||||
else { k = 0; j = -1; }
|
||||
}
|
||||
}
|
||||
|
||||
/// Change focus
|
||||
if (FindNextEnabledCombo(comboBoxes, ref k, ref j, isLrOrder))
|
||||
{
|
||||
okButton.Focus();
|
||||
}
|
||||
else
|
||||
{
|
||||
comboBoxes[k, j].Focus();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void comboBox_TextChanged(object sndr, EventArgs e)
|
||||
{
|
||||
if (!isHandlersEnabled) return;
|
||||
|
||||
string[] kj = (sndr as ComboBox).Name.Split(new char[] { '~' });
|
||||
int k = int.Parse(kj[0]);
|
||||
int j = int.Parse(kj[1]);
|
||||
|
||||
if (k >= 0) checkBoxes[j].Checked = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find the next enabled combo box, update k and i.
|
||||
/// Return true (=pastTheEndOfArray) when there is no such next combo box.
|
||||
/// </summary>
|
||||
/// <param name="comboBoxes">2-dimensional array of combo boxes</param>
|
||||
/// <param name="k">0-nased column </param>
|
||||
/// <param name="i">0-based row</param>
|
||||
/// <param name="isLrOrder">Order of progress to the next combo: true = left-right, false = top-down</param>
|
||||
/// <returns>true (=pastTheEndOfArray) when there is no such next combo box, otherwise false</returns>
|
||||
bool FindNextEnabledCombo(ComboBox[,] comboBoxes, ref int k, ref int i, bool isLrOrder)
|
||||
{
|
||||
bool pastTheEndOfArray = false;
|
||||
do
|
||||
{
|
||||
if (isLrOrder)
|
||||
{
|
||||
if (++k == comboBoxes.GetLength(0))
|
||||
{
|
||||
k = 0;
|
||||
if (++i == comboBoxes.GetLength(1))
|
||||
{
|
||||
i = 0;
|
||||
pastTheEndOfArray = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (++i == comboBoxes.GetLength(1))
|
||||
{
|
||||
i = 0;
|
||||
if (++k == comboBoxes.GetLength(0))
|
||||
{
|
||||
k = 0;
|
||||
pastTheEndOfArray = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
while (!comboBoxes[k, i].Enabled);
|
||||
|
||||
return pastTheEndOfArray;
|
||||
}
|
||||
|
||||
#region Forced close handling
|
||||
|
||||
public void StartForceCloseHandler()
|
||||
{
|
||||
UiBridge.Bridge.CloseModelessFormHandler += delegate(object sender, EventArgs args)
|
||||
{
|
||||
if (InvokeRequired) { Invoke(new EventHandler<EventArgs>(OnForceClose), sender, args); }
|
||||
else OnForceClose(sender, args);
|
||||
};
|
||||
}
|
||||
|
||||
private void OnForceClose(object sender, EventArgs args)
|
||||
{
|
||||
DialogResult = DialogResult.Cancel;
|
||||
Close();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user