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 Sample_Environment_Control_Interface.Dialogs.Blocks
11 {
12 
13  public partial class AddEditBlock : Form
14  {
15  public Seci.BlockInfo Block;
17  private Boolean _isEdit;
18 
19  public AddEditBlock()
20  {
21  InitializeComponent();
22  }
23 
24  private void AddBlock_Load(object sender, EventArgs e)
25  {
26  if (Block == null)
27  {
28  _isEdit = false;
29 
30  //Add
31  fillInVIs();
32  }
33  else
34  {
35  _isEdit = true;
36 
37  //Edit
38  fillInVIs();
39  txtBlockName.Text = Block.BlockName;
40  txtUnits.Text = Block.BlockUnits;
41  txtAlias.Text = Block.Alias;
42 
43  txtLogRate.Text = Block.LoggingRate.ToString();
44 
45  comVI.SelectedItem = Block.ParentVI;
46  comRead.SelectedItem = Block.ReadControl;
47 
48  chkEnabled.Checked = Block.BlockEnabled;
49 
50  radLog.Checked = Block.LogValueToFile;
51  radOnlyChanges.Checked = Block.LogChangesOnly;
52  radOnlyChangesTol.Checked = Block.LogChangesTolerance;
53  txtTolerance.Text = Block.Tolerance.ToString();
54 
55  if (Block.WriteControl != null)
56  {
57  comWrite.SelectedItem = Block.WriteControl;
58  }
59 
60  if (Block.GoButton != null)
61  {
62  comGo.SelectedItem = Block.GoButton;
63  }
64 
65  if (Block.WaitForControl != null)
66  {
67  comWaitfor.SelectedItem = Block.WaitForControl;
68  }
69 
70  if (Block.UnderRunControl)
71  {
72  chkRuncontrol.Checked = true;
73  txtUpper.Text = Block.UpperLimit.ToString();
74  txtLower.Text = Block.LowerLimit.ToString();
75  }
76 
77  chkSaveSettings.Checked = Block.SaveSettings;
78  }
79  }
80 
81  private void fillInVIs()
82  {
83  //Get list of VI's
84  for (int i = 0; i < Seci.SeciInterface.LVPanelProperties.Count; ++i)
85  {
86  comVI.Items.Add(Seci.SeciInterface.LVPanelProperties[i].PanelName);
87  }
88 
89  //Add DAE
90  comVI.Items.Add(Seci.SeciInterface.DAE_GetLocation());
91 
92  //Add Beam Logger
93  comVI.Items.Add(Seci.SeciInterface.BeamLogger_GetLocation());
94 
95  }
96 
97  private void fillInControls(String VIname)
98  {
99  String[] controls = Seci.SeciInterface.LV_GetListOfControls(VIname).ToArray();
100 
101  //Read
102  comRead.Items.Clear();
103  comRead.Items.AddRange(controls);
104  comRead.SelectedIndex = -1;
105 
106  //Write
107  comWrite.Items.Clear();
108  comWrite.Items.AddRange(controls);
109  comWrite.SelectedIndex = -1;
110 
111  //Go
112  comGo.Items.Clear();
113  comGo.Items.AddRange(controls);
114  comGo.SelectedIndex = -1;
115 
116  //Waitfor
117  comWaitfor.Items.Clear();
118  comWaitfor.Items.AddRange(controls);
119  comWaitfor.SelectedIndex = -1;
120  }
121 
122  private void comVI_SelectedIndexChanged(object sender, EventArgs e)
123  {
124  if (comVI.SelectedIndex != -1)
125  {
126  fillInControls(comVI.SelectedItem.ToString());
127  }
128  }
129 
130  private void btnClearWrite_Click(object sender, EventArgs e)
131  {
132  comWrite.SelectedIndex = -1;
133  }
134 
135  private void btnClearGo_Click(object sender, EventArgs e)
136  {
137  comGo.SelectedIndex = -1;
138  }
139 
140  private void btnClearWaitfor_Click(object sender, EventArgs e)
141  {
142  comWaitfor.SelectedIndex = -1;
143  }
144 
145  private void btnOK_Click(object sender, EventArgs e)
146  {
147  //Check that the required options are chosen
148  if (checkFilledOutCorrectly())
149  {
150  Seci.BlockInfo newBlock = new Seci.BlockInfo();
151 
152  //Always present
153  newBlock.BlockName = txtBlockName.Text;
154  newBlock.BlockUnits = txtUnits.Text;
155  newBlock.ParentVI = comVI.SelectedItem.ToString();
156  newBlock.Alias = txtAlias.Text;
157 
158  newBlock.ReadControl = comRead.SelectedItem.ToString();
159  newBlock.LoggingRate = Convert.ToSingle(txtLogRate.Text);
160 
161  newBlock.BlockEnabled = chkEnabled.Checked;
162 
163  //Optional stuff
164  newBlock.LogValueToFile = radLog.Checked;
165  newBlock.LogChangesOnly = radOnlyChanges.Checked;
166  newBlock.LogChangesTolerance = radOnlyChangesTol.Checked;
167 
168  newBlock.Tolerance = Convert.ToDouble(txtTolerance.Text);
169 
170  newBlock.UnderRunControl = chkRuncontrol.Checked;
171  newBlock.UpperLimit = Convert.ToDouble(txtUpper.Text);
172  newBlock.LowerLimit = Convert.ToDouble(txtLower.Text);
173  newBlock.SaveSettings = chkSaveSettings.Checked;
174 
175  if (comWrite.SelectedIndex != -1)
176  {
177  newBlock.WriteControl = comWrite.SelectedItem.ToString();
178  }
179 
180  if (comGo.SelectedIndex != -1)
181  {
182  newBlock.GoButton = comGo.SelectedItem.ToString();
183  }
184 
185  if (comWaitfor.SelectedIndex != -1)
186  {
187  newBlock.WaitForControl = comWaitfor.SelectedItem.ToString();
188  }
189 
190  if (!_isEdit)
191  {
192  try
193  {
194  //Add to tempblocks
195  Dictionary.Add(newBlock.BlockName, newBlock);
196  }
197  catch (Exception err)
198  {
199  MessageBox.Show(err.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
200  return;
201  }
202  }
203  else
204  {
205  try
206  {
207  //Replace the old block - remove then add because the name may have changed
208  //which would chnage the key
209  Dictionary.Remove(Block.BlockName);
210  Dictionary.Add(newBlock.BlockName, newBlock);
211  }
212  catch (Exception err)
213  {
214  MessageBox.Show(err.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
215  return;
216  }
217  }
218 
219  DialogResult = DialogResult.OK;
220  Close();
221  }
222 
223  }
224 
225  private Boolean checkFilledOutCorrectly()
226  {
227  List<String> filledList = checkFilledOutandValid();
228 
229  if (filledList.Count > 0)
230  {
231  String message = "The following entries are invalid:";
232 
233  for (int i = 0; i < filledList.Count; ++i)
234  {
235  message = message + Environment.NewLine + filledList[i];
236  }
237 
238  MessageBox.Show(message, "Invalid Entries!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
239 
240  return false;
241  }
242  else
243  {
244  return true;
245  }
246 
247  }
248 
249  private List<String> checkFilledOutandValid()
250  {
251  List<String> list = new List<String>();
252 
253  Double dummy; //Used for testing for valid doubles
254 
255  if (txtBlockName.Text == "")
256  {
257  list.Add(" - No block name entered.");
258  }
259 
260  if (comVI.SelectedIndex == -1)
261  {
262  list.Add(" - No VI selected.");
263  }
264 
265  if (comRead.SelectedIndex == -1)
266  {
267  list.Add(" - No read control selected.");
268  }
269 
270  if (chkRuncontrol.Checked)
271  {
272  if (txtUpper.Text == "")
273  {
274  list.Add(" - No upper limit entered for run-control.");
275  }
276  else if (!Double.TryParse(txtUpper.Text, out dummy))
277  {
278  list.Add(" - upper limit value not valid.");
279  }
280 
281  if (txtLower.Text == "")
282  {
283  list.Add(" - No lower limit entered for run-control.");
284  }
285  else if (!Double.TryParse(txtLower.Text, out dummy))
286  {
287  list.Add(" - lower limit value not valid.");
288  }
289  }
290  else
291  {
292  if ((txtUpper.Text == "")||(!Double.TryParse(txtUpper.Text, out dummy)))
293  {
294  txtUpper.Text = "0";
295  }
296 
297  if ((txtLower.Text == "")||(!Double.TryParse(txtLower.Text, out dummy)))
298  {
299  txtLower.Text = "0";
300  }
301  }
302 
303  if (radLog.Checked)
304  {
305 
306  if (txtLogRate.Text == "")
307  {
308  list.Add(" - No log rate entered.");
309  }
310  else
311  {
312  if (!Double.TryParse(txtLogRate.Text, out dummy))
313  {
314  list.Add(" - log-rate value not valid.");
315  }
316  }
317  }
318  else if (radOnlyChangesTol.Checked)
319  {
320  if (txtTolerance.Text == "")
321  {
322  list.Add(" - No tolerance entered.");
323  }
324  else
325  {
326  if (!Double.TryParse(txtTolerance.Text, out dummy))
327  {
328  list.Add(" - tolerance value not valid.");
329  }
330  }
331  }
332 
333  return list;
334  }
335 
336  //Check that key presses are numeric
337  private void NumericOnly_KeyPress(object sender, KeyPressEventArgs e)
338  {
339  if (!isNumeric(e))
340  {
341  e.Handled = true;
342  }
343  }
344 
345  //Check that key presses are numeric or letter or underscore
346  //Anything else may cause a problem with Open GENIE
347  private void isCharValid_KeyPress(object sender, KeyPressEventArgs e)
348  {
349  if (!isCharValid(e))
350  {
351  e.Handled = true;
352  }
353  }
354 
355  private Boolean isNumeric(KeyPressEventArgs e)
356  {
357  // \b is backspace/delete
358  if (Char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == '\b')
359  {
360  return true;
361  }
362  else
363  {
364  return false;
365  }
366  }
367 
368  private Boolean isCharValid(KeyPressEventArgs e)
369  {
370  // \b is backspace/delete
371  if (Char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == '_' || e.KeyChar == '\b')
372  {
373  return true;
374  }
375  else
376  {
377  return false;
378  }
379  }
380 
381  private void chkRuncontrol_CheckedChanged(object sender, EventArgs e)
382  {
383  if (chkRuncontrol.Checked)
384  {
385  txtUpper.Enabled = true;
386  txtLower.Enabled = true;
387  }
388  else
389  {
390  txtUpper.Enabled = false;
391  txtLower.Enabled = false;
392  }
393  }
394  }
395 }
void isCharValid_KeyPress(object sender, KeyPressEventArgs e)
void NumericOnly_KeyPress(object sender, KeyPressEventArgs e)
Specialised version of the Dictionary class which can be converted to XML. Also has some additional s...