Fixed validation to show errors in a popup window

This commit is contained in:
Chris Bell 2025-05-20 11:16:28 -05:00
parent d561ba39f8
commit ec310b2f6e
14 changed files with 253 additions and 74 deletions

View File

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<_LastSelectedProfileId>C:\Users\chris\Documents\school\csharp1\C968Project\C968Project\Properties\PublishProfiles\FolderProfile.pubxml</_LastSelectedProfileId>
</PropertyGroup>
<ItemGroup>
<Compile Update="Views\AddModifyPartScreen.cs">
<SubType>Form</SubType>

View File

@ -31,5 +31,44 @@ namespace C968Project
return false;
}
public static void SearchDataGridView(DataGridView data, string searchValue)
{
// Search the datagridview items for text and then select the first occurence
data.CurrentCell = null;
string search = searchValue;
if (searchValue == string.Empty)
{
MessageBox.Show("Cant search an empty value", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (data.Rows.Count == 0)
{
MessageBox.Show("Nothing to search for", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
foreach (DataGridViewRow row in data.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.FormattedValue != null && cell.FormattedValue.ToString().ToLower().Contains(search.ToLower(), StringComparison.OrdinalIgnoreCase))
{
data.CurrentCell = cell;
return;
}
}
}
MessageBox.Show("No matching search results", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public static void ShowErrorMessage(string message, string title = "", MessageBoxButtons buttons = MessageBoxButtons.OK)
{
MessageBox.Show(message, title, buttons, MessageBoxIcon.Error);
}
}
}

View File

@ -7,7 +7,7 @@ static class Program
{
public static Inventory Inventory = new();
public static int PartIdCounter = 2;
public static int PartIdCounter = 1;
public static int ProductIdCounter = 1;
private static Part _testInhousePart = new Inhouse()
@ -33,7 +33,7 @@ static class Program
private static Product _testProduct = new Product()
{
Name = "Product",
Name = "One Product",
InStock = 500,
Max = 4000,
Min = 2,
@ -41,19 +41,25 @@ static class Program
ProductId = 0,
};
/// <summary>
/// The main entry point for the application.
/// </summary>
private static Product _testProduct2 = new Product()
{
Name = "Two Product",
InStock = 1,
Max = 1,
Min = 1,
Price = 1.40f,
ProductId = 1,
};
[STAThread]
static void Main()
{
Inventory.AddPart(_testOutsourcedPart);
Inventory.AddPart(_testInhousePart);
Inventory.AddProduct(_testProduct);
Inventory.AddProduct(_testProduct2);
Inventory.Products[0].AddAssociatedPart(Inventory.Parts[0]);
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new MainScreen());
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://go.microsoft.com/fwlink/?LinkID=208121. -->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net9.0-windows\publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://go.microsoft.com/fwlink/?LinkID=208121. -->
<Project>
<PropertyGroup>
<History>True|2025-05-16T03:22:13.0789367Z||;</History>
<LastFailureDetails />
</PropertyGroup>
</Project>

View File

@ -118,7 +118,7 @@
nameTextBox.Name = "nameTextBox";
nameTextBox.Size = new Size(153, 23);
nameTextBox.TabIndex = 4;
nameTextBox.Validating += nameTextBox_Validating;
nameTextBox.TextChanged += nameTextBox_TextChanged;
//
// inventoryLabel
//
@ -135,7 +135,7 @@
inventoryTextBox.Name = "inventoryTextBox";
inventoryTextBox.Size = new Size(153, 23);
inventoryTextBox.TabIndex = 6;
inventoryTextBox.Validating += inventoryTextBox_Validating;
inventoryTextBox.TextChanged += inventoryTextBox_TextChanged;
//
// priceCostLabel
//
@ -152,7 +152,7 @@
priceCostTextBox.Name = "priceCostTextBox";
priceCostTextBox.Size = new Size(153, 23);
priceCostTextBox.TabIndex = 8;
priceCostTextBox.Validating += priceCostTextBox_Validating;
priceCostTextBox.TextChanged += priceCostTextBox_TextChanged;
//
// machineCompanyLabel
//
@ -169,7 +169,7 @@
machineCompanyTextBox.Name = "machineCompanyTextBox";
machineCompanyTextBox.Size = new Size(153, 23);
machineCompanyTextBox.TabIndex = 10;
machineCompanyTextBox.Validating += machineCompanyTextBox_Validating;
machineCompanyTextBox.TextChanged += machineCompanyTextBox_TextChanged;
//
// label6
//
@ -186,7 +186,7 @@
maxTextBox.Name = "maxTextBox";
maxTextBox.Size = new Size(69, 23);
maxTextBox.TabIndex = 12;
maxTextBox.Validating += maxTextBox_Validating;
maxTextBox.TextChanged += maxTextBox_TextChanged;
//
// label7
//
@ -203,7 +203,7 @@
minTextBox.Name = "minTextBox";
minTextBox.Size = new Size(69, 23);
minTextBox.TabIndex = 14;
minTextBox.Validating += minTextBox_Validating;
minTextBox.TextChanged += minTextBox_TextChanged;
//
// saveButton
//

View File

@ -38,16 +38,14 @@ 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();
}
ValidateInputs(true);
}
private void PopulatePartData(Part part)
@ -193,22 +191,35 @@ namespace C968Project.Views
OUTSOURCED
}
private bool ValidateInputs()
private bool ValidateInputs(bool initial = false)
{
ValidateName();
ValidateInventory();
ValidatePrice();
ValidateMax();
ValidateMin();
ValidateMachineOrCompany();
bool valid = false;
string err = string.Empty;
return _isNameValid && _isInventoryValid && _isPriceValid && _isMaxValid && _isMinValid && _isMachineOrCompanyValid;
err += ValidateName();
err += ValidateInventory();
err += ValidatePrice();
err += ValidateMax();
err += ValidateMin();
err += ValidateMachineOrCompany();
valid = _isNameValid && _isInventoryValid && _isPriceValid && _isMaxValid && _isMinValid && _isMachineOrCompanyValid;
if (!valid && !initial)
{
Helpers.ShowErrorMessage(err, "Validation error");
}
return valid;
}
private void ValidateName()
private string ValidateName()
{
string err = string.Empty;
if (nameTextBox.Text == string.Empty)
{
err = "Part must have a name\n";
errorProvider1.SetError(nameTextBox, "Please enter a part name");
_isNameValid = false;
}
@ -219,15 +230,12 @@ namespace C968Project.Views
}
Helpers.SetTextBoxValidationState(nameTextBox, _isNameValid);
return err;
}
private void nameTextBox_Validating(object sender, CancelEventArgs e)
{
ValidateName();
}
private void ValidateInventory()
private string ValidateInventory()
{
string err = string.Empty;
int inventory = 0;
if (inventoryTextBox.Text != string.Empty && int.TryParse(inventoryTextBox.Text, out inventory))
@ -236,11 +244,13 @@ namespace C968Project.Views
{
if (inventory > _validatedMax)
{
err = $"Inventory must be less than Max: {_validatedMax}\n";
errorProvider1.SetError(inventoryTextBox, $"Must be less than Max: {_validatedMax}");
_isInventoryValid = false;
}
else if (inventory < _validatedMin)
{
err = $"Inventory must be more than Min: {_validatedMin}\n";
errorProvider1.SetError(inventoryTextBox, $"Must be greater than Min: {_validatedMin}");
_isInventoryValid = false;
}
@ -252,26 +262,25 @@ namespace C968Project.Views
}
else
{
err = "Inventory can't be set without valid Min and Max values\n";
errorProvider1.SetError(inventoryTextBox, $"Min and Max values must be set");
_isInventoryValid = false;
}
}
else
{
err = "Inventory must be an integer value\n";
_isInventoryValid = false;
errorProvider1.SetError(inventoryTextBox, "Field must contain an integer value");
}
Helpers.SetTextBoxValidationState(inventoryTextBox, _isInventoryValid);
return err;
}
private void inventoryTextBox_Validating(object sender, CancelEventArgs e)
{
ValidateInventory();
}
private void ValidatePrice()
private string ValidatePrice()
{
string err = string.Empty;
float price = 0f;
if (priceCostTextBox.Text != string.Empty && float.TryParse(priceCostTextBox.Text, out price))
@ -283,18 +292,16 @@ namespace C968Project.Views
{
_isPriceValid = false;
errorProvider1.SetError(priceCostTextBox, "Field must contain a floating point number");
err = "Price must be a decimal value\n";
}
Helpers.SetTextBoxValidationState(priceCostTextBox, _isPriceValid);
return err;
}
private void priceCostTextBox_Validating(object sender, CancelEventArgs e)
{
ValidatePrice();
}
private void ValidateMax()
private string ValidateMax()
{
string err = string.Empty;
int max = 0;
if (maxTextBox.Text != string.Empty && int.TryParse(maxTextBox.Text, out max))
{
@ -306,19 +313,23 @@ namespace C968Project.Views
{
_isMaxValid = false;
errorProvider1.SetError(maxTextBox, "Field must contain an integer value");
err = "Max must be an integer value\n";
}
if (_isMinValid && _isMaxValid && _validatedMax < _validatedMin)
{
_isMaxValid = false;
err = $"Max cannot be less than Min: {_validatedMin}\n";
errorProvider1.SetError(maxTextBox, $"Max cannot be less than Min: {_validatedMin}");
}
ValidateInventory();
Helpers.SetTextBoxValidationState(maxTextBox, _isMaxValid);
return err;
}
private void maxTextBox_Validating(object sender, CancelEventArgs e)
{
ValidateMax();
}
private void ValidateMin()
private string ValidateMin()
{
string err = string.Empty;
int min = 0;
if (minTextBox.Text != string.Empty && int.TryParse(minTextBox.Text, out min))
{
@ -330,19 +341,25 @@ namespace C968Project.Views
{
_isMinValid = false;
errorProvider1.SetError(minTextBox, "Field must contain an integer value");
err = "Min must be an integer value\n";
}
if (_isMinValid && _isMaxValid && _validatedMin > _validatedMax)
{
_isMinValid = false;
err = $"Min cannot be more than Max: {_validatedMax}\n";
errorProvider1.SetError(maxTextBox, $"Min cannot be more than Max: {_validatedMax}");
}
ValidateInventory();
Helpers.SetTextBoxValidationState(minTextBox, _isMinValid);
return err;
}
private void minTextBox_Validating(object sender, CancelEventArgs e)
private string ValidateMachineOrCompany()
{
ValidateMin();
}
string err = string.Empty;
private void ValidateMachineOrCompany()
{
if (machineCompanyTextBox.Text == string.Empty)
{
_isMachineOrCompanyValid = false;
@ -350,11 +367,13 @@ namespace C968Project.Views
if (_mode is Mode.INHOUSE)
{
errorProvider1.SetError(machineCompanyTextBox, "Field must contain an integer value");
err = "Machine ID must have an integer value\n";
}
if (_mode is Mode.OUTSOURCED)
{
errorProvider1.SetError(machineCompanyTextBox, "Field must contain a value");
err = "Company Name must have a value\n";
}
}
else
@ -371,6 +390,7 @@ namespace C968Project.Views
{
_isMachineOrCompanyValid = false;
errorProvider1.SetError(machineCompanyTextBox, "Field must contain an integer value");
err = "Machine ID must be an integer value\n";
}
}
@ -385,11 +405,42 @@ namespace C968Project.Views
}
Helpers.SetTextBoxValidationState(machineCompanyTextBox, _isMachineOrCompanyValid);
return err;
}
private void machineCompanyTextBox_Validating(object sender, CancelEventArgs e)
{
ValidateMachineOrCompany();
}
private void nameTextBox_TextChanged(object sender, EventArgs e)
{
ValidateName();
}
private void inventoryTextBox_TextChanged(object sender, EventArgs e)
{
ValidateInventory();
}
private void priceCostTextBox_TextChanged(object sender, EventArgs e)
{
ValidatePrice();
}
private void maxTextBox_TextChanged(object sender, EventArgs e)
{
ValidateMax();
}
private void minTextBox_TextChanged(object sender, EventArgs e)
{
ValidateMin();
}
private void machineCompanyTextBox_TextChanged(object sender, EventArgs e)
{
ValidateMachineOrCompany();
}
}
}

