将列表的总和作为变量并在运行时对其进行编辑

Take the sum of a list as variable and edit it at runtime

我正在尝试计算特定变量的最终值,该变量取决于复选框是否被选中。然而,这是动态完成的。

我想要达到的目标:

上面的例子工作正常,但当我开始取消选中复选框时它开始出错。它减去了双倍的时间,我不知道为什么:

我的代码(TimeSpent是我最后想要的变量):

Class

public class Activity
{
    public int ID { get; set; }
    public int IncidentID { get; set; }
    public string Description { get; set; }
    public int TimeSpent { get; set; }
    public static int StartX { get; set; } = 10;
    public static int StartY { get; set; } = 10;

    private TextBox textBox = null;

    private CheckBox checkBox = null;

    public Activity(int iD, int incidentID, string description, int timeSpent)
    {
        this.ID = iD;
        this.IncidentID = incidentID;
        this.Description = description;
        this.TimeSpent = timeSpent;
    }

    public Activity()
    { }

    public bool isChecked() {
        return checkBox.Checked;
    }

    public void setTimeSpent(int timeSpent) {
        this.TimeSpent = timeSpent;
        textBox.Text = timeSpent.ToString();
    }

    public int getTimeSpent()
    {
        return Int32.Parse(textBox.Text);
    }

    public void DrawToForm(Panel p)
    {    
        var label = new Label();
        textBox = new TextBox();
        checkBox = new CheckBox();
        checkBox.Checked = true;
        textBox.Size = new System.Drawing.Size(40, 20);
        label.Text = Description.ToString();
        label.AutoSize = true;
        textBox.Text = TimeSpent.ToString();
        label.Left = StartX;
        label.Top = StartY;
        StartX += 430;// Move position to right
        textBox.Left = StartX;
        textBox.Top = StartY;
        StartX += 160;// Move position to right
        checkBox.Left = StartX;
        checkBox.Top = StartY;
        StartX = 10;// Reset to start
        StartY += 50;// Move position to down
        p.Controls.Add(label);
        p.Controls.Add(textBox);
        p.Controls.Add(checkBox);
    }
}

图形用户界面:

private void Btn_Validate_Click(object sender, EventArgs e)
{      
    tb_TotalTime.Text = calculateTotalTime().ToString();
}

public int calculateTotalTime()
{
    int total = 0;
    foreach (Activity a in activities)
    {                  
        if(a.isChecked())
        {
            total += a.getTimeSpent();
        }else
        {
            total -= a.getTimeSpent();
        }
    }
    return total;
}

为什么减法不正确?

未选中复选框时减去:

if(a.isChecked())
{
    total += a.getTimeSpent();
}
else
{
    total -= a.getTimeSpent();// << THIS
}

因此,在设置第二张图片(前两张未选中,底部一张已选中)时,您可以这样做:

0 - 5 - 5 + 5 = -5

0来自默认值Total (int)


要解决此问题,您只需删除 else 子句,因为您不需要减去计费时间 ever.

你应该完全去掉减法。 所以将其更改为:

if(a.isChecked())
{
     total += a.getTimeSpent();
}

您有total=0,您没有在计算前添加所有值。所以你不需要减法。

你的逻辑是如果选中就加,如果不选中就减去。如果仅选中,则应添加您的逻辑。

private void Btn_Validate_Click(object sender, EventArgs e)
    {

        tb_TotalTime.Text = calculateTotalTime().ToString();
    }

    public int calculateTotalTime()
    {
        int total = 0;
        foreach (Activity a in activities)
        {

            if (a.isChecked())
            {
                total += a.getTimeSpent();
            }
        }
        return total;
    }