-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
534 lines (453 loc) · 23.7 KB
/
Copy pathMainForm.cs
File metadata and controls
534 lines (453 loc) · 23.7 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
531
532
533
534
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using InventoryApp.Models;
namespace InventoryApp
{
public partial class MainForm : Form
{
private BindingList<Product> allProducts = new BindingList<Product>();
private int _nextProductId;
private int _nextPartId;
public MainForm()
{
InitializeComponent();
Inventory.InitializeIDs();
TextBoxSearchProducts.TextChanged += TextBoxSearchProducts_TextChanged;
SetupDataGrids();
DataGridViewParts.CellMouseEnter += DataGridViewParts_CellMouseEnter; // Makes hover color work
DataGridViewParts.CellMouseLeave += DataGridViewParts_CellMouseLeave; // Makes hover color work
DataGridViewProducts.CellMouseEnter += DataGridViewProducts_CellMouseEnter; // Makes hover color work
DataGridViewProducts.CellMouseLeave += DataGridViewProducts_CellMouseLeave; // Makes hover color work
DataGridViewParts.CellDoubleClick += DataGridViewParts_CellDoubleClick; // Makes double-clicking work to modify
DataGridViewProducts.CellDoubleClick += DataGridViewProducts_CellDoubleClick; // Makes double-clicking work to modify
DataGridViewParts.KeyDown += DataGridViewParts_KeyDown; // Makes Enter Button work in Parts grid
DataGridViewProducts.KeyDown += DataGridViewProducts_KeyDown; // Makes Enter Button work in Products grid
}
private BindingList<Part> filteredParts;
private BindingList<Product> filteredProducts;
private void SetupDataGrids()
{
filteredParts = new BindingList<Part>(Inventory.Parts.ToList());
filteredProducts = new BindingList<Product>(Inventory.Products.ToList());
ConfigurePartsGrid();
ConfigureProductsGrid();
}
private void ConfigurePartsGrid()
{
DataGridViewParts.Columns.Clear();
DataGridViewParts.AutoGenerateColumns = false;
DataGridViewParts.AllowUserToAddRows = false;
DataGridViewParts.ReadOnly = true;
DataGridViewParts.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
DataGridViewParts.MultiSelect = false;
DataGridViewParts.RowHeadersVisible = false;
DataGridViewParts.ScrollBars = ScrollBars.Vertical;
DataGridViewParts.DefaultCellStyle.BackColor = Color.White;
DataGridViewParts.DefaultCellStyle.SelectionBackColor = Color.SkyBlue;
DataGridViewParts.DefaultCellStyle.SelectionForeColor = Color.White;
DataGridViewParts.DataSource = filteredParts;
DataGridViewParts.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "ID", HeaderText = "ID", AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells });
DataGridViewParts.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "Name", HeaderText = "Name", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill });
DataGridViewParts.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "InStock", HeaderText = "Inventory", AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells });
DataGridViewParts.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "Price", HeaderText = "Price", AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells });
DataGridViewParts.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "Min", HeaderText = "Min", AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells });
DataGridViewParts.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "Max", HeaderText = "Max", AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells });
}
private void ConfigureProductsGrid()
{
DataGridViewProducts.Columns.Clear();
DataGridViewProducts.AutoGenerateColumns = false;
DataGridViewProducts.AllowUserToAddRows = false;
DataGridViewProducts.ReadOnly = true;
DataGridViewProducts.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
DataGridViewProducts.MultiSelect = false;
DataGridViewProducts.RowHeadersVisible = false;
DataGridViewProducts.ScrollBars = ScrollBars.Vertical;
DataGridViewProducts.DefaultCellStyle.BackColor = Color.White;
DataGridViewProducts.DefaultCellStyle.SelectionBackColor = Color.SkyBlue;
DataGridViewProducts.DefaultCellStyle.SelectionForeColor = Color.White;
DataGridViewProducts.DataSource = filteredProducts;
DataGridViewProducts.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "ProductID", HeaderText = "ID", AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells });
DataGridViewProducts.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "Name", HeaderText = "Name", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill });
DataGridViewProducts.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "InStock", HeaderText = "Inventory", AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells });
DataGridViewProducts.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "Price", HeaderText = "Price", AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells });
DataGridViewProducts.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "Min", HeaderText = "Min", AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells });
DataGridViewProducts.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "Max", HeaderText = "Max", AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells });
}
// PARTS: Cool color effect on rows when mouse-hovering Parts Table
private void DataGridViewParts_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
DataGridViewParts.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightBlue;
}
private void DataGridViewParts_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
DataGridViewParts.Rows[e.RowIndex].DefaultCellStyle.BackColor = DataGridViewParts.DefaultCellStyle.BackColor;
}
private void DataGridViewProducts_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
DataGridViewProducts.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightBlue;
}
// PRODUCTS: Cool color effect on rows when mouse-hovering Products Table
private void DataGridViewProducts_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
DataGridViewProducts.Rows[e.RowIndex].DefaultCellStyle.BackColor = DataGridViewProducts.DefaultCellStyle.BackColor;
}
// Code for Parts search Box
private void ButtonSearchParts_Click(object sender, EventArgs e)
{
ApplyPartsFilter();
}
// Parts search box code that makes the ENTER Button work
private void TextBoxSearchParts_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
e.Handled = true;
ButtonSearchParts.PerformClick();
string searchText = TextBoxSearchParts.Text.Trim().ToLower();
var filtered = Inventory.Parts
.Where(p => p.Name.ToLower().Contains(searchText) || p.ID.ToString() == searchText)
.ToList();
if (filtered.Count == 1)
{
var selectedPart = filtered[0];
// Delay showing the modify form to prevent "ding" and UI issues
this.BeginInvoke((MethodInvoker)delegate
{
var modifyForm = new Parts(this, selectedPart); // Pass MainForm (this) and the selected part
modifyForm.ShowDialog();
});
}
else
{
DataGridViewParts.DataSource = new BindingList<Part>(filtered);
}
}
}
private void TextBoxSearchParts_TextChanged(object sender, EventArgs e)
{
ApplyPartsFilter();
}
// Code to make the Parts search box work
private void ApplyPartsFilter()
{
string searchTerm = TextBoxSearchParts.Text.Trim().ToLower();
if (string.IsNullOrWhiteSpace(searchTerm))
{
filteredParts = new BindingList<Part>(Inventory.Parts.ToList());
DataGridViewParts.DataSource = filteredParts;
return;
}
var results = Inventory.Parts
.Where(p => p.ID.ToString().Contains(searchTerm) || p.Name.ToLower().Contains(searchTerm))
.ToList();
filteredParts = new BindingList<Part>(results);
DataGridViewParts.DataSource = filteredParts;
}
public void AddPart(Part part)
{
Inventory.Parts.Add(part);
}
public void UpdatePart(Part updatedPart)
{
var existing = Inventory.LookupPart(updatedPart.ID);
if (existing != null)
{
int index = Inventory.Parts.IndexOf(existing);
Inventory.Parts[index] = updatedPart;
}
}
// Code for Products search Box
private void ButtonSearchProducts_Click(object sender, EventArgs e)
{
ApplyProductsFilter();
}
// Products search box code that makes the ENTER Button work
private void TextBoxSearchProducts_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
e.Handled = true;
ButtonSearchProducts.PerformClick();
string searchText = TextBoxSearchProducts.Text.Trim().ToLower();
var filtered = Inventory.Products
.Where(p => p.Name.ToLower().Contains(searchText) || p.ProductID.ToString() == searchText)
.ToList();
if (filtered.Count == 1)
{
var selectedProduct = filtered[0];
// Delay showing the modify form to prevent "ding" and UI issues
this.BeginInvoke((MethodInvoker)delegate
{
// Instantiate the Products form (used for both adding and modifying products)
var modifyForm = new Products(this, Inventory.Products, Inventory.Parts, selectedProduct);
var result = modifyForm.ShowDialog();
});
TextBoxSearchProducts.Clear();
}
else
{
DataGridViewProducts.DataSource = new BindingList<Product>(filtered);
TextBoxSearchProducts.Clear();
}
}
}
private void TextBoxSearchProducts_TextChanged(object sender, EventArgs e)
{
ApplyProductsFilter();
}
private void ApplyProductsFilter()
{
string searchTerm = TextBoxSearchProducts.Text.Trim().ToLower();
if (string.IsNullOrWhiteSpace(searchTerm))
{
filteredProducts = new BindingList<Product>(Inventory.Products.ToList());
DataGridViewProducts.DataSource = filteredProducts;
return;
}
var results = Inventory.Products
.Where(p => p.ProductID.ToString().Contains(searchTerm) || p.Name.ToLower().Contains(searchTerm))
.ToList();
filteredProducts = new BindingList<Product>(results);
DataGridViewProducts.DataSource = filteredProducts;
}
// Code that opens the Parts form on click
private void ButtonAddPart_Click(object sender, EventArgs e)
{
var addPart = new Parts(this, null, _nextPartId);
var result = addPart.ShowDialog();
if (result == DialogResult.OK)
{
_nextPartId++; // Only increment if a part was successfully added
}
// Code that refreshes the parts grid with new/live data from Inventory
filteredParts = new BindingList<Part>(Inventory.Parts.ToList());
DataGridViewParts.DataSource = filteredParts;
}
// Code to Modify the Parts form on click
private void ButtonModifyPart_Click(object sender, EventArgs e)
{
if (DataGridViewParts.CurrentRow?.DataBoundItem is Part selectedPart)
{
var partForm = new Parts(this, selectedPart); // partToEdit = Modify mode
partForm.ShowDialog();
DataGridViewParts.Refresh(); // Refreshes the DataGridView in case the part was updated
}
else
{
MessageBox.Show("Please select a part to modify.");
}
}
// Code to open the Products form on click to Add
private void ButtonAddProduct_Click(object sender, EventArgs e)
{
var productForm = new Products(this,Inventory.Products, Inventory.Parts, _nextProductId); // Correctly passing the new Product ID
var result = productForm.ShowDialog();
if (result == DialogResult.OK)
{
_nextProductId++; // Only increment if product was saved
}
// Refresh the product DataGridView
filteredProducts = new BindingList<Product>(Inventory.Products.ToList());
DataGridViewProducts.DataSource = filteredProducts;
}
private void ModifySelectedProduct()
{
if (DataGridViewProducts.CurrentRow?.DataBoundItem is Product selectedProduct)
{
var modifyForm = new Products(this, Inventory.Products, Inventory.Parts, selectedProduct);
modifyForm.ShowDialog();
DataGridViewProducts.Refresh(); // Refreshes the DataGridView in case the product was updated
}
else
{
MessageBox.Show("Please select a product to modify.");
}
}
// Code to Modify the Products form on click
private void ButtonModifyProduct_Click(object sender, EventArgs e)
{
if (DataGridViewProducts.CurrentRow?.DataBoundItem is Product selectedProduct)
{
var modifyForm = new Products(this, Inventory.Products, Inventory.Parts, selectedProduct);
modifyForm.ShowDialog();
DataGridViewProducts.Refresh(); // Refreshes the DataGridView in case the product was updated
}
else
{
MessageBox.Show("Please select a part to modify.");
}
}
private void DataGridViewParts_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && DataGridViewParts.Rows[e.RowIndex].DataBoundItem is Part selectedPart)
{
var partsForm = new Parts(this, selectedPart);
partsForm.ShowDialog();
}
}
private void DataGridViewProducts_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.RowIndex < DataGridViewProducts.Rows.Count)
{
var selectedProduct = DataGridViewProducts.Rows[e.RowIndex].DataBoundItem as Product;
if (selectedProduct != null)
{
var modifyForm = new Products(this, Inventory.Products, Inventory.Parts, selectedProduct);
modifyForm.ShowDialog();
DataGridViewProducts.DataSource = new BindingList<Product>(Inventory.Products);
}
}
}
// Code that makes highlighting a row and using Enter key open the Modify Parts form
private void DataGridViewParts_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && DataGridViewParts.CurrentRow?.DataBoundItem is Part)
{
e.SuppressKeyPress = true;
e.Handled = true;
ButtonModifyPart_Click(sender, e); // reuse your existing Modify Part logic
}
}
// Code that makes highlighting a row and using Enter key open the Modify Products form
private void DataGridViewProducts_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
e.Handled = true; // prevents the ding sound
ModifySelectedProduct();
}
}
public void ClearSearchBox()
{
// Clear the search box
TextBoxSearchParts.Clear();
// Reset the data in DataGridView to show all parts
DataGridViewParts.DataSource = new BindingList<Part>(Inventory.Parts);
}
// Code that makes the Parts Delete Button work and ensure Part isn't associated with a Product
private void ButtonDeletePart_Click(object sender, EventArgs e)
{
if (DataGridViewParts.CurrentRow == null)
{
MessageBox.Show("Please select a part to delete.");
return;
}
var selectedPart = (Part)DataGridViewParts.CurrentRow.DataBoundItem;
// Check if this part is associated with any product
bool isAssociated = Inventory.Products.Any(p => p.AssociatedParts.Contains(selectedPart));
if (isAssociated)
{
MessageBox.Show("This part is associated with a product and cannot be deleted.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var result = MessageBox.Show("Are you sure you want to delete this part?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Inventory.Parts.Remove(selectedPart);
filteredParts.Remove(selectedPart); // If you're using filteredParts
}
}
// Code that makes the Products Delete Button work, checks if any parts are associated, then confirms if user is sure
private void ButtonDeleteProduct_Click(object sender, EventArgs e)
{
if (DataGridViewProducts.CurrentRow == null)
{
MessageBox.Show("Please select a product to delete.");
return;
}
var selectedProduct = (Product)DataGridViewProducts.CurrentRow.DataBoundItem;
// Check for associated parts
if (selectedProduct.AssociatedParts.Count > 0)
{
MessageBox.Show("Please \"Modify\" to remove associated parts before deleting product.",
"Delete Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
var result = MessageBox.Show("Are you sure you want to delete this product?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Inventory.Products.Remove(selectedProduct);
ApplyProductsFilter();
}
}
// Code that exits the program
private void ButtonExit_Click(object sender, EventArgs e)
{
if (System.Windows.Forms.Application.MessageLoop)
{
// If WinForms app
System.Windows.Forms.Application.Exit();
}
else
{
// If console app
System.Environment.Exit(1);
}
}
private void MainForm_Load(object sender, EventArgs e)
{
var aliceBlue = Color.AliceBlue;
DataGridViewParts.BackgroundColor = aliceBlue;
DataGridViewParts.DefaultCellStyle.BackColor = aliceBlue;
DataGridViewParts.ColumnHeadersDefaultCellStyle.BackColor = aliceBlue;
DataGridViewParts.ColumnHeadersDefaultCellStyle.SelectionBackColor = aliceBlue;
DataGridViewParts.EnableHeadersVisualStyles = false;
DataGridViewProducts.BackgroundColor = aliceBlue;
DataGridViewProducts.DefaultCellStyle.BackColor = aliceBlue;
DataGridViewProducts.ColumnHeadersDefaultCellStyle.BackColor = aliceBlue;
DataGridViewProducts.ColumnHeadersDefaultCellStyle.SelectionBackColor = aliceBlue;
DataGridViewProducts.EnableHeadersVisualStyles = false;
// Add sample parts
Inventory.Parts.Add(new Inhouse(1, "Wheel", 25.99m, 15, 5, 25, 123));
Inventory.Parts.Add(new Inhouse(2, "Pedal", 9.99m, 11, 5, 25, 124));
Inventory.Parts.Add(new Outsourced(3, "Chain", 4.99m, 10, 5, 25, "BikeCo"));
Inventory.Parts.Add(new Outsourced(4, "Seat", 12.11m, 15, 5, 25, "CycleWorks"));
Inventory.Parts.Add(new Inhouse(5, "Handlebar", 12.99m, 3, 2, 10, 125));
// Add sample products
var redBike = new Product(1, "Red Bicycle", 89.99m, 12, 1, 25); // Red bike
redBike.AddAssociatedPart(Inventory.Parts[0]); // Wheel
redBike.AddAssociatedPart(Inventory.Parts[3]); // Seat
redBike.AddAssociatedPart(Inventory.Parts[4]); // Handlebar
Inventory.Products.Add(redBike);
var yellowBike = new Product(2, "Yellow Bicycle", 79.99m, 8, 1, 25); // Yellow bike
yellowBike.AddAssociatedPart(Inventory.Parts[0]); // Wheel
yellowBike.AddAssociatedPart(Inventory.Parts[1]); // Pedal
yellowBike.AddAssociatedPart(Inventory.Parts[3]); // Seat
Inventory.Products.Add(yellowBike);
var blueBike = new Product(3, "Blue Bicycle", 84.99m, 6, 1, 25); // Blue bike
blueBike.AddAssociatedPart(Inventory.Parts[0]); // Wheel
blueBike.AddAssociatedPart(Inventory.Parts[1]); // Pedal
blueBike.AddAssociatedPart(Inventory.Parts[2]); // Chain
blueBike.AddAssociatedPart(Inventory.Parts[3]); // Seat
Inventory.Products.Add(blueBike);
var partsBindingSource = new BindingSource();
partsBindingSource.DataSource = Inventory.Parts;
DataGridViewParts.DataSource = partsBindingSource;
var productsBindingSource = new BindingSource();
productsBindingSource.DataSource = Inventory.Products;
DataGridViewProducts.DataSource = productsBindingSource;
allProducts = Inventory.Products;
_nextProductId = Inventory.Products.Count > 0
? Inventory.Products.Max(p => p.ProductID) + 1
: 1;
_nextPartId = Inventory.Parts.Count > 0
? Inventory.Parts.Max(p => p.ID) + 1
: 1;
}
}
}