View File

@ -178,6 +178,7 @@
searchButton.TabIndex = 19;
searchButton.Text = "Search";
searchButton.UseVisualStyleBackColor = true;
searchButton.Click += searchButton_Click;
//
// searchTextBox
//
@ -188,9 +189,12 @@
//
// allPartsDataGridView
//
allPartsDataGridView.AllowUserToAddRows = false;
allPartsDataGridView.AllowUserToDeleteRows = false;
allPartsDataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
allPartsDataGridView.Location = new Point(407, 91);
allPartsDataGridView.Name = "allPartsDataGridView";
allPartsDataGridView.ReadOnly = true;
allPartsDataGridView.Size = new Size(482, 150);
allPartsDataGridView.TabIndex = 21;
//
@ -214,9 +218,11 @@
//
// associatedPartsDataGridView
//
associatedPartsDataGridView.AllowUserToAddRows = false;
associatedPartsDataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
associatedPartsDataGridView.Location = new Point(407, 361);
associatedPartsDataGridView.Name = "associatedPartsDataGridView";
associatedPartsDataGridView.ReadOnly = true;
associatedPartsDataGridView.Size = new Size(482, 150);
associatedPartsDataGridView.TabIndex = 25;
//

View File

@ -45,7 +45,7 @@ namespace C968Project.Views
allPartsDataGridView.DataSource = Program.Inventory.Parts;
ValidateInputs();
ValidateInputs(true);
}
private void PopulateProductData(Product product)
@ -117,7 +117,7 @@ namespace C968Project.Views
private void saveButton_Click(object sender, EventArgs e)
{
if (ValidateInputs())
if (ValidateInputs())
{
if (_screenOption is ScreenOption.ADD)
{
@ -185,11 +185,14 @@ namespace C968Project.Views
ValidateName();
}
private void ValidateName()
private string ValidateName()
{
string err = string.Empty;
if (nameTextBox.Text == string.Empty)
{
errorProvider1.SetError(nameTextBox, "Please enter a product name");
err = "Must have a product name\n";
errorProvider1.SetError(nameTextBox, "Must have a product name");
_isNameValid = false;
}
else
@ -199,6 +202,7 @@ namespace C968Project.Views
}
Helpers.SetTextBoxValidationState(nameTextBox, _isNameValid);
return err;
}
private void inventoryTextBox_TextChanged(object sender, EventArgs e)
@ -206,8 +210,9 @@ namespace C968Project.Views
ValidateInventory();
}
private void ValidateInventory()
private string ValidateInventory()
{
string err = string.Empty;
int inventory = 0;
if (inventoryTextBox.Text != string.Empty && int.TryParse(inventoryTextBox.Text, out inventory))
@ -216,11 +221,13 @@ namespace C968Project.Views
{
if (inventory > _validatedMax)
{
err = $"Inventory must be less than Max: {_validatedMax}\n";
errorProvider1.SetError(inventoryTextBox, $"Must be less than Max: {_validatedMax}");
_isInventoryValid = false;
}
else if (inventory < _validatedMin)
{
err = $"Inventory must be greater than Min: {_validatedMin}\n";
errorProvider1.SetError(inventoryTextBox, $"Must be greater than Min: {_validatedMin}");
_isInventoryValid = false;
}
@ -232,17 +239,20 @@ namespace C968Project.Views
}
else
{
err = $"Inventory can't be set without valid Min and Max values\n";
errorProvider1.SetError(inventoryTextBox, $"Min and Max values must be set");
_isInventoryValid = false;
}
}
else
{
err = $"Inventory must contain an integer\n";
_isInventoryValid = false;
errorProvider1.SetError(inventoryTextBox, "Field must contain an integer value");
}
Helpers.SetTextBoxValidationState(inventoryTextBox, _isInventoryValid);
return err;
}
private void priceTextBox_TextChanged(object sender, EventArgs e)
@ -250,8 +260,9 @@ namespace C968Project.Views
ValidatePrice();
}
private void ValidatePrice()
private string ValidatePrice()
{
string err = string.Empty;
float price = 0f;
if (priceTextBox.Text != string.Empty && float.TryParse(priceTextBox.Text, out price))
@ -262,10 +273,12 @@ namespace C968Project.Views
else
{
_isPriceValid = false;
errorProvider1.SetError(priceTextBox, "Field must contain a floating point number");
err = "Price must contain a decimal value\n";
errorProvider1.SetError(priceTextBox, "Field must contain a decimal value");
}
Helpers.SetTextBoxValidationState(priceTextBox, _isPriceValid);
return err;
}
private void maxTextBox_TextChanged(object sender, EventArgs e)
@ -273,8 +286,9 @@ namespace C968Project.Views
ValidateMax();
}
private void ValidateMax()
private string ValidateMax()
{
string err = string.Empty;
int max = 0;
if (maxTextBox.Text != string.Empty && int.TryParse(maxTextBox.Text, out max))
{
@ -286,10 +300,19 @@ namespace C968Project.Views
{
_isMaxValid = false;
errorProvider1.SetError(maxTextBox, "Field must contain an integer value");
err = "Max must be an integer value\n";
}
if (_isMinValid && _isMaxValid && _validatedMax < _validatedMin)
{
_isMaxValid = false;
err = $"Max cannot be less than Min: {_validatedMin}\n";
errorProvider1.SetError(maxTextBox, $"Max cannot be less than Min: {_validatedMin}");
}
ValidateInventory();
Helpers.SetTextBoxValidationState(maxTextBox, _isMaxValid);
return err;
}
private void minTextBox_TextChanged(object sender, EventArgs e)
@ -297,8 +320,9 @@ namespace C968Project.Views
ValidateMin();
}
private void ValidateMin()
private string ValidateMin()
{
string err = string.Empty;
int min = 0;
if (minTextBox.Text != string.Empty && int.TryParse(minTextBox.Text, out min))
{
@ -310,21 +334,45 @@ namespace C968Project.Views
{
_isMinValid = false;
errorProvider1.SetError(minTextBox, "Field must contain an integer value");
err = "Min must be an integer value\n";
}
if (_isMinValid && _isMaxValid && _validatedMin > _validatedMax)
{
_isMinValid = false;
err = $"Min cannot be more than Max: {_validatedMax}\n";
errorProvider1.SetError(maxTextBox, $"Min cannot be more than Max: {_validatedMax}");
}
ValidateInventory();
Helpers.SetTextBoxValidationState(minTextBox, _isMinValid);
return err;
}
private bool ValidateInputs()
private bool ValidateInputs(bool initial = false)
{
ValidateName();
ValidateInventory();
ValidatePrice();
ValidateMin();
ValidateMax();
bool valid = false;
string err = string.Empty;
return _isNameValid && _isInventoryValid && _isPriceValid && _isMinValid && _isMaxValid;
err += ValidateName();
err += ValidateInventory();
err += ValidatePrice();
err += ValidateMin();
err += ValidateMax();
valid = _isNameValid && _isInventoryValid && _isPriceValid && _isMinValid && _isMaxValid;
if (!valid && !initial)
{
Helpers.ShowErrorMessage(err, "Validation error");
}
return valid;
}
private void searchButton_Click(object sender, EventArgs e)
{
Helpers.SearchDataGridView(allPartsDataGridView, searchTextBox.Text);
}
}
}

