-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
473 lines (391 loc) · 16.2 KB
/
Form1.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.AccessControl;
namespace LibraryScanner
{
public partial class Form1 : Form
{
public static Microsoft.Office.Interop.Excel.Application xlDatabase, xlLog;
public static Workbook wbDatabase, wbLog;
public static Worksheet wsDatabase, wsLog;
private String databasePath;
private String logPath;
private String logFileName;
private String databaseFileName;
private int tally;
private int nextRow;
public Form1()
{
//Create form
InitializeComponent();
//Open excel
xlDatabase = new Microsoft.Office.Interop.Excel.Application();
xlLog = new Microsoft.Office.Interop.Excel.Application();
//Prevent log file from sending save overwrite alerts
xlLog.DisplayAlerts = false;
//Set text labels values
idLabel.Text = "";
nameLabel.Text = "";
gradeLabel.Text = "";
updateDatabasePath((String)Properties.Settings.Default["databasePath"]);
updateLogPath((String)Properties.Settings.Default["logPath"]);
updateTally((int)Properties.Settings.Default["tally"]);
//File.SetAttributes(logPath, FileAttributes.ReadOnly);
createDatabase(databasePath);
}
//Creates notepad file with database from Excel file. Input full path of file.
private void createDatabase(String xlPath)
{
//Opens Excel file
if (File.Exists(xlPath) && (xlPath.IndexOf(".xls") >= 0 || xlPath.IndexOf(".xlsx") >= 0)) //Checks if file is a valid Excel file.
{
try
{
wbDatabase = xlDatabase.Workbooks.Open(xlPath);
wsDatabase = wbDatabase.ActiveSheet;
}
catch
{
MessageBox.Show("Please close any open instances of the database.");
}
}
else
{
MessageBox.Show("Student database does not exist. Please select a valid file.");
}
//Finds columns with ID, name, and grade
int idCol = wsDatabase.Cells.Find("ID").Column;
int nameCol = wsDatabase.Cells.Find("Name").Column;
int gradeCol = wsDatabase.Cells.Find("Gr").Column;
int row = wsDatabase.Cells.Find("ID").Row;
ArrayList data = new ArrayList();
while(row <= wsDatabase.UsedRange.Rows.Count)
{
data.Add(wsDatabase.Cells[row, idCol].Value + ", " + wsDatabase.Cells[row, nameCol].Value + ", " + wsDatabase.Cells[row, gradeCol].Value);
row++;
data.Sort();
}
System.IO.File.WriteAllLines(@"D:\Antony\Desktop\test.txt", (String[])data.ToArray(typeof(string)));
xlDatabase.Quit();
}
//Sets textbox to path of student database
private void updateDatabasePath(String path)
{
databasePath = path;
databaseLabel.Text = databasePath;
Properties.Settings.Default["databasePath"] = databasePath;
Properties.Settings.Default.Save();
//TEST
openDatabase();
}
//Sets textbox to folder where logs are stored
private void updateLogPath(String path)
{
logPath = path;
logLabel.Text = logPath;
Properties.Settings.Default["logPath"] = logPath;
Properties.Settings.Default.Save();
//TEST
openLog();
}
//Sets check in tally to desired number
private void updateTally(int t)
{
tally = t;
tallyLabel.Text = ""+tally;
Properties.Settings.Default["tally"] = tally;
Properties.Settings.Default.Save();
}
//Opens student database
private void openDatabase()
{
//Check if file is valid
if (File.Exists(databasePath) && (databasePath.IndexOf(".xls") >= 0 || databasePath.IndexOf(".xlsx") >= 0))
{
try
{
wbDatabase = xlDatabase.Workbooks.Open(databasePath);
wsDatabase = wbDatabase.ActiveSheet;
}
catch
{
MessageBox.Show("Please close any open instances of the database.");
}
}
else
{
MessageBox.Show("Student database does not exist. Please select a valid file.");
}
}
//Creates and opens log file
private void openLog()
{
logFileName = logPath + "\\Library_Log_";
DateTime today = DateTime.Today;
logFileName += today.ToString("MM_yyyy")+".xlsx";
if(File.Exists(logFileName))
{
//xlLog.Visible = true;
try
{
File.SetAttributes(logFileName, ~FileAttributes.ReadOnly);
wbLog = xlLog.Workbooks.Open(logFileName);
wsLog = wbLog.ActiveSheet;
wbLog.Save();
//MessageBox.Show("wbLog.Save() openlog");
}
catch
{
MessageBox.Show("Please close any open instances of " + logFileName);
}
//Range startCell = wsLog.Cells[1, 1];
//Range endCell = wsLog.Cells[65536, 5];
nextRow = wsLog./*Range[startCell,endCell].*/UsedRange.Rows.Count+1;
}
else
{
wbLog = xlLog.Application.Workbooks.Add();
wsLog = wbLog.Application.Worksheets.Add();
wsLog.Columns[1].ColumnWidth = 10;
wsLog.Cells[1, 1].Value = "Date";
wsLog.Columns[2].ColumnWidth = 10;
wsLog.Cells[1, 2].Value = "Time";
wsLog.Columns[3].ColumnWidth = 25;
wsLog.Cells[1, 3].Value = "Student Name";
wsLog.Columns[4].ColumnWidth = 10;
wsLog.Cells[1, 4].Value = "ID";
wsLog.Columns[5].ColumnWidth = 10;
wsLog.Cells[1, 5].Value = "Grade";
wsLog.Cells[1, 7].Value = "Date";
wsLog.Columns[7].ColumnWidth = 10;
wsLog.Cells[1, 8].Value = "Count";
wsLog.Columns[8].ColumnWidth = 10;
wsLog.Cells[1, 9].Value = DateTime.Now.ToString("MMM") + " Total";
wsLog.Cells[1, 9].Font.Bold = true;
wsLog.Cells[2, 9].Value = 0;
wsLog.Cells[2, 9].Font.Bold = true;
wsLog.Columns[9].ColumnWidth = 10;
//File.SetAttributes(logFileName, ~FileAttributes.ReadOnly);
try {
wbLog.SaveAs(logFileName);
}
catch { }
nextRow = 2;
}
try
{
File.SetAttributes(logFileName, ~FileAttributes.ReadOnly);
//MessageBox.Show(logFileName);
}
catch { }
}
//Finds student in database, displays their name on screen, and adds them to the log
private void recordStudent(String id)
{
//if(wbDatabase == null || wsDatabase == null)
{
openDatabase();
}
if(wbLog == null || wbDatabase == null)
{
openLog();
}
if (wsDatabase.Cells.Find("ID") == null || wsDatabase.Cells.Find("Name") == null || wsDatabase.Cells.Find("Gr") == null)
{
MessageBox.Show("Database file does not have columns for \"Student Id\", \"Student Name\", and \"Grd\".");
return;
}
int idCol = wsDatabase.Cells.Find("ID").Column;
int nameCol = wsDatabase.Cells.Find("Name").Column;
int gradeCol = wsDatabase.Cells.Find("Gr").Column;
Range idCell = wsDatabase.Cells.Find(id);
idBox.Text = "";
if(idCell == null)
{
MessageBox.Show("Student not found.");
return;
}
int studentRow = idCell.Row;
String name = wsDatabase.Cells[studentRow, nameCol].Value;
String grade = ""+wsDatabase.Cells[studentRow, gradeCol].Value;
DateTime time = DateTime.Now;
idLabel.Text = id;
nameLabel.Text = name;
gradeLabel.Text = grade;
wsLog.Cells[nextRow, 1].Value = time.ToString("MM/dd/yyyy");
wsLog.Cells[nextRow, 2].Value = time.ToString("hh:mm:ss");
wsLog.Cells[nextRow, 3].Value = name;
wsLog.Cells[nextRow, 4].Value = id;
wsLog.Cells[nextRow, 5].Value = grade;
nextRow++;
updateTally(tally + 1);
incrementDailyTally();
try
{
File.SetAttributes(logFileName, ~FileAttributes.ReadOnly);
wbLog.Save();
//MessageBox.Show("wbLog.Save() at closing");
}
catch
{
MessageBox.Show("Please close any open instances of the log file.");
return;
}
}
private void incrementDailyTally()
{
if (wbLog == null)
{
openLog();
}
int row = 2;
while(wsLog.Cells[row,7].Value != null)
{
row++;
}
String date = DateTime.Today.ToString("MM/dd/yyyy");
//MessageBox.Show(wsLog.Cells[row - 1, 7].Value.GetType().ToString());
if (wsLog.Cells[row-1,7].Value.ToString() == DateTime.Today.ToString())
{
//wsLog.Cells[row-1, 8].Value = Int32.Parse(wsLog.Cells[row-1, 8].Value) + 1;
wsLog.Cells[row - 1, 8].Value = wsLog.Cells[row - 1, 8].Value + 1;
}
else
{
wsLog.Cells[row, 7].Value = date;
wsLog.Cells[row, 8].Value = 1;
}
wsLog.Cells[2, 9].Value = wsLog.Cells[2, 9].Value + 1;
}
//ID Textbox event handler to remove non numerical characters
private void idBox_TextChanged(object sender, EventArgs e)
{
//Retrieve text from textbox
System.Windows.Forms.TextBox box = (System.Windows.Forms.TextBox)sender;
String text = box.Text;
//Remove non numerical characters
for(int i=0; i<text.Length; i++)
{
if (text.Length > 0 && ((int)text[i] < 48 || (int)text[i] > 57))
{
box.Text = text.Substring(0, i) + text.Substring(i+1);
box.Select(i, 0);
}
}
}
//ID Textbox event handler to process ID
private void idBox_KeyDown(object sender, KeyEventArgs e)
{
//Check if key pressed is Enter
if (e.KeyCode == Keys.Enter)
{
//Retrieve text from textbox
System.Windows.Forms.TextBox box = (System.Windows.Forms.TextBox)sender;
String text = box.Text;
if(text.Length != 7)
{
box.Text = "";
MessageBox.Show("ID Must be 7 digits long.");
return;
}
recordStudent(text);
}
}
//Allows user to select database Excel file after clicking the Browse button
private void databaseBrowseButton_Click(object sender, EventArgs e)
{
//Create a new dialog that allows user to select .xls or .xlsx files
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Excel Files (*.xls,*.xlsx)|*.xls;*.xlsx|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
//Displays dialog and records selected file
DialogResult result = openFileDialog1.ShowDialog();
String fileName = openFileDialog1.FileName;
//Checks if file is valid and updates path
if (result == DialogResult.OK && File.Exists(fileName) && (fileName.IndexOf(".xls") >= 0 || fileName.IndexOf(".xlsx") >= 0))
{
updateDatabasePath(fileName);
if(wbDatabase == null)
{
openDatabase();
}
}
else if(result != DialogResult.Cancel)
{
MessageBox.Show("File does not exist or is not a valid Excel file.");
}
}
//Opens folder with log files
private void openButton_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(logPath);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
File.SetAttributes(logFileName, ~FileAttributes.ReadOnly);
wbLog.Save();
//MessageBox.Show("wbLog.Save() at closing");
}
catch
{
MessageBox.Show("Please close any open instances of the log file before quitting.");
return;
}
xlDatabase.Quit();
xlLog.Quit();
File.SetAttributes(logFileName, FileAttributes.ReadOnly);
}
//Allows user to select folder for storing logs after clicking the Browse button
private void logBrowseButton_Click(object sender, EventArgs e)
{
//Creates a new dialog that allows user to select folder
FolderBrowserDialog fbd = new FolderBrowserDialog();
//Displays dialog and records user selection
DialogResult result = fbd.ShowDialog();
//Checks if folder is valid and updates path
if (result == DialogResult.OK)
{
updateLogPath(fbd.SelectedPath);
}
}
//Resets tally
private void resetButton_Click(object sender, EventArgs e)
{
updateTally(0);
//openLog();
}
//Sends enter key to idBox when button is pressed
private void enterButton_Click(object sender, EventArgs e)
{
idBox.Focus();
SendKeys.Send("{ENTER}");
}
//Stops log from being deleted, courtesy of MSDN
private void lockFile()
{
FileSecurity fSecurity = File.GetAccessControl(logFileName);
fSecurity.AddAccessRule(new FileSystemAccessRule("Authenticated Users", FileSystemRights.Read, AccessControlType.Allow));
fSecurity.AddAccessRule(new FileSystemAccessRule("Authenticated Users", FileSystemRights.Write, AccessControlType.Deny));
File.SetAccessControl(logFileName, fSecurity);
}
private void unlockFile()
{
FileSecurity fSecurity = File.GetAccessControl(logFileName);
fSecurity.AddAccessRule(new FileSystemAccessRule("Authenticated Users", FileSystemRights.Read, AccessControlType.Allow));
fSecurity.AddAccessRule(new FileSystemAccessRule("Authenticated Users", FileSystemRights.Write, AccessControlType.Allow));
File.SetAccessControl(logFileName, fSecurity);
}
}
}