SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
AddEditBlock.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Data;
6 using System.Drawing;
7 using System.Text;
8 using System.Windows.Forms;
9 
10 namespace SeciUserInterface.Dialogs.Blocks
11 {
12 
13  public partial class AddEditBlock : Form
14  {
17  public Boolean IsComponent = false;
18  private Boolean _isEdit;
19 
20  public AddEditBlock()
21  {
22  InitializeComponent();
23  }
24 
25  private void AddBlock_Load(object sender, EventArgs e)
26  {
27  try
28  {
29  if (Block == null)
30  {
31  _isEdit = false;
32 
33  //Add
34  fillInVIs();
35  txtLower.Text = "-Infinity";
36  txtUpper.Text = "Infinity";
37  comFormatting.SelectedIndex = 0;
38  }
39  else
40  {
41  _isEdit = true;
42  this.Text = "SECI - Edit Block";
43 
44  //Edit
45  fillInVIs();
46  txtBlockName.Text = Block.BlockName;
47  txtUnits.Text = Block.BlockUnits;
48  txtAlias.Text = Block.Alias;
49 
50  txtLogRate.Text = Block.LoggingRate.ToString();
51 
52  comVI.SelectedItem = Block.ParentVI.ToLower();
53  comRead.SelectedItem = Block.ReadControl;
54 
55  chkEnabled.Checked = Block.BlockEnabled;
56  chkVisible.Checked = Block.Visible;
57 
58  if (Block.ApplyFormatting)
59  {
60  comFormatting.SelectedIndex = 0;
61  }
62  else if (Block.ApplyFixedFormatting)
63  {
64  comFormatting.SelectedIndex = 1;
65  }
66  else
67  {
68  comFormatting.SelectedIndex = 2;
69  }
70 
71  txtDecPlaces.Text = Block.NumberDecimalPlaces.ToString();
72 
73  //Logging
74  if (Block.LogValueToFile)
75  {
76  radLog.Checked = true;
77  if (Block.LogChangesTolerance)
78  {
79  chkOnlyIfChanged.Checked = true;
80  txtLogTolerance.Text = Block.Tolerance.ToString();
81  }
82  else
83  {
84  chkOnlyIfChanged.Checked = false;
85  }
86 
87  if (Block.LogChangesOnly)
88  {
89  radOnlyChangesTol.Checked = true;
90  txtTolerance.Text = Block.Tolerance.ToString();
91  }
92  }
93  else
94  {
95  //No logging
96  radNoLogging.Checked = true;
97  }
98 
99  chkLogToSingleFile.Checked = Block.LogToSharedFile;
100 
101  if (Block.WriteControl != null)
102  {
103  comWrite.SelectedItem = Block.WriteControl;
104  }
105 
106  if (Block.GoButton != null)
107  {
108  comGo.SelectedItem = Block.GoButton;
109  }
110 
111  if (Block.WaitForControl != null)
112  {
113  comWaitfor.SelectedItem = Block.WaitForControl;
114  }
115 
116  //if (Block.UnderRunControl)
117  //{
118  chkRuncontrol.Checked = Block.UnderRunControl; //true;
119  Seci.Definitions.ControlType type = Seci.Managers.LabViewMgr.GetControlType(comVI.SelectedItem.ToString(), comRead.SelectedItem.ToString());
120  if (type == Seci.Definitions.ControlType.BOOLEAN || type == Seci.Definitions.ControlType.STRING)
121  {
122  txtRCValue.Text = Block.RunControlValue;
123  }
124  else
125  {
126  txtUpper.Text = Block.UpperLimit.ToString();
127  txtLower.Text = Block.LowerLimit.ToString();
128  }
129  //}
130 
131  chkSaveSettings.Checked = Block.SaveSettings;
132 
133  if (IsComponent)
134  {
135  txtBlockName.ReadOnly = true;
136  }
137  }
138 
139  if (Seci.Definitions.Status.LogAllBlocksToSingleFile)
140  {
141  chkLogToSingleFile.Checked = true;
142  chkLogToSingleFile.Enabled = false;
143  }
144 
145  }
146  catch (Exception err)
147  {
148  MessageBox.Show(err.Message);
149  }
150  }
151 
152  private void fillInVIs()
153  {
154  //Get list of VI's
155  Seci.SerialisableList<Seci.LabView.LabViewPanelInfo> panels = Seci.Managers.LabViewMgr.GetAllPanelsInfo();
156 
157  for (int i = 0; i < panels.Count; ++i)
158  {
159  comVI.Items.Add(panels[i].FilePath.ToLower());
160  }
161 
162  //Add DAE
163  comVI.Items.Add(Seci.Standard.Dae.FilePath.ToLower());
164 
165  //Add Beam Logger
166  comVI.Items.Add(Seci.Standard.BeamLogger.FilePath.ToLower());
167  }
168 
169  private void fillInControls(String VIname)
170  {
171  try
172  {
173  String[] controls = Seci.Managers.LabViewMgr.GetVIControls(VIname).ToArray();
174  //Read
175  comRead.Items.Clear();
176  comRead.Items.AddRange(controls);
177  comRead.SelectedIndex = -1;
178 
179  //Write
180  comWrite.Items.Clear();
181  comWrite.Items.AddRange(controls);
182  comWrite.SelectedIndex = -1;
183 
184  //Go
185  comGo.Items.Clear();
186  comGo.Items.AddRange(controls);
187  comGo.SelectedIndex = -1;
188 
189  //Waitfor
190  comWaitfor.Items.Clear();
191  comWaitfor.Items.AddRange(controls);
192  comWaitfor.SelectedIndex = -1;
193  }
194  catch (Exception err)
195  {
196  MessageBox.Show(err.Message);
197  }
198  }
199 
200  private void comVI_SelectedIndexChanged(object sender, EventArgs e)
201  {
202  if (comVI.SelectedIndex != -1)
203  {
204  fillInControls(comVI.SelectedItem.ToString());
205  }
206  }
207 
208  private void btnClearWrite_Click(object sender, EventArgs e)
209  {
210  comWrite.SelectedIndex = -1;
211  }
212 
213  private void btnClearGo_Click(object sender, EventArgs e)
214  {
215  comGo.SelectedIndex = -1;
216  }
217 
218  private void btnClearWaitfor_Click(object sender, EventArgs e)
219  {
220  comWaitfor.SelectedIndex = -1;
221  }
222 
223  private void btnOK_Click(object sender, EventArgs e)
224  {
225  //Check that the required options are chosen
226  if (checkFilledOutCorrectly())
227  {
229 
230  //Always present
231  newBlock.BlockName = txtBlockName.Text;
232  newBlock.BlockUnits = txtUnits.Text;
233  newBlock.ParentVI = comVI.SelectedItem.ToString();
234  newBlock.Alias = txtAlias.Text;
235 
236  newBlock.ReadControl = comRead.SelectedItem.ToString();
237  newBlock.LoggingRate = Convert.ToSingle(txtLogRate.Text);
238 
239  newBlock.BlockEnabled = chkEnabled.Checked;
240  newBlock.Visible = chkVisible.Checked;
241 
242  if (comFormatting.SelectedIndex == 0)
243  {
244  newBlock.ApplyFormatting = true;
245  newBlock.ApplyFixedFormatting = false;
246  newBlock.NumberDecimalPlaces = Int32.Parse(txtDecPlaces.Text);
247  }
248  else if (comFormatting.SelectedIndex == 1)
249  {
250  newBlock.ApplyFormatting = false;
251  newBlock.ApplyFixedFormatting = true;
252  newBlock.NumberDecimalPlaces = Int32.Parse(txtDecPlaces.Text);
253  }
254  else
255  {
256  newBlock.ApplyFormatting = false;
257  newBlock.ApplyFixedFormatting = false;
258  newBlock.NumberDecimalPlaces = Int32.Parse(txtDecPlaces.Text);
259  }
260 
261  newBlock.ReadType = Seci.Managers.LabViewMgr.GetControlType(comVI.SelectedItem.ToString(), comRead.SelectedItem.ToString());
262 
263  //Logging
264  newBlock.LoggingRate = Convert.ToSingle(txtLogRate.Text);
265 
266  if (radLog.Checked)
267  {
268  newBlock.LogValueToFile = true;
269  if (chkOnlyIfChanged.Checked == true)
270  {
271  newBlock.LogChangesTolerance = true;
272  newBlock.Tolerance = Convert.ToDouble(txtLogTolerance.Text);
273  newBlock.LogChangesOnly = false;
274  }
275  else
276  {
277  newBlock.LogChangesTolerance = false;
278  newBlock.LogChangesOnly = false;
279  }
280  }
281  else if (radOnlyChangesTol.Checked)
282  {
283  newBlock.LogValueToFile = true;
284  newBlock.LogChangesTolerance = true;
285  newBlock.LogChangesOnly = true;
286  newBlock.Tolerance = Convert.ToDouble(txtTolerance.Text);
287  }
288  else
289  {
290  //No logging
291  newBlock.LogChangesOnly = false;
292  newBlock.LogValueToFile = false;
293  newBlock.LogChangesTolerance = false;
294  newBlock.Tolerance = 0.0;
295  }
296 
297  if (newBlock.ReadType == Seci.Definitions.ControlType.NUMERIC)
298  {
299  newBlock.SetRunControlLimits(chkRuncontrol.Checked, Convert.ToDouble(txtLower.Text), Convert.ToDouble(txtUpper.Text));
300  }
301  else if (newBlock.ReadType == Seci.Definitions.ControlType.BOOLEAN || newBlock.ReadType == Seci.Definitions.ControlType.STRING)
302  {
303  newBlock.SetRunControlLimits(chkRuncontrol.Checked, txtRCValue.Text);
304  }
305  else
306  {
307  newBlock.SetRunControlLimits(chkRuncontrol.Checked, Convert.ToDouble(txtLower.Text), Convert.ToDouble(txtUpper.Text));
308  }
309 
310  newBlock.SaveSettings = chkSaveSettings.Checked;
311 
312  newBlock.LogToSharedFile = chkLogToSingleFile.Checked;
313 
314  if (_isEdit)
315  {
316  //Copy the group
317  newBlock.Group = Block.Group;
318  }
319 
320  if (comWrite.SelectedIndex != -1)
321  {
322  newBlock.WriteControl = comWrite.SelectedItem.ToString();
323  }
324 
325  if (comGo.SelectedIndex != -1)
326  {
327  newBlock.GoButton = comGo.SelectedItem.ToString();
328  }
329 
330  if (comWaitfor.SelectedIndex != -1)
331  {
332  newBlock.WaitForControl = comWaitfor.SelectedItem.ToString();
333  }
334 
335  if (!_isEdit)
336  {
337  try
338  {
339  if (newBlock.BlockName.ToLower() == Seci.Definitions.Status.StatusBlockName.ToLower())
340  {
341  MessageBox.Show("Block name is reserved. Please, modify the Block name.", "Block name is not valid", MessageBoxButtons.OK, MessageBoxIcon.Error);
342  return;
343  }
344 
345  if (Dictionary.Contains(newBlock.BlockName))
346  {
347  MessageBox.Show("Block name already exists. Please, modify the Block name.", "Block name is not unique", MessageBoxButtons.OK, MessageBoxIcon.Error);
348  return;
349  }
350 
351  if (!String.IsNullOrEmpty(newBlock.Alias) && !Dictionary.IsAliasUnique(newBlock.Alias))
352  {
353  MessageBox.Show("Alias is not unique. Please, modify the alias.", "Alias is not unique", MessageBoxButtons.OK, MessageBoxIcon.Error);
354  return;
355  }
356 
357  //Add to tempblocks
358  Dictionary.Add(newBlock.BlockName, newBlock);
359  }
360  catch (Exception err)
361  {
362  MessageBox.Show(err.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
363  return;
364  }
365  }
366  else
367  {
368  //Replace the old block - remove then add (in the same place) because the name may have changed
369  //which would change the key
370  try
371  {
372  //Copy over the owning sub-config
373  newBlock.OwningComponent = Block.OwningComponent;
374  Dictionary.Replace(Block.BlockName, newBlock.BlockName, newBlock);
375  }
376  catch (Exception err)
377  {
378  MessageBox.Show(err.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
379  return;
380  }
381  }
382 
383  DialogResult = DialogResult.OK;
384  Close();
385  }
386 
387  }
388 
389  private Boolean checkFilledOutCorrectly()
390  {
391  List<String> filledList = checkFilledOutandValid();
392 
393  if (filledList.Count > 0)
394  {
395  String message = "The following entries are invalid:";
396 
397  for (int i = 0; i < filledList.Count; ++i)
398  {
399  message = message + Environment.NewLine + filledList[i];
400  }
401 
402  MessageBox.Show(message, "Invalid Entries!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
403 
404  return false;
405  }
406  else
407  {
408  return true;
409  }
410 
411  }
412 
413  private List<String> checkFilledOutandValid()
414  {
415  List<String> list = new List<String>();
416 
417  Double dummy; //Used for testing for valid doubles
418 
419  if (txtBlockName.Text == "")
420  {
421  list.Add(" - No block name entered.");
422  }
423 
424  if (comVI.SelectedIndex == -1)
425  {
426  list.Add(" - No VI selected.");
427  }
428 
429  if (comRead.SelectedIndex == -1)
430  {
431  list.Add(" - No read control selected.");
432  }
433 
434  if (chkRuncontrol.Checked)
435  {
436  Seci.Definitions.ControlType type = Seci.Managers.LabViewMgr.GetControlType(comVI.SelectedItem.ToString(), comRead.SelectedItem.ToString());
437  if (type == Seci.Definitions.ControlType.BOOLEAN || type == Seci.Definitions.ControlType.STRING)
438  {
439  if (txtRCValue.Text.Trim() == "")
440  {
441  list.Add(" - No value entered for run-control.");
442  }
443  }
444  else
445  {
446  if (txtUpper.Text.Trim() == "")
447  {
448  list.Add(" - No upper limit entered for run-control.");
449  }
450  else
451  {
452  txtUpper.Text = ConfigureBlocks.CheckForInfinity(txtUpper.Text);
453 
454  if (!Double.TryParse(txtUpper.Text, out dummy))
455  {
456  list.Add(" - upper limit value not valid.");
457  }
458  }
459 
460  if (txtLower.Text.Trim() == "")
461  {
462  list.Add(" - No lower limit entered for run-control.");
463  }
464  else
465  {
466  txtLower.Text = ConfigureBlocks.CheckForInfinity(txtLower.Text);
467 
468  if (!Double.TryParse(txtLower.Text, out dummy))
469  {
470  list.Add(" - lower limit value not valid.");
471  }
472  }
473  }
474  }
475  else
476  {
477  if ((txtUpper.Text == "") || (!Double.TryParse(txtUpper.Text, out dummy)))
478  {
479  txtUpper.Text = "0";
480  }
481 
482  if ((txtLower.Text == "") || (!Double.TryParse(txtLower.Text, out dummy)))
483  {
484  txtLower.Text = "0";
485  }
486  }
487 
488  if (radLog.Checked)
489  {
490 
491  if (txtLogRate.Text == "")
492  {
493  list.Add(" - No log rate entered.");
494  }
495  else
496  {
497  if (!Double.TryParse(txtLogRate.Text, out dummy))
498  {
499  list.Add(" - log-rate value not valid.");
500  }
501  else
502  {
503  if (dummy < 5)
504  {
505  list.Add(" - log-rate value must be 5 seconds or higher.");
506  }
507  }
508  }
509  }
510  else if (radOnlyChangesTol.Checked)
511  {
512  if (txtTolerance.Text == "")
513  {
514  list.Add(" - No tolerance entered.");
515  }
516  else
517  {
518  if (!Double.TryParse(txtTolerance.Text, out dummy))
519  {
520  list.Add(" - tolerance value not valid.");
521  }
522  }
523  }
524 
525  int decplaces;
526 
527  if (!int.TryParse(txtDecPlaces.Text, out decplaces) || decplaces < 0)
528  {
529  list.Add(" - number of decimal places not valid.");
530  }
531 
532  return list;
533  }
534 
535  //Check that key presses are numeric
536  private void NumericOnly_KeyPress(object sender, KeyPressEventArgs e)
537  {
538  if (!isNumeric(e))
539  {
540  e.Handled = true;
541  }
542  }
543 
544  //Check that key presses are numeric or letter or underscore
545  //Anything else may cause a problem with Open GENIE
546  private void isCharValid_KeyPress(object sender, KeyPressEventArgs e)
547  {
548  if (!isCharValid(e))
549  {
550  e.Handled = true;
551  }
552  }
553 
554  private Boolean isNumeric(KeyPressEventArgs e)
555  {
556  // \b is backspace/delete
557  if (Char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == '\b')
558  {
559  return true;
560  }
561  else
562  {
563  return false;
564  }
565  }
566 
567  private Boolean isCharValid(KeyPressEventArgs e)
568  {
569  // \b is backspace/delete
570  if (Char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == '_' || e.KeyChar == '\b')
571  {
572  return true;
573  }
574  else
575  {
576  return false;
577  }
578  }
579 
580  private void chkRuncontrol_CheckedChanged(object sender, EventArgs e)
581  {
582  if (chkRuncontrol.Checked)
583  {
584  txtUpper.Enabled = true;
585  txtLower.Enabled = true;
586  txtRCValue.Enabled = true;
587  }
588  else
589  {
590  txtUpper.Enabled = false;
591  txtLower.Enabled = false;
592  txtRCValue.Enabled = false;
593  }
594  }
595 
596  private void radLog_CheckedChanged(object sender, EventArgs e)
597  {
598  chkOnlyIfChanged.Enabled = radLog.Checked;
599  txtLogRate.Enabled = radLog.Checked;
600  txtLogTolerance.Enabled = radLog.Checked && chkOnlyIfChanged.Checked;
601  }
602 
603  private void chkOnlyIfChanged_CheckedChanged(object sender, EventArgs e)
604  {
605  txtLogTolerance.Enabled = chkOnlyIfChanged.Checked;
606  }
607 
608  private void radOnlyChangesTol_CheckedChanged(object sender, EventArgs e)
609  {
610  txtTolerance.Enabled = radOnlyChangesTol.Checked;
611  }
612 
613  private void comRead_SelectedIndexChanged(object sender, EventArgs e)
614  {
615  Seci.Definitions.ControlType type = Seci.Managers.LabViewMgr.GetControlType(comVI.SelectedItem.ToString(), comRead.SelectedItem.ToString());
616 
617  if (type == Seci.Definitions.ControlType.NUMERIC)
618  {
619  txtUpper.Visible = true;
620  txtLower.Visible = true;
621  lblRCLower.Visible = true;
622  lblRCUpper.Visible = true;
623  lblRCValue.Visible = false;
624  txtRCValue.Visible = false;
625  }
626  else if (type == Seci.Definitions.ControlType.STRING || type == Seci.Definitions.ControlType.BOOLEAN)
627  {
628  txtUpper.Visible = false;
629  txtLower.Visible = false;
630  lblRCLower.Visible = false;
631  lblRCUpper.Visible = false;
632  lblRCValue.Visible = true;
633  txtRCValue.Visible = true;
634  }
635  else
636  {
637  txtUpper.Visible = true;
638  txtLower.Visible = true;
639  lblRCLower.Visible = true;
640  lblRCUpper.Visible = true;
641  lblRCValue.Visible = false;
642  txtRCValue.Visible = false;
643  }
644  }
645  }
646 }
void btnClearWrite_Click(object sender, EventArgs e)
This class is used for storing the information for any blocks that are created and provides methods f...
Definition: BlockInfo.cs:18
Boolean isCharValid(KeyPressEventArgs e)
void btnOK_Click(object sender, EventArgs e)
void AddBlock_Load(object sender, EventArgs e)
Definition: AddEditBlock.cs:25
void comRead_SelectedIndexChanged(object sender, EventArgs e)
Boolean isNumeric(KeyPressEventArgs e)
void radOnlyChangesTol_CheckedChanged(object sender, EventArgs e)
void radLog_CheckedChanged(object sender, EventArgs e)
void isCharValid_KeyPress(object sender, KeyPressEventArgs e)
void btnClearWaitfor_Click(object sender, EventArgs e)
void chkRuncontrol_CheckedChanged(object sender, EventArgs e)
Specialised version of the Dictionary class which can be converted to XML. Also has some additional s...
void NumericOnly_KeyPress(object sender, KeyPressEventArgs e)
void btnClearGo_Click(object sender, EventArgs e)
This class contains all the information about a LabVIEW VI that needs to be saved in the configuratio...
void chkOnlyIfChanged_CheckedChanged(object sender, EventArgs e)
void comVI_SelectedIndexChanged(object sender, EventArgs e)