Ejemplo n.º 1
0
        private void lstProductCart_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            int soluong = 0;



            if (e.IsSelected)
            {
                soluong = Convert.ToInt32(e.Item.SubItems[2].Text);
                Frm_themSoluong_Mon f = new Frm_themSoluong_Mon(soluong);
                f.ShowDialog();
                this.Show();
                Cursor.Current = Cursors.Arrow;
                if (f.huychonSP == 1)

                {
                    int sothutu = e.ItemIndex;

                    tongtien -= Convert.ToDouble(e.Item.SubItems[5].Text);

                    txttotalPrice.Text = tongtien.ToString();
                    txtTongTien.Text   = txttotalPrice.Text;



                    for (int i = sothutu + 1; i < lstProductCart.Items.Count; i++)
                    {
                        lstProductCart.Items[i].Text = i.ToString();
                    }
                    lstProductCart.Items[e.ItemIndex].Selected = false;
                    lstProductCart.Items.RemoveAt(e.ItemIndex);


                    order_index--;
                }
                else
                {
                    e.Item.SubItems[2].Text = f.soluongSP_Text.ToString();

                    double gia_lucdau = Convert.ToDouble(e.Item.SubItems[5].Text) * soluong;
                    double gia_update = f.soluongSP_Text * Convert.ToDouble(e.Item.SubItems[3].Text);
                    e.Item.SubItems[5].Text = gia_update.ToString();

                    if (gia_lucdau > gia_update)
                    {
                        tongtien += gia_lucdau - gia_update;
                    }
                    else
                    {
                        tongtien += gia_update - gia_lucdau;
                    }

                    txttotalPrice.Text = tongtien.ToString();
                    txtTongTien.Text   = txttotalPrice.Text;
                }
            }
        }
Ejemplo n.º 2
0
        // Nếu nhấp vào sản phẩm hiện có trên danh sách đặt hàng thì sẽ mở form mới để chọn số lượng hoặc hủy chọn sản phẩm
        // Sau khi chọn xong thì Tổng tiền sẽ tự cập nhật.
        // Sau khi hủy sản phẩm đã chọn thì danh sách đặt hàng sẽ tự động cập nhật lại.
        private void lstProductCart_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            int soluong = 0;



            if (e.IsSelected)
            {
                // Nếu nhấp vào sản phẩm hiện có trên danh sách đặt hàng thì sẽ mở form mới để chọn số lượng hoặc hủy chọn sản phẩm
                soluong = Convert.ToInt32(e.Item.SubItems[2].Text);
                // Truyền qua form Frm_themSoluong_Mon số lượng của sản phẩm mình nhấp vào
                Frm_themSoluong_Mon f = new Frm_themSoluong_Mon(soluong);
                f.ShowDialog();
                this.Show();
                Cursor.Current = Cursors.Arrow;
                // Nếu ta chọn hủy bên form kia (Frm_themSoluong_Mon) thì biến huychọnSP sẽ == 1. Ta phải ưu tiên nó trước.
                if (f.huychonSP == 1)
                {
                    // Khi hủy chọn thì tổng tiền giảm, các textbox khác cũng tự cập nhật.

                    int sothutu = e.ItemIndex;

                    tongtien -= Convert.ToDouble(e.Item.SubItems[5].Text);

                    txttotalPrice.Text = tongtien.ToString();
                    txtTongTien.Text   = txttotalPrice.Text;



                    // Vd: Khi hủy chọn ở vị trí thứ 3 thì trong danh sách đặt hàng:
                    // Các số thứ từ vị trí thứ 4 đến vị trí cuối cùng sẽ giảm xuống để lấp ô trống mà sản phẩm ta đã hủy.
                    for (int i = sothutu + 1; i < lstProductCart.Items.Count; i++)
                    {
                        lstProductCart.Items[i].Text = i.ToString();
                    }
                    lstProductCart.Items[e.ItemIndex].Selected = false;
                    lstProductCart.Items.RemoveAt(e.ItemIndex);


                    order_index--;
                }
                else
                {
                    // Nếu không hủy món mà đã chọn số lượng ở form kia (Frm_themSoluong_Mon) thì sẽ nhận giá trị về form
                    // Banghang này và cập nhật tổng giá sản phẩm
                    e.Item.SubItems[2].Text = f.soluongSP_Text.ToString();

                    double gia_lucdau = Convert.ToDouble(e.Item.SubItems[5].Text) * soluong;
                    double gia_update = f.soluongSP_Text * Convert.ToDouble(e.Item.SubItems[3].Text);
                    e.Item.SubItems[5].Text = gia_update.ToString();

                    // Vd : trước khi thay đổi số lượng sản phẩm thì
                    // Bánh kem A có số lượng :5 || Giá : 50000 => Tổng tiền lúc đó là : 250 000
                    // Sau khi thay đổi thì số lượng hiện giờ là : 10 => Tổng tiền hiện giờ là : 500 000
                    // Tổng tiền toàn bộ sẽ = tổng tiền sau khi thay sl của sp - tổng tiền trước đó của sp.


                    if (gia_lucdau > gia_update)
                    {
                        tongtien += gia_lucdau - gia_update;
                    }
                    else
                    {
                        tongtien += gia_update - gia_lucdau;
                    }

                    txttotalPrice.Text = tongtien.ToString();
                    txtTongTien.Text   = txttotalPrice.Text;
                }
            }
        }