コード例 #1
0
        private void DeleteRow(object sender, EventArgs e)
        {
            PictureBox pb       = (PictureBox)sender;
            int        rowIndex = Convert.ToInt32(pb.Tag);

            basketTableLayoutPanel.SuspendLayout();

            // delete all controls of row that we want to delete
            for (int i = 0; i < basketTableLayoutPanel.ColumnCount; i++)
            {
                var control = basketTableLayoutPanel.GetControlFromPosition(i, rowIndex);
                basketTableLayoutPanel.Controls.Remove(control);
            }

            // move up row controls that comes after row we want to remove
            for (int i = rowIndex + 1; i < basketTableLayoutPanel.RowCount; i++)
            {
                for (int j = 0; j < basketTableLayoutPanel.ColumnCount; j++)
                {
                    var control = basketTableLayoutPanel.GetControlFromPosition(j, i);
                    if (control != null)
                    {
                        basketTableLayoutPanel.SetRow(control, i - 1);

                        if (j > 1)
                        {
                            control.Tag = i - 1;
                        }
                    }
                }
            }

            var removeStyle = basketTableLayoutPanel.RowCount - 1;

            if (basketTableLayoutPanel.RowStyles.Count > removeStyle)
            {
                basketTableLayoutPanel.RowStyles.RemoveAt(removeStyle);
            }

            basketTableLayoutPanel.RowCount--;
            totalPrice           = totalPrice - (float.Parse(Basket_list[rowIndex - 1][3]) * int.Parse(Basket_list[rowIndex - 1][5]));
            totalPricelabel.Text = "Total Price: " + totalPrice + " €";
            Basket_list.RemoveAt(rowIndex - 1);


            if (basketTableLayoutPanel.RowCount == 1)
            {
                basketTableLayoutPanel.Visible = false;
                noItemsLabel.Visible           = true;
                totalPricelabel.Text           = "Total Price: 0 €";
            }

            basketTableLayoutPanel.ResumeLayout();
        }
コード例 #2
0
        private void checkQuantity(object sender, EventArgs e)
        {
            NumericUpDown quant = (NumericUpDown)sender;
            int           row   = Convert.ToInt32(quant.Tag);

            if (quant.Value > int.Parse(Basket_list[row - 1][4]))
            {
                basketTableLayoutPanel.GetControlFromPosition(2, row).BackColor = Color.Red;
            }
            else
            {
                basketTableLayoutPanel.GetControlFromPosition(2, row).BackColor = Color.White;
            }

            if (Convert.ToInt32(quant.Value) == 0)
            {
                basketTableLayoutPanel.SuspendLayout();

                // delete all controls of row that we want to delete
                for (int i = 0; i < basketTableLayoutPanel.ColumnCount; i++)
                {
                    var control = basketTableLayoutPanel.GetControlFromPosition(i, row);
                    basketTableLayoutPanel.Controls.Remove(control);
                }

                // move up row controls that comes after row we want to remove
                for (int i = row + 1; i < basketTableLayoutPanel.RowCount; i++)
                {
                    for (int j = 0; j < basketTableLayoutPanel.ColumnCount; j++)
                    {
                        var control = basketTableLayoutPanel.GetControlFromPosition(j, i);
                        if (control != null)
                        {
                            basketTableLayoutPanel.SetRow(control, i - 1);

                            if (j > 1)
                            {
                                control.Tag = i - 1;
                            }
                        }
                    }
                }

                var removeStyle = basketTableLayoutPanel.RowCount - 1;

                if (basketTableLayoutPanel.RowStyles.Count > removeStyle)
                {
                    basketTableLayoutPanel.RowStyles.RemoveAt(removeStyle);
                }

                basketTableLayoutPanel.RowCount--;
                totalPrice           = totalPrice - (float.Parse(Basket_list[row - 1][3]) * int.Parse(Basket_list[row - 1][5]));
                totalPricelabel.Text = "Total Price: " + totalPrice + " €";
                Basket_list.RemoveAt(row - 1);


                if (basketTableLayoutPanel.RowCount == 1)
                {
                    basketTableLayoutPanel.Visible = false;
                    noItemsLabel.Visible           = true;
                    totalPricelabel.Text           = "Total Price: 0 €";
                }

                basketTableLayoutPanel.ResumeLayout();
            }
            else if (quant.Value > int.Parse(Basket_list[row - 1][5]))
            {
                totalPrice += float.Parse(Basket_list[row - 1][3]);
                Basket_list[row - 1][5] = quant.Value.ToString();
                totalPricelabel.Text    = "Total Price: " + totalPrice + " €";
            }
            else if (quant.Value < int.Parse(Basket_list[row - 1][5]))
            {
                totalPrice -= float.Parse(Basket_list[row - 1][3]);
                Basket_list[row - 1][5] = quant.Value.ToString();
                totalPricelabel.Text    = "Total Price: " + totalPrice + " €";
            }
        }