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

时间:2017-07-07 13:51:29

标签: c# subtraction

我试图计算特定变量的结束值,该变量取决于是否选中了复选框。然而,这是动态完成的。

我想要实现的目标:

enter image description here

上面的示例工作正常但是当我开始取消选中复选框时它会出错。它减去了两倍的时间,我不明白为什么:

enter image description here

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

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);
    }
}

GUI:

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;
}

为什么减法没有正确完成?

3 个答案:

答案 0 :(得分:3)

未选中复选框时减去:

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

因此,通过设置第二张图片(未检查的前2位图片),您可以执行以下操作:

0 - 5 - 5 + 5 = -5

0来自默认值Total (int)

要解决此问题,您只需删除else子句,因为您无需减去永久的计费时间。

答案 1 :(得分:1)

你应该删除减去。 所以改成它:

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

您有total=0,在计算之前尚未添加所有值。所以你不需要减去。

答案 2 :(得分:1)

如果取消选中Check减去,则添加逻辑。如果仅选中,则应添加逻辑。

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;
    }