SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
ConfigureBlocks.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 
11 namespace Sample_Environment_Control_Interface.Dialogs.Blocks
12 {
13  public partial class ConfigureBlocks : Form
14  {
15  //Temporary dictionary to contain the configs blocks
16  private Seci.BlockDictionary _tempBlocks = new Seci.BlockDictionary();
17  private Boolean _changesMade = false;
18 
19  public ConfigureBlocks()
20  {
21  InitializeComponent();
22  }
23 
24  private void configureBlocks_Load(object sender, EventArgs e)
25  {
26  //Disable datagrid sorting
27  foreach (DataGridViewColumn i in dataGridView1.Columns)
28  {
29  i.SortMode = DataGridViewColumnSortMode.NotSortable;
30  }
31 
32  //Copy/clone current config blocks on to tempblocks
33  _tempBlocks = (Seci.BlockDictionary)Seci.SeciInterface.Blocks.Clone();
34 
35  //Check if tempblocks is null, i.e. no blocks in config
36  if (_tempBlocks == null)
37  {
38  //declare new instance
39  _tempBlocks = new Seci.BlockDictionary();
40  }
41 
42  updateDataGrid();
43 
44  }
45 
46  private void updateDataGrid()
47  {
48  dataGridView1.Rows.Clear();
49 
50  //Main config blocks
51  if (_tempBlocks != null)
52  {
53  for (int i = 0; i < _tempBlocks.Count; ++i)
54  {
55  Seci.BlockInfo temp = _tempBlocks[i];
56 
57  dataGridView1.Rows.Add(temp.BlockName, temp.BlockUnits, temp.OwningComponent,
58  temp.ParentVI.Substring(temp.ParentVI.LastIndexOf('\\') + 1),
59  temp.UnderRunControl, temp.LowerLimit, temp.UpperLimit,
60  temp.BlockEnabled);
61 
62  }
63  }
64 
65  dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Ascending);
66  }
67 
68  private void btnAdd_Click(object sender, EventArgs e)
69  {
70  AddEditBlock addBlock = new AddEditBlock();
71  addBlock.Dictionary = _tempBlocks;
72 
73  if (addBlock.ShowDialog() == DialogResult.OK)
74  {
75  _changesMade = true;
76  //update datagrid
77  updateDataGrid();
78  }
79  }
80 
81  private void btnEdit_Click(object sender, EventArgs e)
82  {
83  if (dataGridView1.CurrentRow != null)
84  {
85  //Check block is not part of a component, as these are not editable bar runcontrol
86  if (dataGridView1.CurrentRow.Cells[2].Value == null)
87  {
88  AddEditBlock editBlock = new AddEditBlock();
89  editBlock.Dictionary = _tempBlocks;
90 
91  editBlock.Block = (Seci.BlockInfo)_tempBlocks[dataGridView1.CurrentRow.Cells[colName.Name].Value.ToString()];
92 
93  if (editBlock.ShowDialog() == DialogResult.OK)
94  {
95  _changesMade = true;
96  //update datagrid
97  updateDataGrid();
98  }
99  }
100  else
101  {
102  MessageBox.Show("The selected block cannot be edited as it is part of a component. However, the run-control settings can be changed via the table."
103  , "Cannot edit component block", MessageBoxButtons.OK, MessageBoxIcon.Warning);
104  }
105  }
106  }
107 
108  private void btnDelete_Click(object sender, EventArgs e)
109  {
110  if (dataGridView1.CurrentRow != null)
111  {
112  //Check block is not part of a component, as these are not editable bar runcontrol
113  if (dataGridView1.CurrentRow.Cells[2].Value == null)
114  {
115  //delete the currently selected data row
116  if (dataGridView1.CurrentRow != null)
117  {
118  _tempBlocks.Remove(dataGridView1.CurrentRow.Cells[colName.Name].Value.ToString());
119  dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
120  _changesMade = true;
121  }
122  }
123  else
124  {
125  MessageBox.Show("The selected block cannot be deleted as it is part of a component."
126  , "Cannot delete component block", MessageBoxButtons.OK, MessageBoxIcon.Warning);
127  }
128  }
129  }
130 
131  private void btnSetNexus_Click(object sender, EventArgs e)
132  {
133  SetNexusInfo nexus = new SetNexusInfo();
134 
135  if (_tempBlocks != null)
136  {
137  //Copy/clone current config blocks on to tempblocks
138  nexus.blocksNexus = (Seci.BlockDictionary)_tempBlocks.Clone();
139  }
140 
141  if (nexus.ShowDialog() == DialogResult.OK)
142  {
143  _tempBlocks = nexus.blocksNexus;
144  _changesMade = true;
145  }
146  }
147 
148  private void btnOK_Click(object sender, EventArgs e)
149  {
150  //Pause threads
151  SECI_GUI.Pause_Timers();
152 
153  //Copy tempblocks onto main config blocks
154  Seci.SeciInterface.Blocks = _tempBlocks;
155 
156  if (Seci.SeciInterface.SeciStatus.UseDaeMonitorVi)
157  {
158  Seci.SeciInterface.DAEMonitor_SetBlocks();
159  }
160 
161  //Restart threads
162  SECI_GUI.Resume_Timers();
163 
164  if (_changesMade)
165  {
166  DialogResult = DialogResult.OK;
167  }
168  else
169  {
170  DialogResult = DialogResult.Cancel;
171  }
172 
173  Close();
174  }
175 
176  //Values changed
177  private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
178  {
179  doChanges((Seci.BlockInfo)_tempBlocks[dataGridView1.CurrentRow.Cells[colName.Name].Value.ToString()]);
180  }
181 
182  private void doChanges(Seci.BlockInfo block)
183  {
184  //make changes to run-control, visibility and enabled only
185  if (dataGridView1.CurrentCell == dataGridView1.CurrentRow.Cells["colRuncontrol"])
186  {
187  block.UnderRunControl = (bool)dataGridView1.CurrentRow.Cells["colRuncontrol"].Value;
188  }
189  else if (dataGridView1.CurrentCell == dataGridView1.CurrentRow.Cells["colLower"])
190  {
191  try
192  {
193  block.LowerLimit = Convert.ToDouble(dataGridView1.CurrentRow.Cells["colLower"].Value);
194  }
195  catch
196  {
197  MessageBox.Show("Invalid value enter; value rest to previous value.", "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Error);
198  dataGridView1.CurrentRow.Cells["colLower"].Value = block.LowerLimit;
199  }
200  }
201  else if (dataGridView1.CurrentCell == dataGridView1.CurrentRow.Cells["colUpper"])
202  {
203  try
204  {
205  block.UpperLimit = Convert.ToDouble(dataGridView1.CurrentRow.Cells["colUpper"].Value);
206  }
207  catch
208  {
209  MessageBox.Show("Invalid value enter; value rest to previous value.", "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Error);
210  dataGridView1.CurrentRow.Cells["colUpper"].Value = block.UpperLimit;
211  }
212 
213  }
214  else if (dataGridView1.CurrentCell == dataGridView1.CurrentRow.Cells["colEnabled"])
215  {
216  block.BlockEnabled = (bool)dataGridView1.CurrentRow.Cells["colEnabled"].Value;
217  }
218  }
219  }
220 }
void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
Specialised version of the Dictionary class which can be converted to XML. Also has some additional s...