Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想更新列表框c#winform上的数量_C#_Winforms - Fatal编程技术网

我想更新列表框c#winform上的数量

我想更新列表框c#winform上的数量,c#,winforms,C#,Winforms,如何更新列表上的e特定值。。 例如,当我单击一个按钮时,它会将产品添加到列表中 名称:咖啡| | |数量:1 | |价格:2$ 当我点击angain相同的产品时,数量会增加1 我使用了这个代码,但它并没有改变数量的数字 private BindingList<recipt> Lista2 = new BindingList<recipt>(); private void addtolist(object sender, EventArgs e) { Button

如何更新列表上的e特定值。。 例如,当我单击一个按钮时,它会将产品添加到列表中

名称:咖啡| | |数量:1 | |价格:2$

当我点击angain相同的产品时,数量会增加1 我使用了这个代码,但它并没有改变数量的数字

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;
}
private BindingList Lista2=new BindingList();
私有void addtolist(对象发送方,事件参数e)
{
按钮b=(按钮)发送器;
产品p=(产品)b.标签;
recipt fat=新recipt()
{
名称=p.名称,
数量=1,
价格=p.Cmimi
};
bool-found=false;
如果(listBox1.Items.Count>0)
{
foreach(列表A2中的var pr)
{
如果(pr.Name==p.Name)
{
请购单数量=请购单数量+1;
发现=真;
}
}
如果(!找到)
{
fat.tot=fat.quantity*fat.price;
fat.Nr_bill=Convert.ToInt32(txtNrbill.Text);
列表A2.添加(脂肪);
}
}
其他的
{
fat.tot=fat.quantity*fat.price;
fat.Nr_bill=Convert.ToInt32(txtNrbill.Text);
列表A2.添加(脂肪);
}
fat.tot=fat.quantity*fat.price;
fat.Nr_bill=Convert.ToInt32(txtNrbill.Text);
列表A2.添加(脂肪);
添加(脂肪);
pe.SaveChanges();
总计=总计+(整数)fat.price;
listBox1.SelectedIndex=listBox1.Items.Count-1;
}

要自动更新列表框中的值,您需要将收据的
BindingList
设置到
ListBox.DataSource
,并使
Receipt
类实现
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));
    }
}
然后在形式上

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();
        }
    }
}
公共类YourForm:Form
{
私有只读绑定列表\u收据;
公共表格
{
_收据=新的绑定列表();
listBox1.DisplayMember=“Info”;
listBox1.DataSource=\u收据;
}
私有void AddToList(对象发送方,事件参数e)
{
var按钮=(按钮)发送器;
var product=(product)button.Tag;
var receiptInfo=_Receipts.FirstOrDefault(Receipts=>Receipts.Name.Equals(product.Name));
if(receiptInfo==null)
{
receiptInfo=新收据(product.Name、product.Cmimi、txtNrbill.Text);
_Receipts.Add(receiptInfo);
}
其他的
{
receiptInfo.AddOne();
}
}
}

ListBox的可能重复项以相同的方式绑定。我不想绑定..我想在每次添加同一产品时更新数量值..如果对象中的数量已更新(使用手表检查),则可能是您的Recit类缺少INotifyPropertyChanged接口。BindingList告诉控件在添加或删除对象时进行更新。但是当对象被更改时,它不会做任何事情。所以我需要将BindingList更改为列表?@MarcoGuignard-
BindingList
将在对象实现
INotifyPropertyChanged
时通知控件对象内的更改。