-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParts.cs
More file actions
213 lines (195 loc) · 6.66 KB
/
Parts.cs
File metadata and controls
213 lines (195 loc) · 6.66 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
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 static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Reflection.Emit;
using System.Xml.Linq;
using System.Security.AccessControl;
using System.Runtime.Remoting.Contexts;
using InventoryApp.Models;
namespace InventoryApp
{
public partial class Parts : Form
{
private readonly MainForm _mainForm;
private bool isEditMode;
private Part editingPart = null;
private int _newPartId;
public Parts(MainForm mainForm, Part partToEdit = null, int nextPartId = 1)
{
InitializeComponent();
_mainForm = mainForm;
isEditMode = partToEdit != null;
editingPart = partToEdit;
_newPartId = nextPartId;
// Radio Button handlers
RadioButtonInHouse.CheckedChanged += RadioButtonInHouse_CheckedChanged;
RadioButtonOutsourced.CheckedChanged += RadioButtonOutsourced_CheckedChanged;
if (isEditMode)
{
LoadPartData(partToEdit);
TextBoxID.ReadOnly = true;
this.Text = "Modify Part";
label1.Text = "Modify Part";
}
else
{
TextBoxID.Text = _newPartId.ToString();
this.Text = "Add Part";
label1.Text = "Add Part";
}
this.FormClosed += (sender, e) => _mainForm.ClearSearchBox(); // Add this line to handle form close
}
private void LoadPartData(Part part)
{
TextBoxID.Text = part.ID.ToString();
TextBoxName.Text = part.Name;
TextBoxInventory.Text = part.InStock.ToString();
TextBoxPrice.Text = part.Price.ToString();
TextBoxMin.Text = part.Min.ToString();
TextBoxMax.Text = part.Max.ToString();
if (part is Inhouse inhouse)
{
RadioButtonInHouse.Checked = true;
TextBoxMachineID.Text = inhouse.MachineID.ToString();
TextBoxCompanyName.Text = "";
}
else if (part is Outsourced outsourced)
{
RadioButtonOutsourced.Checked = true;
TextBoxCompanyName.Text = outsourced.CompanyName;
TextBoxMachineID.Text = "";
}
}
// Radio Button 1's code - In House - Machine ID
private void RadioButtonInHouse_CheckedChanged(object sender, EventArgs e)
{
if (RadioButtonInHouse.Checked)
{
TextBoxMachineID.Visible = true;
TextBoxCompanyName.Visible = false;
labelThatChangesWithClick.Text = "Machine ID*";
}
}
// Radio Button 2's code - Outsourced - Company Name
private void RadioButtonOutsourced_CheckedChanged(object sender, EventArgs e)
{
if (RadioButtonOutsourced.Checked)
{
TextBoxMachineID.Visible = false;
TextBoxCompanyName.Visible = true;
labelThatChangesWithClick.Text = "Company*";
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
try
{
string nameText = TextBoxName.Text.Trim();
string inventoryText = TextBoxInventory.Text.Trim();
string priceText = TextBoxPrice.Text.Trim();
string minimum = TextBoxMin.Text.Trim();
string maximum = TextBoxMax.Text.Trim();
string machineIDText = TextBoxMachineID.Text.Trim();
string companyNameText = TextBoxCompanyName.Text.Trim();
if (string.IsNullOrWhiteSpace(nameText) ||
string.IsNullOrWhiteSpace(inventoryText) ||
string.IsNullOrWhiteSpace(priceText) ||
string.IsNullOrWhiteSpace(minimum) ||
string.IsNullOrWhiteSpace(maximum) ||
(RadioButtonInHouse.Checked && string.IsNullOrWhiteSpace(machineIDText)) ||
(RadioButtonOutsourced.Checked && string.IsNullOrWhiteSpace(companyNameText)))
{
MessageBox.Show("All fields must be filled.");
return;
}
if (nameText.Any(char.IsDigit))
{
MessageBox.Show("The \"Name\" field cannot contain numbers.");
return;
}
if (nameText.Length > 20)
{
MessageBox.Show("The \"Name\" 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(minimum, out int min) || min < 0)
{
MessageBox.Show("Please enter a valid, non-negative integer for \"Minimum\" field.");
return;
}
if (!int.TryParse(maximum, 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;
}
if (companyNameText.Length > 20)
{
MessageBox.Show("The \"Company Name\" must be fewer than 20 characters.");
return;
}
int partID = int.Parse(TextBoxID.Text);
Part newPart;
if (RadioButtonInHouse.Checked)
{
if (!int.TryParse(machineIDText, out int machineID) || machineID < 1)
{
MessageBox.Show("Please enter a valid integer for \"Machine ID\" greater than 0.");
return;
}
newPart = new Inhouse(partID, nameText, price, inventory, min, max, machineID);
}
else
{
newPart = new Outsourced(partID, nameText, price, inventory, min, max, companyNameText);
}
if (isEditMode)
{
_mainForm.UpdatePart(newPart); // Replace part by matching ID
}
else
{
_mainForm.AddPart(newPart); // Add new part
}
this.DialogResult = DialogResult.OK;
this.Close(); // Done!
}
catch (Exception ex)
{
MessageBox.Show($"An unexpected error occurred: {ex.Message}");
}
}
// Code that makes the cancel Button work
private void ButtonCancel_Click(object sender, EventArgs e)
{
this.Close(); // Closes the AddPart form
}
}
}