Advertisement
RockField64

frmAddProduct

Oct 26th, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace frmAddProduct
  13. {
  14.    
  15.     public partial class frmAddProduct : Form
  16.     {
  17.         private string _ProductName;
  18.         private string _Category;
  19.         private string _MfgDate;
  20.         private string _ExpDate;
  21.         private string _Description;
  22.  
  23.         private int _Quantity;
  24.         private double _SellPrice;
  25.  
  26.         BindingSource showProductList = new BindingSource();
  27.  
  28.  
  29.         public string Product_Name(string name)
  30.         {
  31.  
  32.             if (!Regex.IsMatch(name, @"^[a-zA-Z]+$"))
  33.                 //Exception here
  34.                 return name;
  35.         }
  36.         public int Quantity(string qty)
  37.         {
  38.             if (!Regex.IsMatch(qty, @"^[0-9]"))
  39.                 //Exception here
  40.                 return Convert.ToInt32(qty);
  41.         }
  42.         public double SellingPrice(string price)
  43.         {
  44.             if (!Regex.IsMatch(price.ToString(), @"^(\d*\.)?\d+$"))
  45.                 //Exception here
  46.                 return Convert.ToDouble(price);
  47.         }
  48.  
  49.        
  50.         public frmAddProduct()
  51.         {
  52.             InitializeComponent();
  53.         }
  54.        
  55.  
  56.         private void frmAddProduct_Load(object sender, EventArgs e)
  57.         {
  58.             string[] ListOfProductCategory = {"Beverages" , "Bread/Bakery",
  59.             "Canned/Jarred Goods", "Dairy", "Frozen Goods", "Meat",
  60.             "Personal Care", "Other"};
  61.  
  62.             foreach (string listofproductcategory in ListOfProductCategory)
  63.             {
  64.                 cbCategory.Items.Add(listofproductcategory);
  65.             }
  66.         }
  67.  
  68.         private void btnAddProduct_Click(object sender, EventArgs e)
  69.         {
  70.             _ProductName = Product_Name(txtProductName.Text);
  71.             _Category = cbCategory.Text;
  72.             _MfgDate = dtPickerMfgDate.Value.ToString("yyyy-MM-dd");
  73.             _ExpDate = dtPickerExpDate.Value.ToString("yyyy-MM-dd");
  74.             _Description = richTxtDescription.Text;
  75.             _Quantity = Quantity(txtQuantity.Text);
  76.             _SellPrice = SellingPrice(txtSellPrice.Text);
  77.             showProductList.Add(new ProductClass(_ProductName, _Category,
  78.                 _MfgDate, _ExpDate, _SellPrice, _Quantity, _Description));
  79.             gridViewProductList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  80.             gridViewProductList.DataSource = showProductList;
  81.         }
  82.        class NumberFormatException : Exception
  83.         {
  84.             public NumberFormatException (string qty) : base(qty) { }
  85.         }
  86.         class StringFormatException : Exception
  87.         {
  88.             public StringFormatException (string name) : base (name) { }
  89.         }
  90.         class CurrencyFormatException : Exception
  91.         {
  92.             public CurrencyFormatException (string price) : base (price) { }
  93.         }
  94.            
  95.     }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement