-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPrinterStatusForm.cs
337 lines (321 loc) · 12 KB
/
PrinterStatusForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace PrinterStatus
{
public partial class PrinterStatusForm : Form
{
delegate void RefreshPrinterGridViewCallback();
delegate void SetProgressBarCallback(int percentDone);
delegate void SetStatusTextCallback(string status);
delegate void StopUpdatingCallback();
delegate void StartUpdatingCallback();
public static DataGridViewSelectedCellCollection selectedCells;
public static DataGridViewColumn sortingColumn;
public static ListSortDirection sortingDirection = ListSortDirection.Ascending;
public PrinterStatusForm()
{
InitializeComponent();
}
private void ConditionalFormatting()
{
foreach (DataGridViewRow printerGridViewRow in PrinterGridView.Rows)
{
DataGridViewCell tonerCell = printerGridViewRow.Cells[4];
string tonerStatus = "";
if(tonerCell.Value != null)
{
tonerStatus = tonerCell.Value.ToString();
}
if (tonerStatus != "")
{
MatchCollection matches = Regex.Matches(tonerStatus, @"(\d{1,3})%.+", RegexOptions.Multiline);
bool tonerCritical = false;
bool tonerWarning = false;
foreach (Match match in matches)
{
int percentUsed = int.Parse(match.Groups[1].ToString());
if (percentUsed < Program.tonerCriticalThreshold)
{
tonerCritical = true;
break;
}
else if (percentUsed < Program.tonerWarningThreshold)
{
tonerWarning = true;
}
}
if (tonerCritical)
{
tonerCell.Style.BackColor = Color.LightCoral;
}
else if(tonerWarning){
tonerCell.Style.BackColor = Color.Khaki;
}
else
{
tonerCell.Style = tonerCell.OwningColumn.DefaultCellStyle;
}
}
DataGridViewCell paperCell = printerGridViewRow.Cells[5];
string paperStatus = paperCell.Value.ToString();
if (paperStatus != "")
{
Match matchEmpty = Regex.Match(paperStatus, @".*Empty.*", RegexOptions.Singleline);
Match matchOpen = Regex.Match(paperStatus, @".*Tray Open.*", RegexOptions.Singleline);
if (matchEmpty.Success)
{
paperCell.Style.BackColor = Color.LightCoral;
}
else if (matchOpen.Success)
{
paperCell.Style.BackColor = Color.Khaki;
}
else
{
paperCell.Style = paperCell.OwningColumn.DefaultCellStyle;
}
}
}
}
//public void RefreshPrinterGridView()
//{
// // InvokeRequired required compares the thread ID of the
// // calling thread to the thread ID of the creating thread.
// // If these threads are different, it returns true.
// if (this.PrinterGridView.InvokeRequired)
// {
// RefreshPrinterGridViewCallback d = new RefreshPrinterGridViewCallback(RefreshPrinterGridView);
// try
// {
// this.Invoke(d, new object[] {});
// }
// catch (Exception ex)
// {
// Debug.WriteLine(ex.StackTrace);
// }
// }
// else
// {
// //while (Program.updatingTable)
// //{
// // Thread.Sleep(100);
// //}
// //Program.printerBindingSource.ResetBindings(false);
// PrinterGridView.Sort(this.printerNameDataGridViewTextBoxColumn, ListSortDirection.Ascending);
// ConditionalFormatting();
// }
//}
public void SetProgressBar(int percentDone)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.statusStrip.InvokeRequired)
{
SetProgressBarCallback d = new SetProgressBarCallback(SetProgressBar);
try
{
this.Invoke(d, new object[] {percentDone});
}
catch (Exception ex)
{
Debug.WriteLine(ex.StackTrace);
Application.Exit();
}
}
else
{
this.progressBar.Value = percentDone;
}
}
public void StopUpdating()
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.statusStrip.InvokeRequired)
{
StopUpdatingCallback d = new StopUpdatingCallback(StopUpdating);
try
{
this.Invoke(d, new object[] { });
}
catch (Exception ex)
{
Debug.WriteLine(ex.StackTrace);
Application.Exit();
}
}
else
{
//Program.printerBindingSource.DataSource = Program.printerTableLast;
//if (!Program.updatingTable)
//{
Program.updatingTable = true;
PrinterGridView.DataSource = Program.printerTableLast;
//PrinterGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
if (sortingColumn != null)
{
PrinterGridView.Sort(sortingColumn, sortingDirection);
}
ConditionalFormatting();
SelectCell(Program.selectedY, Program.selectedX);
//SelectCellCollection();
Program.updatingTable = false;
//}
//PrinterGridView.Sort(this.printerNameDataGridViewTextBoxColumn, ListSortDirection.Ascending);
}
}
public void StartUpdating()
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.statusStrip.InvokeRequired)
{
StartUpdatingCallback d = new StartUpdatingCallback(StartUpdating);
try
{
this.Invoke(d, new object[] { });
}
catch (Exception ex)
{
Debug.WriteLine(ex.StackTrace);
Application.Exit();
}
}
else
{
//if (!Program.updatingTable)
//{
Program.updatingTable = true;
PrinterGridView.DataSource = Program.printerTable;
//PrinterGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
if (sortingColumn != null)
{
PrinterGridView.Sort(sortingColumn, sortingDirection);
}
ConditionalFormatting();
SelectCell(Program.selectedY, Program.selectedX);
//SelectCellCollection();
Program.updatingTable = false;
//}
}
}
private void SelectCell(int row, int col)
{
if(row == -1 && col == -1)
{
PrinterGridView.CurrentCell.Selected = false;
return;
}
if (row < PrinterGridView.Rows.Count)
{
if(col < PrinterGridView.Rows[row].Cells.Count)
{
PrinterGridView.CurrentCell = PrinterGridView.Rows[row].Cells[col];
}
}
}
private void SelectCellCollection()
{
if (selectedCells != null)
{
foreach (DataGridViewCell cell in selectedCells)
{
if (cell.RowIndex > 0 && cell.RowIndex < PrinterGridView.Rows.Count)
{
if (cell.ColumnIndex > 0 && cell.ColumnIndex < PrinterGridView.Rows[cell.RowIndex].Cells.Count)
{
PrinterGridView.Rows[cell.RowIndex].Cells[cell.ColumnIndex].Selected = true;
}
}
}
}
}
public void SetStatusText(string status)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.statusStrip.InvokeRequired)
{
SetStatusTextCallback d = new SetStatusTextCallback(SetStatusText);
try
{
this.Invoke(d, new object[] { status });
}
catch (Exception ex)
{
Debug.WriteLine(ex.StackTrace);
Application.Exit();
}
}
else
{
this.statusText.Text = status;
}
}
private void PrinterStatusForm_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
Environment.Exit(0);
}
private void PrinterGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (!Program.updatingTable)
{
Program.selectedX = e.ColumnIndex;
Program.selectedY = e.RowIndex;
}
}
private void PrinterGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (!Program.selectionChanged)
{
PrinterGridView.ClearSelection();
Program.selectedX = -1;
Program.selectedY = -1;
Program.selectionChanged = true;
}
else
{
Program.selectionChanged = false;
}
}
private void PrinterGridView_SelectionChanged(object sender, EventArgs e)
{
if (!Program.updatingTable)
{
Program.selectionChanged = true;
}
}
private void PrinterGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
sortingColumn = PrinterGridView.SortedColumn;
if(PrinterGridView.SortOrder == SortOrder.Ascending)
{
sortingDirection = ListSortDirection.Ascending;
}
else
{
sortingDirection = ListSortDirection.Descending;
}
ConditionalFormatting();
}
private void PrinterStatusForm_Load(object sender, EventArgs e)
{
sortingColumn = PrinterGridView.Columns[0];
}
}
}