0% found this document useful (0 votes)
24 views5 pages

Enventory

The document defines a Windows form called frmAddProduct that allows adding product information to an inventory list. It includes fields for product name, category, dates, description, quantity, and price. It validates the input for each field and adds valid entries to a BindingSource to display in a DataGridView. Custom exception classes are defined for number, string, and currency validation errors.

Uploaded by

LYNDON CORTEZ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

Enventory

The document defines a Windows form called frmAddProduct that allows adding product information to an inventory list. It includes fields for product name, category, dates, description, quantity, and price. It validates the input for each field and adds valid entries to a BindingSource to display in a DataGridView. Custom exception classes are defined for number, string, and currency validation errors.

Uploaded by

LYNDON CORTEZ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Inventory
{
public partial class frmAddProduct : Form
{
private string _ProductName;
private string _Category;
private string _MfgDate;
private string _ExpDate;
private string _Description;
private int _Quantity;
private double _SellPrice;

BindingSource showProductList;
public frmAddProduct()
{
InitializeComponent();
showProductList = new BindingSource();
}

private void frmAddProduct_Load(object sender, EventArgs e)


{
string[] ListOfProductCategory = new string[]
{
"Beverages","Bread/Bakery",
"Canned/Jarred Goods","Dairy",
"Frozen Goods","Meat",
"Personal Care","Other"
};
foreach (string ProductArray in ListOfProductCategory)
{
cbCategory.Items.Add(ProductArray);
}
}
public string Product_Name(string name)
{
try
{
if (!Regex.IsMatch(name, @"^[a-zA-Z]+$"))
//Exception here
{
throw new NumberFormatException("There a
StringFormatException");}
{
return name;
}
}
catch (NumberFormatException ex)
{
MessageBox.Show("Error:" + ex.Message);
return null;
}

}
public int Quantity(string qty)
{
try
{
if (!Regex.IsMatch(qty, @"^[0-9]"))
//Exception here
{
throw new StringFormException("There a StringFormatException");
}
{
return Convert.ToInt32(qty);
}
}
catch (StringFormException e)
{
MessageBox.Show("Error: " + e.Message);
return 0;
}
}
public double SellingPrice(string price)
{
try
{
if (!Regex.IsMatch(price.ToString(), @"^(\d*\.)?\d+$"))
//Exception here
{
throw new CurrencyFormatException("There a
StringFormatException");}
{
return Convert.ToDouble(price);
}
}
catch (CurrencyFormatException c)
{
MessageBox.Show("Error: " + c.Message);
return 0;
}

private void btnAddProduct_Click(object sender, EventArgs e)


{
_ProductName = Product_Name(txtProductName.Text);
_Category = cbCategory.Text;
_MfgDate = dtPickerMfgDate.Value.ToString("yyyy-mm-dd");
_ExpDate = dtPickerExpDate.Value.ToString("yyyy-mm-dd");
_Description = richTxtDescription.Text;
_Quantity = Quantity(txtQuatity.Text);
_SellPrice = SellingPrice(txtSellPrice.Text);
showProductList.Add(new ProductClass(_ProductName, _Category, _MfgDate,
_ExpDate, _SellPrice, _Quantity, _Description));
gridViewProductList.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.Fill;
gridViewProductList.DataSource = showProductList;
}

}
class NumberFormatException : Exception
{
public NumberFormatException(string qty) : base(qty) { }
}
class StringFormException : Exception
{
public StringFormException(string name) : base(name) { }

}
class CurrencyFormatException : Exception
{
public CurrencyFormatException(string price) : base(price) { }
}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Inventory
{
public partial class frmAddProduct : Form
{
private string _ProductName;
private string _Category;
private string _MfgDate;
private string _ExpDate;
private string _Description;
private int _Quantity;
private double _SellPrice;

BindingSource showProductList;
public frmAddProduct()
{
InitializeComponent();
showProductList = new BindingSource();
}

private void frmAddProduct_Load(object sender, EventArgs e)


{
string[] ListOfProductCategory = new string[]
{
"Beverages","Bread/Bakery",
"Canned/Jarred Goods","Dairy",
"Frozen Goods","Meat",
"Personal Care","Other"
};
foreach (string ProductArray in ListOfProductCategory)
{
cbCategory.Items.Add(ProductArray);
}
}
public string Product_Name(string name)
{
try
{
if (!Regex.IsMatch(name, @"^[a-zA-Z]+$"))
//Exception here
{
throw new NumberFormatException("There a
StringFormatException");}
{
return name;
}
}
catch (NumberFormatException ex)
{
MessageBox.Show("Error:" + ex.Message);
return null;
}

}
public int Quantity(string qty)
{
try
{
if (!Regex.IsMatch(qty, @"^[0-9]"))
//Exception here
{
throw new StringFormException("There a StringFormatException");
}
{
return Convert.ToInt32(qty);
}
}
catch (StringFormException e)
{
MessageBox.Show("Error: " + e.Message);
return 0;
}
}
public double SellingPrice(string price)
{
try
{
if (!Regex.IsMatch(price.ToString(), @"^(\d*\.)?\d+$"))
//Exception here
{
throw new CurrencyFormatException("There a
StringFormatException");}
{
return Convert.ToDouble(price);
}
}
catch (CurrencyFormatException c)
{
MessageBox.Show("Error: " + c.Message);
return 0;
}

private void btnAddProduct_Click(object sender, EventArgs e)


{
_ProductName = Product_Name(txtProductName.Text);
_Category = cbCategory.Text;
_MfgDate = dtPickerMfgDate.Value.ToString("yyyy-mm-dd");
_ExpDate = dtPickerExpDate.Value.ToString("yyyy-mm-dd");
_Description = richTxtDescription.Text;
_Quantity = Quantity(txtQuatity.Text);
_SellPrice = SellingPrice(txtSellPrice.Text);
showProductList.Add(new ProductClass(_ProductName, _Category, _MfgDate,
_ExpDate, _SellPrice, _Quantity, _Description));
gridViewProductList.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.Fill;
gridViewProductList.DataSource = showProductList;
}

}
class NumberFormatException : Exception
{
public NumberFormatException(string qty) : base(qty) { }
}
class StringFormException : Exception
{
public StringFormException(string name) : base(name) { }

}
class CurrencyFormatException : Exception
{
public CurrencyFormatException(string price) : base(price) { }
}
}

You might also like