View File

@ -123,4 +123,7 @@
<metadata name="$this.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>60</value>
</metadata>
</root>

View File

@ -156,10 +156,12 @@ partial class MainScreen
//
// productsDataGridView
//
productsDataGridView.AllowUserToAddRows = false;
productsDataGridView.Anchor = AnchorStyles.Right;
productsDataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
productsDataGridView.Location = new Point(477, 112);
productsDataGridView.Name = "productsDataGridView";
productsDataGridView.ReadOnly = true;
productsDataGridView.Size = new Size(440, 162);
productsDataGridView.TabIndex = 9;
//
@ -237,10 +239,12 @@ partial class MainScreen
//
// partsDataGridView
//
partsDataGridView.AllowUserToAddRows = false;
partsDataGridView.Anchor = AnchorStyles.Left;
partsDataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
partsDataGridView.Location = new Point(12, 112);
partsDataGridView.Name = "partsDataGridView";
partsDataGridView.ReadOnly = true;
partsDataGridView.Size = new Size(440, 162);
partsDataGridView.TabIndex = 0;
//

View File

@ -68,7 +68,7 @@ public partial class MainScreen : Form
private void partsSearchButton_Click(object sender, EventArgs e)
{
// TODO: Search functionality
Helpers.SearchDataGridView(partsDataGridView, partsSearchTextBox.Text);
}
// --- Products
@ -126,7 +126,7 @@ public partial class MainScreen : Form
private void productsSearchButton_Click(object sender, EventArgs e)
{
// TODO: Search functionality
Helpers.SearchDataGridView(productsDataGridView, productsSearchTextBox.Text);
}

Binary file not shown.

Binary file not shown.