-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainDisplay.cs
More file actions
530 lines (456 loc) · 19 KB
/
Copy pathMainDisplay.cs
File metadata and controls
530 lines (456 loc) · 19 KB
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace Inventory_Management_System
{
public partial class MainDisplay : UserControl
{
public MainDisplay()
{
InitializeComponent();
// Update and populate the parts and products tables
updatePartsTable();
updateProductsTable();
}
// Method to handle clicks on add part button
private void btnAddPart_Click(object sender, EventArgs e)
{
// Assign the AddPart user control and MainWindow form to variables
AddPart addPartScreen = new AddPart();
Form mainWindow = this.ParentForm;
// Add the addPartScreen as a control in the MainWindow Form
mainWindow.Controls.Add(addPartScreen);
// Set the add part screen as the active control
mainWindow.ActiveControl = addPartScreen;
// Set title on addPartScreen
addPartScreen.setTitle("Add Part");
// Set the location of the add part screen, make sure it's in the front, and set it as the input focus
addPartScreen.Location = new Point(this.Width / 4, this.Height / 4);
addPartScreen.BringToFront();
addPartScreen.Focus();
}
// Method to handle clicks on modify part button
private void btnModifyPart_Click(object sender, EventArgs e)
{
// Assign the AddPart user control and MainWindow form to variables
AddPart modPartScreen = new AddPart();
Form mainWindow = this.ParentForm;
// Get the selected Part
Object selected = partsDataView.SelectedRows[0].DataBoundItem;
if (selected is Inhouse)
{
// Set the modify parts screen for an inhouse part
modPartScreen.ModInhousePart(selected as Inhouse);
}
else
{
// Set the modiy parts screen for an outsourced part
modPartScreen.ModOutsourcedPart(selected as Outsourced);
}
// Add the addPartScreen as a control in the MainWindow Form
mainWindow.Controls.Add(modPartScreen);
// Set the add part screen as the active control
mainWindow.ActiveControl = modPartScreen;
// Set title on addPartScreen
modPartScreen.setTitle("Modify Part");
// Set the location of the add part screen, make sure it's in the front, and set it as the input focus
modPartScreen.Location = new Point(this.Width / 4, this.Height / 4);
modPartScreen.BringToFront();
modPartScreen.Focus();
}
private void btnAddProduct_Click(object sender, EventArgs e)
{
// Assign the AddPart user control and MainWindow form to variables
AddProduct addProductScreen = new AddProduct();
Form mainWindow = this.ParentForm;
// Add the addPartScreen as a control in the MainWindow Form
mainWindow.Controls.Add(addProductScreen);
// Set the add part screen as the active control
mainWindow.ActiveControl = addProductScreen;
// Set title on addPartScreen
addProductScreen.setTitle("Add Product");
// Set the location of the add part screen, make sure it's in the front, and set it as the input focus
addProductScreen.Location = new Point(8, 8);
addProductScreen.BringToFront();
addProductScreen.Focus();
}
private void btnModifyProduct_Click(object sender, EventArgs e)
{
// Assign the AddPart user control and MainWindow form to variables
AddProduct modProductScreen = new AddProduct();
Form mainWindow = this.ParentForm;
// Get the selected Part
Product selected = productsDataView.SelectedRows[0].DataBoundItem as Product;
// Add the addPartScreen as a control in the MainWindow Form
mainWindow.Controls.Add(modProductScreen);
// Set the add part screen as the active control
mainWindow.ActiveControl = modProductScreen;
// Set title on addPartScreen
modProductScreen.setTitle("Modify Product");
modProductScreen.setModProduct(selected);
// Set the location of the add part screen, make sure it's in the front, and set it as the input focus
modProductScreen.Location = new Point(8, 8);
modProductScreen.BringToFront();
modProductScreen.Focus();
}
// When the MainDisplay is enabled/disabled
private void MainDisplay_EnabledChanged(object sender, EventArgs e)
{
// Update tables
updatePartsTable();
updateProductsTable();
checkTableStatus();
}
// Check the tables' status to enable/disable buttons
private void checkTableStatus()
{
// Check if the tables are empty, if so, disable the modify, delete, and search buttons
if (isTableEmpty(partsDataView))
{
disableButtons("Parts");
}
else
{
enableButtons("Parts");
}
if (isTableEmpty(productsDataView))
{
disableButtons("Products");
}
else
{
enableButtons("Products");
}
}
// Disable buttons if a table is empty
private void disableButtons(string source)
{
if (source == "Parts")
{
btnModifyPart.Enabled = false;
btnDeletePart.Enabled = false;
btnPartsSearch.Enabled = false;
}
if (source == "Products")
{
btnModifyProduct.Enabled = false;
btnDeleteProduct.Enabled = false;
btnProductsSearch.Enabled = false;
}
}
// Enable buttons if a table is not empty
private void enableButtons(string source)
{
if (source == "Parts")
{
btnModifyPart.Enabled = true;
btnDeletePart.Enabled = true;
btnPartsSearch.Enabled = true;
}
if (source == "Products")
{
btnModifyProduct.Enabled = true;
btnDeleteProduct.Enabled = true;
btnProductsSearch.Enabled = true;
}
}
// Check if a table is empty
private bool isTableEmpty(DataGridView table)
{
if (table.Rows.Count == 0)
{
return true;
}
return false;
}
// Method to update the parts table
private void updatePartsTable()
{
DataGridView partsTable = this.partsDataView;
partsTable.DataSource = Inventory.AllParts;
// Check the tables' status to enable/disable buttons
checkTableStatus();
}
// Method to update the products table
private void updateProductsTable()
{
DataGridView productsTable = this.productsDataView;
productsTable.DataSource = Inventory.Products;
// Check the tables' status to enable/disable buttons
checkTableStatus();
}
// Delete a part from the partsDataView
private void btnDeletePart_Click(object sender, EventArgs e)
{
// Get the selected part from the partsDataView
Part selected = partsDataView.SelectedRows[0].DataBoundItem as Part;
// If the part is NOT associated with a product
if (!checkAssociation(selected))
{
// Create a message with part info to display to the user
String message = $"Are you sure you want to permanently delete this part?\n" +
$"ID: {selected.PartID}\n" +
$"Name: {selected.Name}\n" +
$"Price: {selected.Price}";
// Display a yes/no dialog to the user to confirm delete of part
DialogResult result = MessageBox.Show(message, "Warning!", MessageBoxButtons.YesNo);
// If the user selected yes
if (result == DialogResult.Yes)
{
// Delete the part and update the parts table
Inventory.deletePart(selected);
updatePartsTable();
}
// Check all tables' status and update
checkTableStatus();
}
}
// Delete Product from products table
private void btnDeleteProduct_Click(object sender, EventArgs e)
{
// Get the selected product from the table
Product selected = productsDataView.SelectedRows[0].DataBoundItem as Product;
// If there are parts associated with the product
if (selected.AssociatedParts.Count > 0)
{
// Create a string to show parts' names
String associatedParts = "";
// Iterate through the list of parts
foreach (Part associatedPart in selected.AssociatedParts)
{
// Add the part name to the string
associatedParts += $"{associatedPart.Name}\n";
}
// Notify the user that the part can not be deleted due to association
MessageBox.Show("This product can not be deleted.\n" +
"It is associated with the following parts:\n" +
$"{associatedParts}");
// Return without deleting the product
return;
}
// If there are no associated parts
// Build a message to ask the user to confirm delete
String message = $"Are you sure you want to permanently delete this product?\n" +
$"ID: {selected.ProductID}\n" +
$"Name: {selected.Name}\n" +
$"Price: {selected.Price}";
// Display a dialog asking teh user to confirm
DialogResult result = MessageBox.Show(message, "Warning!", MessageBoxButtons.YesNo);
// If the user selected yes
if (result == DialogResult.Yes)
{
// Remove the product from the inventory and update the table
Inventory.removeProduct(Inventory.Products.IndexOf(selected));
updateProductsTable();
}
// Check the tables' status and update the buttons' status
checkTableStatus();
}
// Method for parts search
private void btnPartsSearch_Click(object sender, EventArgs e)
{
// Get the string from the users' input
string searchTerm = inputPartSearch.Text;
// If the string is just whitespace
if (string.IsNullOrWhiteSpace(searchTerm))
{
// Clear the search box and update the table to display all parts again
inputPartSearch.Text = string.Empty;
updatePartsTable();
return;
}
// If the user did not enter valid text
else if (!searchTerm.All(x => Char.IsLetterOrDigit(x) || x == ' '))
{
partNotFound();
return;
}
// If text is valid, call the search parts method
var result = searchParts(searchTerm, Inventory.AllParts);
// If the result returns null or is empty
if (result == null || result.ToArray().Count() < 1)
{
partNotFound();
return;
}
// If there are results
else
{
// Set the partsDataView source as the search results
partsDataView.DataSource = result.ToArray();
// Select the first result, update, and refresh the view to display results
partsDataView.Rows[0].Selected = true;
partsDataView.Update();
partsDataView.Refresh();
}
}
// Method to clear the parts search and reset the parts table
private void clearPartSearch_Click(object sender, EventArgs e)
{
inputPartSearch.Text = string.Empty;
updatePartsTable();
}
// Method to search the products table
private void btnProductsSearch_Click(object sender, EventArgs e)
{
// Get the string from the user input
string searchTerm = inputProductSearch.Text;
// If the string is whitespace or empty
if (string.IsNullOrWhiteSpace(searchTerm))
{
// Clear the search box and reset the table
inputProductSearch.Text = string.Empty;
updateProductsTable();
return;
}
// If the user did not enter valid text
else if (!searchTerm.All(x => Char.IsLetterOrDigit(x) || x == ' '))
{
productNotFound();
return;
}
// If the text is valid, call the searchProducts method
var result = searchProducts(searchTerm, Inventory.Products);
// If the result is null or empty
if (result == null || result.ToArray().Count() < 1)
{
productNotFound();
return;
}
// If there are results
else
{
// Set the results as the productsDataView source
productsDataView.DataSource = result.ToArray();
// Select the top result, update, and refresh the table
productsDataView.Rows[0].Selected = true;
productsDataView.Update();
productsDataView.Refresh();
}
}
// Method to clear the product search and reset the table
private void clearProductSearch_Click(object sender, EventArgs e)
{
inputProductSearch.Text = string.Empty;
updateProductsTable();
}
// Method to exit the program when pressing the exit button
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
// Check if a part is associated with a product
private bool checkAssociation(Part part)
{
bool result = false;
// Create a list to store products associated with the part
List<String> productsWithParts = new List<string>();
// Check all products that might be associated with the part
foreach (Product product in Inventory.Products)
{
var existingPart = product.lookupAssociatedPart(part.PartID);
// If the returned results are not null, add the product to the list
// set result to true so the part will not be deleted
if (existingPart != null)
{
// Add the product name to the list to display to user
productsWithParts.Add(product.Name);
result = true;
}
}
// If the part was found attatched to a product
if (result)
{
// Create a string to store product names
String productNames = "";
// Append each product name to the string
foreach (String name in productsWithParts) { productNames += $"{name}\n"; }
// Notify the user
MessageBox.Show(
"This part can not be deleted." +
"\nIt is associated with the following products:\n" +
$"{productNames}"
);
}
// return the result (If the part was found attatched to a product)
return result;
}
// When MainDisplay is finished loading, check for empty tables
private void MainDisplay_Load(object sender, EventArgs e)
{
checkTableStatus();
}
// Method for search the parts table
private IOrderedEnumerable<Part> searchParts(String term, BindingList<Part> partsList)
{
// First, check if an integer was entered
if (int.TryParse(term, out int id))
{
// Use LINQ to search for matching product ids
var result =
from part in partsList
where part.PartID == id
orderby part.PartID
select part;
return result;
}
// If not an integer, make sure it is a valid string and search for a match
else if (term.All(x => Char.IsLetter(x) || x == ' '))
{
// Use LINQ to search for matching part names
var result =
from part in partsList
where part.Name.ToUpper().Contains(term.ToUpper())
orderby part.Name.ToUpper()
select part;
return result;
}
return null;
}
// Method for searching the products table
private IOrderedEnumerable<Product> searchProducts(String term, BindingList<Product> productsList)
{
// First, check if an integer was entered
if (int.TryParse(term, out int id))
{
// Use LINQ to search for matching product ids
var result =
from product in productsList
where product.ProductID == id
orderby product.ProductID
select product;
return result;
}
// If not an integer, make sure it is a valid string and search for a match
else if (term.All(x => Char.IsLetter(x) || x == ' '))
{
// Use LINQ to search for matching product names
var result =
from product in productsList
where product.Name.ToUpper().Contains(term.ToUpper())
orderby product.Name.ToUpper()
select product;
return result;
}
return null;
}
// Error to display if the part is not found
private void partNotFound()
{
MessageBox.Show("Part not found", "Error");
inputPartSearch.Text = string.Empty;
updatePartsTable();
}
// Error to display if the product is not found
private void productNotFound()
{
MessageBox.Show("Product not found", "Error");
inputProductSearch.Text = string.Empty;
updateProductsTable();
}
}
}