Product screen input validation complete

Inputs are now validated and will now allow saving if they are bad.
This commit is contained in:
Chris Bell 2025-05-15 21:20:28 -05:00
parent 9e83e28d4b
commit d561ba39f8
3 changed files with 188 additions and 9 deletions

View File

@ -28,6 +28,7 @@
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
screenLabel = new Label();
idLabel = new Label();
idTextBox = new TextBox();
@ -51,8 +52,10 @@
deleteButton = new Button();
cancelButton = new Button();
saveButton = new Button();
errorProvider1 = new ErrorProvider(components);
((System.ComponentModel.ISupportInitialize)allPartsDataGridView).BeginInit();
((System.ComponentModel.ISupportInitialize)associatedPartsDataGridView).BeginInit();
((System.ComponentModel.ISupportInitialize)errorProvider1).BeginInit();
SuspendLayout();
//
// screenLabel
@ -97,6 +100,7 @@
nameTextBox.Name = "nameTextBox";
nameTextBox.Size = new Size(153, 23);
nameTextBox.TabIndex = 5;
nameTextBox.TextChanged += nameTextBox_TextChanged;
//
// label2
//
@ -113,6 +117,7 @@
inventoryTextBox.Name = "inventoryTextBox";
inventoryTextBox.Size = new Size(153, 23);
inventoryTextBox.TabIndex = 7;
inventoryTextBox.TextChanged += inventoryTextBox_TextChanged;
//
// label3
//
@ -129,6 +134,7 @@
priceTextBox.Name = "priceTextBox";
priceTextBox.Size = new Size(153, 23);
priceTextBox.TabIndex = 9;
priceTextBox.TextChanged += priceTextBox_TextChanged;
//
// label7
//
@ -145,6 +151,7 @@
minTextBox.Name = "minTextBox";
minTextBox.Size = new Size(69, 23);
minTextBox.TabIndex = 18;
minTextBox.TextChanged += minTextBox_TextChanged;
//
// label6
//
@ -161,6 +168,7 @@
maxTextBox.Name = "maxTextBox";
maxTextBox.Size = new Size(69, 23);
maxTextBox.TabIndex = 16;
maxTextBox.TextChanged += maxTextBox_TextChanged;
//
// searchButton
//
@ -252,6 +260,10 @@
saveButton.UseVisualStyleBackColor = true;
saveButton.Click += saveButton_Click;
//
// errorProvider1
//
errorProvider1.ContainerControl = this;
//
// AddModifyProductScreen
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -286,6 +298,7 @@
Text = "Product";
((System.ComponentModel.ISupportInitialize)allPartsDataGridView).EndInit();
((System.ComponentModel.ISupportInitialize)associatedPartsDataGridView).EndInit();
((System.ComponentModel.ISupportInitialize)errorProvider1).EndInit();
ResumeLayout(false);
PerformLayout();
}
@ -315,5 +328,6 @@
private Button deleteButton;
private Button cancelButton;
private Button saveButton;
private ErrorProvider errorProvider1;
}
}

View File

@ -15,6 +15,15 @@ namespace C968Project.Views
private ScreenOption _screenOption;
private Product? _currentProduct;
private bool _isNameValid = false;
private bool _isInventoryValid = false;
private bool _isPriceValid = false;
private bool _isMinValid = false;
private bool _isMaxValid = false;
private int _validatedMin = 0;
private int _validatedMax = 0;
public AddModifyProductScreen(ScreenOption screenOption, Product? product = null)
{
@ -35,6 +44,8 @@ namespace C968Project.Views
}
allPartsDataGridView.DataSource = Program.Inventory.Parts;
ValidateInputs();
}
private void PopulateProductData(Product product)
@ -106,16 +117,19 @@ namespace C968Project.Views
private void saveButton_Click(object sender, EventArgs e)
{
if (_screenOption is ScreenOption.ADD)
if (ValidateInputs())
{
AddProduct();
Close();
}
if (_screenOption is ScreenOption.ADD)
{
AddProduct();
Close();
}
if (_screenOption is ScreenOption.MODIFY)
{
ModifyProduct();
Close();
if (_screenOption is ScreenOption.MODIFY)
{
ModifyProduct();
Close();
}
}
}
@ -135,7 +149,7 @@ namespace C968Project.Views
Part part = Program.Inventory.LookupPart(allPartsDataGridView.CurrentCell.RowIndex);
if (part is null) return;
if (_currentProduct.AssociatedParts.Contains(part))
{
@ -154,6 +168,7 @@ namespace C968Project.Views
return;
}
if (!Helpers.ConfirmDelete()) return;
Part part = _currentProduct.LookupAssociatedPart(associatedPartsDataGridView.CurrentCell.RowIndex);
if (part is null || !_currentProduct.AssociatedParts.Contains(part))
@ -164,5 +179,152 @@ namespace C968Project.Views
_currentProduct.RemoveAssociatedPart(_currentProduct.AssociatedParts.IndexOf(part));
}
private void nameTextBox_TextChanged(object sender, EventArgs e)
{
ValidateName();
}
private void ValidateName()
{
if (nameTextBox.Text == string.Empty)
{
errorProvider1.SetError(nameTextBox, "Please enter a product name");
_isNameValid = false;
}
else
{
errorProvider1.SetError(nameTextBox, "");
_isNameValid = true;
}
Helpers.SetTextBoxValidationState(nameTextBox, _isNameValid);
}
private void inventoryTextBox_TextChanged(object sender, EventArgs e)
{
ValidateInventory();
}
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");
}
Helpers.SetTextBoxValidationState(inventoryTextBox, _isInventoryValid);
}
private void priceTextBox_TextChanged(object sender, EventArgs e)
{
ValidatePrice();
}
private void ValidatePrice()
{
float price = 0f;
if (priceTextBox.Text != string.Empty && float.TryParse(priceTextBox.Text, out price))
{
_isPriceValid = true;
errorProvider1.SetError(priceTextBox, "");
}
else
{
_isPriceValid = false;
errorProvider1.SetError(priceTextBox, "Field must contain a floating point number");
}
Helpers.SetTextBoxValidationState(priceTextBox, _isPriceValid);
}
private void maxTextBox_TextChanged(object sender, EventArgs e)
{
ValidateMax();
}
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();
Helpers.SetTextBoxValidationState(maxTextBox, _isMaxValid);
}
private void minTextBox_TextChanged(object sender, EventArgs e)
{
ValidateMin();
}
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();
Helpers.SetTextBoxValidationState(minTextBox, _isMinValid);
}
private bool ValidateInputs()
{
ValidateName();
ValidateInventory();
ValidatePrice();
ValidateMin();
ValidateMax();
return _isNameValid && _isInventoryValid && _isPriceValid && _isMinValid && _isMaxValid;
}
}
}

View File

@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="errorProvider1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="$this.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>