From 4de28ea29c165e554d36f290163d827cd684480d Mon Sep 17 00:00:00 2001 From: chrisbell Date: Thu, 15 May 2025 12:01:30 -0500 Subject: [PATCH] Part validation complete --- .../Views/AddModifyPartScreen.Designer.cs | 15 ++ C968Project/Views/AddModifyPartScreen.cs | 241 +++++++++++++++++- C968Project/Views/AddModifyPartScreen.resx | 3 + C968Project/Views/MainScreen.cs | 1 + 4 files changed, 256 insertions(+), 4 deletions(-) diff --git a/C968Project/Views/AddModifyPartScreen.Designer.cs b/C968Project/Views/AddModifyPartScreen.Designer.cs index 5ae12f3..c33be6d 100644 --- a/C968Project/Views/AddModifyPartScreen.Designer.cs +++ b/C968Project/Views/AddModifyPartScreen.Designer.cs @@ -28,6 +28,7 @@ /// private void InitializeComponent() { + components = new System.ComponentModel.Container(); screenLabel = new Label(); inHouseRadioButton = new RadioButton(); outsourcedRadioButton = new RadioButton(); @@ -47,6 +48,8 @@ minTextBox = new TextBox(); saveButton = new Button(); cancelButton = new Button(); + errorProvider1 = new ErrorProvider(components); + ((System.ComponentModel.ISupportInitialize)errorProvider1).BeginInit(); SuspendLayout(); // // screenLabel @@ -115,6 +118,7 @@ nameTextBox.Name = "nameTextBox"; nameTextBox.Size = new Size(153, 23); nameTextBox.TabIndex = 4; + nameTextBox.Validating += nameTextBox_Validating; // // inventoryLabel // @@ -131,6 +135,7 @@ inventoryTextBox.Name = "inventoryTextBox"; inventoryTextBox.Size = new Size(153, 23); inventoryTextBox.TabIndex = 6; + inventoryTextBox.Validating += inventoryTextBox_Validating; // // priceCostLabel // @@ -147,6 +152,7 @@ priceCostTextBox.Name = "priceCostTextBox"; priceCostTextBox.Size = new Size(153, 23); priceCostTextBox.TabIndex = 8; + priceCostTextBox.Validating += priceCostTextBox_Validating; // // machineCompanyLabel // @@ -163,6 +169,7 @@ machineCompanyTextBox.Name = "machineCompanyTextBox"; machineCompanyTextBox.Size = new Size(153, 23); machineCompanyTextBox.TabIndex = 10; + machineCompanyTextBox.Validating += machineCompanyTextBox_Validating; // // label6 // @@ -179,6 +186,7 @@ maxTextBox.Name = "maxTextBox"; maxTextBox.Size = new Size(69, 23); maxTextBox.TabIndex = 12; + maxTextBox.Validating += maxTextBox_Validating; // // label7 // @@ -195,6 +203,7 @@ minTextBox.Name = "minTextBox"; minTextBox.Size = new Size(69, 23); minTextBox.TabIndex = 14; + minTextBox.Validating += minTextBox_Validating; // // saveButton // @@ -216,6 +225,10 @@ cancelButton.UseVisualStyleBackColor = true; cancelButton.Click += cancelButton_Click; // + // errorProvider1 + // + errorProvider1.ContainerControl = this; + // // AddModifyPartScreen // AutoScaleDimensions = new SizeF(7F, 15F); @@ -244,6 +257,7 @@ MinimumSize = new Size(609, 440); Name = "AddModifyPartScreen"; Text = "Part"; + ((System.ComponentModel.ISupportInitialize)errorProvider1).EndInit(); ResumeLayout(false); PerformLayout(); } @@ -269,5 +283,6 @@ private TextBox minTextBox; private Button saveButton; private Button cancelButton; + private ErrorProvider errorProvider1; } } \ No newline at end of file diff --git a/C968Project/Views/AddModifyPartScreen.cs b/C968Project/Views/AddModifyPartScreen.cs index e909937..5f7f079 100644 --- a/C968Project/Views/AddModifyPartScreen.cs +++ b/C968Project/Views/AddModifyPartScreen.cs @@ -17,6 +17,16 @@ namespace C968Project.Views private Part? _selectedPart = null; private Mode _mode = Mode.INHOUSE; + private bool _isNameValid = false; + private bool _isInventoryValid = false; + private bool _isPriceValid = false; + private bool _isMaxValid = false; + private bool _isMinValid = false; + private bool _isMachineOrCompanyValid = false; + + private int _validatedMin = 0; + private int _validatedMax = 0; + public AddModifyPartScreen(ScreenOption screenOption, Part? part = null) { InitializeComponent(); @@ -28,11 +38,15 @@ namespace C968Project.Views { screenLabel.Text = "Add Part"; ChangeMode(Mode.INHOUSE); + ValidateInputs(); } else if (screenOption == ScreenOption.MODIFY) { screenLabel.Text = "Modify Part"; PopulatePartData(_selectedPart); + inHouseRadioButton.Enabled = false; + outsourcedRadioButton.Enabled = false; + ValidateInputs(); } } @@ -122,15 +136,21 @@ namespace C968Project.Views { if (_screenOption is ScreenOption.ADD) { - AddPart(); + if (ValidateInputs()) + { + AddPart(); + Close(); + } } if (_screenOption is ScreenOption.MODIFY) { - ModifyPart(); + if (ValidateInputs()) + { + ModifyPart(); + Close(); + } } - - Close(); } private void cancelButton_Click(object sender, EventArgs e) @@ -163,6 +183,8 @@ namespace C968Project.Views outsourcedRadioButton.Checked = true; machineCompanyLabel.Text = "Company Name"; } + + ValidateMachineOrCompany(); } enum Mode @@ -170,5 +192,216 @@ namespace C968Project.Views INHOUSE, OUTSOURCED } + + private bool ValidateInputs() + { + ValidateName(); + ValidateInventory(); + ValidatePrice(); + ValidateMax(); + ValidateMin(); + ValidateMachineOrCompany(); + + return _isNameValid && _isInventoryValid && _isPriceValid && _isMaxValid && _isMinValid && _isMachineOrCompanyValid; + } + + private void SetTextBoxValidationState(TextBox textBox, bool isValid) + { + if (isValid) + { + textBox.BackColor = Color.White; + } + else + { + textBox.BackColor = Color.Red; + } + } + + private void ValidateName() + { + if (nameTextBox.Text == string.Empty) + { + errorProvider1.SetError(nameTextBox, "Please enter a part name"); + _isNameValid = false; + } + else + { + errorProvider1.SetError(nameTextBox, ""); + _isNameValid = true; + } + + SetTextBoxValidationState(nameTextBox, _isNameValid); + } + + private void nameTextBox_Validating(object sender, CancelEventArgs e) + { + ValidateName(); + } + + private void ValidateInventory() + { + int inventory = 0; + + if (inventoryTextBox.Text != string.Empty && int.TryParse(inventoryTextBox.Text, out inventory)) + { + if (_isMinValid && _isMaxValid) + { + if (inventory > _validatedMax) + { + errorProvider1.SetError(inventoryTextBox, $"Must be less than Max: {_validatedMax}"); + _isInventoryValid = false; + } + else if (inventory < _validatedMin) + { + errorProvider1.SetError(inventoryTextBox, $"Must be greater than Min: {_validatedMin}"); + _isInventoryValid = false; + } + else + { + errorProvider1.SetError(inventoryTextBox, ""); + _isInventoryValid = true; + } + } + else + { + errorProvider1.SetError(inventoryTextBox, $"Min and Max values must be set"); + _isInventoryValid = false; + } + } + else + { + _isInventoryValid = false; + errorProvider1.SetError(inventoryTextBox, "Field must contain an integer value"); + } + + SetTextBoxValidationState(inventoryTextBox, _isInventoryValid); + } + + private void inventoryTextBox_Validating(object sender, CancelEventArgs e) + { + ValidateInventory(); + } + + private void ValidatePrice() + { + float price = 0f; + + if (priceCostTextBox.Text != string.Empty && float.TryParse(priceCostTextBox.Text, out price)) + { + _isPriceValid = true; + errorProvider1.SetError(priceCostTextBox, ""); + } + else + { + _isPriceValid = false; + errorProvider1.SetError(priceCostTextBox, "Field must contain a floating point number"); + } + + SetTextBoxValidationState(priceCostTextBox, _isPriceValid); + } + + private void priceCostTextBox_Validating(object sender, CancelEventArgs e) + { + ValidatePrice(); + } + + private void ValidateMax() + { + int max = 0; + if (maxTextBox.Text != string.Empty && int.TryParse(maxTextBox.Text, out max)) + { + _isMaxValid = true; + errorProvider1.SetError(maxTextBox, ""); + _validatedMax = max; + } + else + { + _isMaxValid = false; + errorProvider1.SetError(maxTextBox, "Field must contain an integer value"); + } + + ValidateInventory(); + SetTextBoxValidationState(maxTextBox, _isMaxValid); + } + + private void maxTextBox_Validating(object sender, CancelEventArgs e) + { + ValidateMax(); + } + + private void ValidateMin() + { + int min = 0; + if (minTextBox.Text != string.Empty && int.TryParse(minTextBox.Text, out min)) + { + _isMinValid = true; + errorProvider1.SetError(minTextBox, ""); + _validatedMin = min; + } + else + { + _isMinValid = false; + errorProvider1.SetError(minTextBox, "Field must contain an integer value"); + } + + ValidateInventory(); + SetTextBoxValidationState(minTextBox, _isMinValid); + } + + private void minTextBox_Validating(object sender, CancelEventArgs e) + { + ValidateMin(); + } + + private void ValidateMachineOrCompany() + { + if (machineCompanyTextBox.Text == string.Empty) + { + _isMachineOrCompanyValid = false; + + if (_mode is Mode.INHOUSE) + { + errorProvider1.SetError(machineCompanyTextBox, "Field must contain an integer value"); + } + + if (_mode is Mode.OUTSOURCED) + { + errorProvider1.SetError(machineCompanyTextBox, "Field must contain a value"); + } + } + else + { + if (_mode is Mode.INHOUSE) + { + int machineId = 0; + if (int.TryParse(machineCompanyTextBox.Text, out machineId)) + { + _isMachineOrCompanyValid = true; + errorProvider1.SetError(machineCompanyTextBox, ""); + } + else + { + _isMachineOrCompanyValid = false; + errorProvider1.SetError(machineCompanyTextBox, "Field must contain an integer value"); + } + } + + if (_mode is Mode.OUTSOURCED) + { + if (machineCompanyTextBox.Text != string.Empty) + { + _isMachineOrCompanyValid = true; + errorProvider1.SetError(machineCompanyTextBox, ""); + } + } + } + + SetTextBoxValidationState(machineCompanyTextBox, _isMachineOrCompanyValid); + } + + private void machineCompanyTextBox_Validating(object sender, CancelEventArgs e) + { + ValidateMachineOrCompany(); + } } } diff --git a/C968Project/Views/AddModifyPartScreen.resx b/C968Project/Views/AddModifyPartScreen.resx index 42d55f9..6bcea65 100644 --- a/C968Project/Views/AddModifyPartScreen.resx +++ b/C968Project/Views/AddModifyPartScreen.resx @@ -117,6 +117,9 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + True diff --git a/C968Project/Views/MainScreen.cs b/C968Project/Views/MainScreen.cs index afc8503..a900f6f 100644 --- a/C968Project/Views/MainScreen.cs +++ b/C968Project/Views/MainScreen.cs @@ -24,6 +24,7 @@ public partial class MainScreen : Form private void partsModifyButton_Click(object sender, EventArgs e) { + if (partsDataGridView.CurrentCell is null) return; Part part = Program.Inventory.LookupPart(partsDataGridView.CurrentCell.RowIndex); if (part is null) return; AddModifyPartScreen partScreen = new AddModifyPartScreen(ScreenOption.MODIFY, part);