-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProducts.cs
More file actions
413 lines (365 loc) · 16.8 KB
/
Products.cs
File metadata and controls
413 lines (365 loc) · 16.8 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using InventoryApp.Models;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace InventoryApp
{
public partial class Products : Form
{
private readonly BindingList<Part> allParts;
private readonly BindingList<Product> allProducts;
private readonly Product editingProduct;
private readonly bool isEditMode;
private readonly BindingList<Part> associatedParts = new BindingList<Part>();
private Product productToModify;
private readonly int _newProductId;
private BindingList<Part> availableParts;
private MainForm _mainForm;
public Products(MainForm mainForm, BindingList<Product> products, BindingList<Part> parts, object thirdArg = null)
{
InitializeComponent();
allProducts = products;
allParts = parts;
_mainForm = mainForm;
if (thirdArg is Product productToEdit)
{
isEditMode = true;
editingProduct = productToEdit;
}
else if (thirdArg is int newProductId)
{
isEditMode = false;
_newProductId = newProductId;
TextBoxProductID.Text = newProductId.ToString();
}
SetupPartsDataGrid();
SetupAssociatedPartsDataGrid();
StyleDataGrids();
if (isEditMode)
{
TextBoxProductID.Text = editingProduct.ProductID.ToString();
TextBoxName.Text = editingProduct.Name;
TextBoxInventory.Text = editingProduct.InStock.ToString();
TextBoxPrice.Text = editingProduct.Price.ToString();
TextBoxMin.Text = editingProduct.Min.ToString();
TextBoxMax.Text = editingProduct.Max.ToString();
TextBoxProductID.Enabled = false;
foreach (var part in editingProduct.AssociatedParts)
{
associatedParts.Add(part);
}
Text = "Modify Product";
labelProduct.Text = "Modify Product";
}
else
{
Text = "Add Product";
labelProduct.Text = "Add Product";
}
DataGridViewParts.CellMouseEnter += DataGridViewParts_CellMouseEnter; // Makes hover color work
DataGridViewParts.CellMouseLeave += DataGridViewParts_CellMouseLeave; // Makes hover color work
DataGridView2.CellMouseEnter += DataGridViewProducts_CellMouseEnter; // Makes hover color work
DataGridView2.CellMouseLeave += DataGridViewProducts_CellMouseLeave; // Makes hover color work
this.FormClosed += (sender, e) => _mainForm.ClearSearchBox();
}
private void SetupPartsDataGrid()
{
DataGridViewParts.AutoGenerateColumns = false;
DataGridViewParts.Columns.Clear();
DataGridViewParts.CellDoubleClick += DataGridViewParts_CellDoubleClick;
DataGridViewParts.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "ID", DataPropertyName = "ID" });
DataGridViewParts.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "Name", DataPropertyName = "Name" });
DataGridViewParts.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "Price", DataPropertyName = "Price" });
DataGridViewParts.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "In Stock", DataPropertyName = "InStock" });
// Create availableParts as a working list (without modifying allParts)
if (isEditMode)
{
var associatedIDs = editingProduct.AssociatedParts.Select(ap => ap.ID).ToHashSet();
var filtered = allParts.Where(p => !associatedIDs.Contains(p.ID)).ToList();
availableParts = new BindingList<Part>(filtered);
}
else
{
availableParts = new BindingList<Part>(allParts.ToList()); // Clone
}
DataGridViewParts.DataSource = availableParts;
}
private void SetupAssociatedPartsDataGrid()
{
DataGridView2.AutoGenerateColumns = false;
DataGridView2.Columns.Clear();
DataGridView2.DataSource = associatedParts;
DataGridView2.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "ID", DataPropertyName = "ID" });
DataGridView2.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "Name", DataPropertyName = "Name" });
DataGridView2.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "Price", DataPropertyName = "Price" });
DataGridView2.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "In Stock", DataPropertyName = "InStock" });
}
private void StyleDataGrids()
{
DataGridView[] grids = { DataGridViewParts, DataGridView2 };
foreach (var grid in grids)
{
grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
grid.MultiSelect = false;
grid.DefaultCellStyle.SelectionBackColor = Color.MediumPurple;
grid.DefaultCellStyle.SelectionForeColor = Color.White;
grid.RowsDefaultCellStyle.SelectionBackColor = Color.MediumPurple;
grid.RowsDefaultCellStyle.SelectionForeColor = Color.White;
grid.EnableHeadersVisualStyles = false;
}
}
// 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.MediumPurple;
}
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)
DataGridView2.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.MediumPurple;
}
// PRODUCTS: Cool color effect on rows when mouse-hovering Products Table
private void DataGridViewProducts_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
DataGridView2.Rows[e.RowIndex].DefaultCellStyle.BackColor = DataGridView2.DefaultCellStyle.BackColor;
}
// Code to make the Save Button work
private void ButtonSave_Click(object sender, EventArgs e)
{
try
{
string name = TextBoxName.Text.Trim();
string inventoryText = TextBoxInventory.Text.Trim();
string priceText = TextBoxPrice.Text.Trim();
string minText = TextBoxMin.Text.Trim();
string maxText = TextBoxMax.Text.Trim();
if (string.IsNullOrWhiteSpace(name) ||
string.IsNullOrWhiteSpace(inventoryText) ||
string.IsNullOrWhiteSpace(priceText) ||
string.IsNullOrWhiteSpace(minText) ||
string.IsNullOrWhiteSpace(maxText))
{
MessageBox.Show("All fields must be filled.");
return;
}
// Conditions
if (name.Any(char.IsDigit))
{
MessageBox.Show("The \"Name\" field cannot contain numbers.");
return;
}
if (name.Length > 20)
{
MessageBox.Show("The \"Name\" field must be fewer than 20 characters.");
return;
}
if (!int.TryParse(inventoryText, out int inventory) || inventory < 0)
{
MessageBox.Show("Please enter a valid, non-negative integer for \"Inventory\" field.");
return;
}
if (!decimal.TryParse(priceText, out decimal price) || price < 0)
{
MessageBox.Show("Please enter a valid, non-negative decimal number without symbols for \"Price\" field.");
return;
}
if (!int.TryParse(minText, out int min) || min < 0)
{
MessageBox.Show("Please enter a valid, non-negative integer for \"Minimum\" field.");
return;
}
if (!int.TryParse(maxText, out int max) || max < 0)
{
MessageBox.Show("Please enter a valid, non-negative integer for \"Maximum\" field.");
return;
}
if (min > max)
{
MessageBox.Show("\"Minimum\" number entered cannot be greater than current value of \"Maximum\"");
return;
}
if (inventory < min || inventory > max)
{
MessageBox.Show("\"Inventory\" value must be between \"Min\" and \"Max\" values.");
return;
}
int id = isEditMode ? int.Parse(TextBoxProductID.Text) : _newProductId;
// Code for the Product object
Product newProduct = new Product(id, name, price, inventory, min, max);
foreach (var part in associatedParts)
{
newProduct.AddAssociatedPart(part);
}
if (isEditMode)
{
editingProduct.Name = name;
editingProduct.InStock = inventory;
editingProduct.Price = price;
editingProduct.Min = min;
editingProduct.Max = max;
editingProduct.ClearAssociatedParts();
foreach (var part in associatedParts)
{
editingProduct.AddAssociatedPart(part);
}
}
else
{
// Add a new product
allProducts.Add(newProduct);
}
this.DialogResult = DialogResult.OK;
this.Close(); // Close the form after saving
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}");
}
}
// Code to make the enter button work in search box
private void TextBoxSearchParts_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
e.Handled = true;
string searchText = TextBoxSearchParts.Text.Trim().ToLower();
var filtered = allParts
.Where(p => p.Name.ToLower().Contains(searchText) || p.ID.ToString() == searchText)
.ToList();
if (filtered.Count > 0)
{
DataGridViewParts.DataSource = new BindingList<Part>(filtered);
}
else
{
MessageBox.Show("No matching parts found.");
DataGridViewParts.DataSource = allParts;
}
}
}
// Code to make the Cancel Button work
private void ButtonCancel_Click(object sender, EventArgs e)
{
this.Close();
}
// Code to add the chosen part to the product and bottom grid
private void ButtonAddAssociatedPart_Click(object sender, EventArgs e)
{
if (DataGridViewParts.CurrentRow?.DataBoundItem is Part selectedPart &&
!associatedParts.Contains(selectedPart))
{
associatedParts.Add(selectedPart);
}
}
// Code to remove a part from a product
private void ButtonRemoveAssociatedPart_Click(object sender, EventArgs e)
{
if (DataGridView2.CurrentRow?.DataBoundItem is Part selectedPart)
{
associatedParts.Remove(selectedPart);
if (!availableParts.Any(p => p.ID == selectedPart.ID))
{
availableParts.Add(selectedPart);
}
}
}
// Code for the Search Button
private void ButtonSearch_Click(object sender, EventArgs e)
{
string search = TextBoxSearchParts.Text.Trim().ToLower();
var filtered = allParts.Where(p =>
p.Name.ToLower().Contains(search) || p.ID.ToString() == search).ToList();
DataGridViewParts.DataSource = new BindingList<Part>(filtered);
}
// Code to for live search
private void TextBoxSearch_TextChanged(object sender, EventArgs e)
{
if (sender is System.Windows.Forms.TextBox searchBox)
{
string searchText = searchBox.Text.Trim().ToLower();
var filtered = allParts
.Where(p => !associatedParts.Contains(p) &&
(p.Name.ToLower().Contains(searchText) || p.ID.ToString().Contains(searchText)))
.ToList();
availableParts.Clear();
foreach (var part in filtered)
{
availableParts.Add(part);
}
}
}
// Code to allow double clicking to select a part
private void DataGridViewParts_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.RowIndex < DataGridViewParts.Rows.Count)
{
var selectedPart = DataGridViewParts.Rows[e.RowIndex].DataBoundItem as Part;
if (selectedPart != null)
{
AddAssociatedPart(selectedPart);
}
}
}
// Code to allow selecting when clicking a part
private void DataGridViewParts_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && DataGridViewParts.Rows[e.RowIndex].DataBoundItem is Part selectedPart)
{
if (!isEditMode) // You can keep this check if needed
{
AddAssociatedPart(selectedPart);
}
}
}
// Code for the Associated Part grid
private void AddAssociatedPart(Part part)
{
if (!associatedParts.Any(p => p.ID == part.ID))
{
associatedParts.Add(part);
// REMOVE BY ID FROM THE availableParts LIST ITSELF
var toRemove = availableParts.FirstOrDefault(p => p.ID == part.ID);
if (toRemove != null)
{
availableParts.Remove(toRemove);
}
else
{
MessageBox.Show("Part not found in availableParts for removal.");
}
}
}
private void Products_Load(object sender, EventArgs e)
{
var lavenderBlush = Color.LavenderBlush;
DataGridViewParts.BackgroundColor = lavenderBlush;
DataGridViewParts.DefaultCellStyle.BackColor = lavenderBlush;
DataGridViewParts.ColumnHeadersDefaultCellStyle.BackColor = lavenderBlush;
DataGridViewParts.ColumnHeadersDefaultCellStyle.SelectionBackColor = lavenderBlush;
DataGridViewParts.EnableHeadersVisualStyles = false;
DataGridView2.BackgroundColor = lavenderBlush;
DataGridView2.DefaultCellStyle.BackColor = lavenderBlush;
DataGridView2.ColumnHeadersDefaultCellStyle.BackColor = lavenderBlush;
DataGridView2.ColumnHeadersDefaultCellStyle.SelectionBackColor = lavenderBlush;
DataGridView2.EnableHeadersVisualStyles = false;
TextBoxSearchParts.KeyDown += TextBoxSearchParts_KeyDown;
}
}
}