Main Screen fixes

Added confimation dialogues for deleting parts/products;
Finished implementing Product class methods;
Propogating part modifications to associated products;
Stopped deleting of items if there is an association;
This commit is contained in:
2025-05-15 13:31:08 -05:00
parent 4de28ea29c
commit ef65598a4d
7 changed files with 157 additions and 35 deletions

View File

@@ -82,13 +82,15 @@ namespace C968Project.Views
}
else
{
MessageBox.Show("Error modifying part: Could not find part index", "Error", MessageBoxButtons.OK);
MessageBox.Show("Error modifying part: Could not find part index", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var part = Program.Inventory.Parts[partIndex];
var partId = part.PartID;
Program.Inventory.Parts[partIndex] = CreatePart(partId);
var oldPart = Program.Inventory.Parts[partIndex];
var oldPartId = oldPart.PartID;
var modifiedPart = CreatePart(oldPartId);
Program.Inventory.Parts[partIndex] = modifiedPart;
Program.Inventory.PropogatePartModificationToAssociatedProducts(oldPart, modifiedPart);
}
private void AddPart()
@@ -205,18 +207,6 @@ namespace C968Project.Views
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)
@@ -230,7 +220,7 @@ namespace C968Project.Views
_isNameValid = true;
}
SetTextBoxValidationState(nameTextBox, _isNameValid);
Helpers.SetTextBoxValidationState(nameTextBox, _isNameValid);
}
private void nameTextBox_Validating(object sender, CancelEventArgs e)
@@ -274,7 +264,7 @@ namespace C968Project.Views
errorProvider1.SetError(inventoryTextBox, "Field must contain an integer value");
}
SetTextBoxValidationState(inventoryTextBox, _isInventoryValid);
Helpers.SetTextBoxValidationState(inventoryTextBox, _isInventoryValid);
}
private void inventoryTextBox_Validating(object sender, CancelEventArgs e)
@@ -297,7 +287,7 @@ namespace C968Project.Views
errorProvider1.SetError(priceCostTextBox, "Field must contain a floating point number");
}
SetTextBoxValidationState(priceCostTextBox, _isPriceValid);
Helpers.SetTextBoxValidationState(priceCostTextBox, _isPriceValid);
}
private void priceCostTextBox_Validating(object sender, CancelEventArgs e)
@@ -321,7 +311,7 @@ namespace C968Project.Views
}
ValidateInventory();
SetTextBoxValidationState(maxTextBox, _isMaxValid);
Helpers.SetTextBoxValidationState(maxTextBox, _isMaxValid);
}
private void maxTextBox_Validating(object sender, CancelEventArgs e)
@@ -345,7 +335,7 @@ namespace C968Project.Views
}
ValidateInventory();
SetTextBoxValidationState(minTextBox, _isMinValid);
Helpers.SetTextBoxValidationState(minTextBox, _isMinValid);
}
private void minTextBox_Validating(object sender, CancelEventArgs e)
@@ -396,7 +386,7 @@ namespace C968Project.Views
}
}
SetTextBoxValidationState(machineCompanyTextBox, _isMachineOrCompanyValid);
Helpers.SetTextBoxValidationState(machineCompanyTextBox, _isMachineOrCompanyValid);
}
private void machineCompanyTextBox_Validating(object sender, CancelEventArgs e)

View File

@@ -12,9 +12,14 @@ namespace C968Project.Views
{
public partial class AddModifyProductScreen : Form
{
public AddModifyProductScreen()
private ScreenOption screenOption;
private Product? _product;
public AddModifyProductScreen(ScreenOption screenOption, Product? product)
{
InitializeComponent();
this.screenOption = screenOption;
_product = product;
}
}
}

View File

@@ -24,7 +24,11 @@ public partial class MainScreen : Form
private void partsModifyButton_Click(object sender, EventArgs e)
{
if (partsDataGridView.CurrentCell is null) return;
if (partsDataGridView.CurrentCell is null)
{
MessageBox.Show("No part selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
Part part = Program.Inventory.LookupPart(partsDataGridView.CurrentCell.RowIndex);
if (part is null) return;
AddModifyPartScreen partScreen = new AddModifyPartScreen(ScreenOption.MODIFY, part);
@@ -33,8 +37,29 @@ public partial class MainScreen : Form
private void partsDeleteButton_Click(object sender, EventArgs e)
{
if (partsDataGridView.CurrentCell is null)
{
MessageBox.Show("No part selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
Part part = Program.Inventory.LookupPart(partsDataGridView.CurrentCell.RowIndex);
Program.Inventory.DeletePart(part);
if (part is null) return;
if (CheckPartForProductAssociation(part)) return;
if (Helpers.ConfirmDelete()) Program.Inventory.DeletePart(part);
}
private bool CheckPartForProductAssociation(Part part)
{
foreach (var product in Program.Inventory.Products)
{
if (product.AssociatedParts.Contains(part))
{
MessageBox.Show($"Cannot delete part as it is associated with product: {product.Name}", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return true;
}
}
return false;
}
private void partsSearchButton_Click(object sender, EventArgs e)
@@ -45,23 +70,54 @@ public partial class MainScreen : Form
// --- Products
private void productsAddButton_Click(object sender, EventArgs e)
{
AddModifyProductScreen productScreen = new AddModifyProductScreen();
productScreen.Show();
//AddModifyProductScreen productScreen = new AddModifyProductScreen();
//productScreen.Show();
}
private void productsModifyButton_Click(object sender, EventArgs e)
{
AddModifyProductScreen productScreen = new AddModifyProductScreen();
productScreen.Show();
if (productsDataGridView.CurrentCell is null)
{
MessageBox.Show("No product selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
//AddModifyProductScreen productScreen = new AddModifyProductScreen();
//productScreen.Show();
}
private void productsDeleteButton_Click(object sender, EventArgs e)
{
if (productsDataGridView.CurrentCell is null)
{
MessageBox.Show("No product selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
Product product = Program.Inventory.LookupProduct(productsDataGridView.CurrentCell.RowIndex);
if (product is null) return;
if (CheckProductForPartAssociations(product)) return;
if (Helpers.ConfirmDelete()) Program.Inventory.RemoveProduct(Program.Inventory.Products.IndexOf(product));
}
private bool CheckProductForPartAssociations(Product product)
{
if (product.AssociatedParts.Count == 0) return false;
string associatedParts = "";
foreach (var part in product.AssociatedParts)
{
associatedParts += part.Name + "\n";
}
MessageBox.Show($"Cannot delete product as it has association with parts:\n{associatedParts}", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return true;
}
private void productsSearchButton_Click(object sender, EventArgs e)
{
// TODO: Search functionality
}
}