简体   繁体   English

c#-根据每个列表项的布尔值添加或减去一个值

[英]c# - Adding or subtracting a value according to the boolean for each List Item


Well, I will explain what I'm trying to do. 好吧,我会解释我要做什么。
I have a List for my purchasers and also a listbox. 我有一个供我的采购员使用的清单和一个列表框。 When I add a purchaser to the listbox, he is also added to the List called Purchasers. 当我将购买者添加到列表框中时,他也被添加到名为购买者的列表中。 In this List, I have a value for the name of the purchaser and a bool to say if he paid or not. 在此清单中,我有一个购买者名称的值和一个用来说明购买者是否付款的布尔值。 See my List and the class for it: 请参阅我的清单和班级:

public class Purchaser
{
     public string Name { get; set; }
     public bool Paid { get; set; }
}

List<Purchaser> Purchasers = new List<Purchaser>();

Now I will explain what I'm planning to do: all the purchasers must pay $5. 现在,我将解释我打算做什么:所有购买者都必须支付5美元。 I have a checkbox called box_Paid and when I check or uncheck it, the variable paid is turned in true or false for the actual selected purchaser in my listbox. 我有一个名为box_Paid的复选框,当我选中或取消选中该复选框时,列表框中实际选择的购买者的pay变量将变为true或false。
I have a limit in the listbox for the purchasers: 14 in total, and a value in one label called label_AmountLeft that is 70$. 我在购买者的列表框中有一个限制:总共14个,在一个名为label_AmountLeft的标签中的值是70 $。
70/14 = 5$. 70/14 = 5 $。 Each purchaser needs to pay 5$. 每个购买者需要支付5美元。 What I need now is, when I turn the variable for determinate purchaser using the box_Paid.CheckedChanged event, the label with the value 70 decreases 5 units. 我现在需要的是,当我使用box_Paid.CheckedChanged事件打开用于确定购买者的变量时,值为70的标签减少了5个单位。 Example, I've checked my checkbox when purchaser Tom is selected in the listbox. 例如,当在列表框中选择购买者Tom时,我已经选中了复选框。 The Tom's variable paid value now will be true and the value of the label_AmountLeft will now be 65. I check now for Dylan, the value goes to 60. If I uncheck the textbox for Tom, the values goes to 65, because Dylan's variable paid is true . 现在,Tom的可变已付值将为truelabel_AmountLeft的值现在将为65。我现在检查Dylan,该值将变为60。如果我取消选中Tom的文本框,则该值将变为65,因为Dylan的已付变量是真的 If I uncheck the checkbox for both purchasers, the variables will be false and the label_AmountLeft value will return to 70. 如果我取消选中两个购买者的复选框,则变量将为falselabel_AmountLeft值将返回70。
I was trying using: 我正在尝试使用:

double a, b = 5, rest;
private void box_Paid_CheckedChanged(object sender, EventArgs e)
{
    if (box_Paid.Checked == true)
    {
        Purchaser p = Purchasers[listDOF.SelectedIndex];
        p.Paid = true;
        a = System.Convert.ToDouble(label_AmountLeft.Text);
        rest = a - b;
        label_AmountLeft.Text = System.Convert.ToString(rest);
    }
    else
    {
        Purchaser p = Purchasers[listDOF.SelectedIndex];
        p.Paid = false;
        a = System.Convert.ToDouble(label_AmountLeft.Text);
        rest = a + b;
        label_AmountLeft.Text = System.Convert.ToString(rest);
    }
}

But this do not worked. 但是,这没有用。 I think this issue is hard to understand, but I hope someone is able to help me. 我认为这个问题很难理解,但是我希望有人能够帮助我。 Thanks in advance! 提前致谢!

If you were updating your list properly, then the sum could simply be calculated using: 如果您要正确更新列表,则可以使用以下公式简单地计算出总和:

var sum = Purchasers.Where(p => p.Paid).Count() * 5;

Or, you could have a separate field for the sum to avoid parsing the text box value each time: 或者,您可以为总和设置一个单独的字段,以避免每次都解析文本框值:

// oh, and btw, don't use double when working with money
private decimal _sum;
private void box_Paid_CheckedChanged(object sender, EventArgs e)
{
    if (box_Paid.Checked == true)
    {
        _sum += 5;
    }
    else
    {
        _sum -= 5;
    }

    label_AmountLeft.Text = _sum.ToString();
}

But the weird thing is that you're not updating items from your original list at all, but instead creating new Purchaser instances each time (which are collected as soon as you return from the event handler). 但是很奇怪的是,您根本没有更新原始列表中的项目,而是每次都创建新的Purchaser实例(从事件处理程序返回后便立即收集这些实例)。

So, I am guessing the code should look something like: 所以,我猜代码应该看起来像:

private void box_Paid_CheckedChanged(object sender, EventArgs e)
{ 
    // get the currently selected item
    var selectedPurchaser = Purchasers[listDOF.SelectedIndex];

    // set its state
    selectedPurchaser.Paid = box_Paid.Checked;

    // calculate the sum and display it
    UpdateSum();
}

private void UpdateSum()
{
    // calculate the paid sum
    var paidSum = Purchasers.Where(x => x.Paid).Count() * 5;

    // get the amount left
    var amountLeft = 70 - paidSum;

    // update the text box (using whatever formatting you prefer)
    label_AmountLeft.Text = string.Format("${0:0.00}", amountLeft);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM