Пример #1
0
        //Event Handler for QuantityTextBox change
        //If user directly enters value of quantity
        private void QuantityTextBox_Leave(object sender, EventArgs e)
        {
            int Value = 0;

            try
            {
                int CurrentValue = int.Parse(QuantityTextBox.Text);
                //checking if input is less than zero
                if (CurrentValue <= 0)
                {
                    MessageBox.Show("Zero or Less quantity not Allowed. You can use delete button to remove the product from Cart");
                    QuantityTextBox.Undo();
                    QuantityTextBox.SelectAll();
                    QuantityTextBox.Focus();
                }
                //if input is greater than 0
                else
                {
                    for (int i = 0; i < MainForm.ProductList.Count; i++)
                    {
                        //finding the product
                        if (MainForm.ProductList[i].ProductCategory == this.ProdCat &&
                            MainForm.ProductList[i].ProductID == this.ProdID)
                        {
                            //checking the available stock quantity
                            if (CurrentValue <= MainForm.ProductList[i].ProductQuantity)
                            {
                                //Rechecking the previous quantity
                                QuantityTextBox.Undo();

                                if (QuantityTextBox.Text != CurrentValue.ToString())
                                {
                                    Value = CurrentValue;
                                    try
                                    {
                                        CurrentValue         = CurrentValue - int.Parse(QuantityTextBox.Text);
                                        QuantityTextBox.Text = Value.ToString();
                                    }
                                    catch
                                    {
                                        QuantityTextBox.Text = Value.ToString();
                                    }
                                }

                                //Calculating Items and Totals using provided quantity
                                MainForm.TotalItems += CurrentValue;
                                MainForm.GrandTotal += (CurrentValue * this._ProductPrice);
                                MainForm frm = (MainForm)this.FindForm();
                                frm.TotalItemsLabel.Text    = MainForm.TotalItems.ToString();
                                frm.GrandTotalLabel.Text    = MainForm.GrandTotal.ToString();
                                ProductTotalPriceLabel.Text = (Value * this._ProductPrice).ToString();
                                break;
                            }
                            else
                            {
                                MessageBox.Show("Requested Quantities of " + this.ProdName + " are not available in inventory",
                                                "Not Enough Items Available", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                QuantityTextBox.Undo();
                                QuantityTextBox.Focus();
                            }
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("Only Numbers are allowed in quantity. Please enter valid quantity",
                                "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error);
                QuantityTextBox.Focus();
                QuantityTextBox.SelectAll();
                QuantityTextBox.Undo();
            }
        }