0

how to update e specific value on a list.. for example when i click a button it adds the product on the list

name: coffe || quantity:1 || Price:2$

and when i click angain the same product the quantity increases by 1 i used this code but it doesnt change the number of the quantity.

private BindingList<recipt> Lista2 = new BindingList<recipt>();
private void addtolist(object sender, EventArgs e)
{

    Button b = (Button)sender;
    Product p = (Product)b.Tag;
    recipt fat = new recipt ()
    {
        Name= p.Name,
        quantity= 1,
        price = p.Cmimi
    };
    bool found = false;
    if (listBox1.Items.Count > 0)
    {
        foreach (var pr in Lista2)
        {
            if (pr.Name== p.Name)
            {
                pr.quantity= pr.quantity+ 1;
                found = true;
            }
        }
        if (!found)
        {
            fat.tot= fat.quantity* fat.price;
            fat.Nr_bill = Convert.ToInt32(txtNrbill.Text);
            Lista2.Add(fat);

        }
    }
    else
    {
        fat.tot= fat.quantity* fat.price;
        fat.Nr_bill = Convert.ToInt32(txtNrbill.Text);
        Lista2.Add(fat);


    }
    fat.tot= fat.quantity* fat.price;
    fat.Nr_bill = Convert.ToInt32(txtNrbill.Text);
    Lista2.Add(fat);
    pe.Faturs.Add(fat);
    pe.SaveChanges();

    Total = Total + (int)fat.price;
    listBox1.SelectedIndex = listBox1.Items.Count - 1;
}
B.Shehu
  • 11
  • 5
  • Possible duplicate of [how to bind a list to a combobox? (Winforms)](http://stackoverflow.com/questions/600869/how-to-bind-a-list-to-a-combobox-winforms) ListBox binds the same way. – tinstaafl Dec 30 '16 at 00:57
  • i dont want to bind..i want to update a value of quantity every time i add the same product.. – B.Shehu Dec 30 '16 at 01:06
  • If the quantity is updated in the object (use a Watch to check), it is probably a lack of the INotifyPropertyChanged interface with your recipt class. BindingList tell the control to update when you add or remove object. But it doesn't do anything when an object is changed. – Marco Guignard Dec 30 '16 at 01:06
  • so i need to change the BindingList to a list? – B.Shehu Dec 30 '16 at 01:49
  • @MarcoGuignard - `BindingList` will notify control about changes inside object if object implements `INotifyPropertyChanged`. – Fabio Dec 30 '16 at 02:24
  • @Fabio - Of course. But the object (recipt class here) have to implement this interface ! B.Shehu - No using the BindingList is correct here, with a list your control (the listbox) would not update when you add (or remove) a objet. The question here is to know if it is an binding update issue (INotifyPropertyChanged not implemented most of time) or an issue with you addtolist() Sub (your code does not step in the right place). – Marco Guignard Dec 30 '16 at 13:02
  • @Fabio - but how to find the total of all receipt in this way ? – B.Shehu Dec 30 '16 at 17:58
  • @Fabio - and then add to database every product in this recipt – B.Shehu Dec 30 '16 at 23:28

1 Answers1

0

For updating values in ListBox automatically you need set BindingList of receipts to the ListBox.DataSource and make Receipt class implement INotifyPropertyChanged

public class Receipt : INotifyPropertyChanged
{
    public string Name { get; }
    public int Quantity { get; private set; }
    public decimal Price { get; }
    public string BillNumber { get; private set; }
    public decimal Total => Price * Quantity;
    public string Info => $"{nameof(Name)}: {Name} || {nameof(Quantity)}: {Quantity} || {nameof(Price)}: {Price:C} || {nameof(Total)}: {Total:C}";

    public Receipt(string name, decimal price, string billNumber)
    {
        Name = name;
        Price = price;
        BillNumber = billNumber;
        Quantity = 1;
    }

    public void AddOne()
    {
        Quantity += 1;
        RaisePropertyChanged(nameof(Info));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Then in the form

public class YourForm : Form
{
    private readonly BindingList<Receipt> _Receipts;

    public YourForm()
    {
        _Receipts = new BindingList<Receipt>();

        listBox1.DisplayMember = "Info";
        listBox1.DataSource = _Receipts;        
    }

    private void AddToList(object sender, EventArgs e)
    {
        var button = (Button) sender;
        var product = (Product) button.Tag;
        var receiptInfo = _Receipts.FirstOrDefault(receipt => receipt.Name.Equals(product.Name));
        if (receiptInfo == null)
        {
            receiptInfo = new Receipt(product.Name, product.Cmimi, txtNrbill.Text);
            _Receipts.Add(receiptInfo);
        }
        else
        {
            receiptInfo.AddOne();
        }
    }
}
Fabio
  • 31,528
  • 4
  • 33
  • 72