title: Take the sum of a list as variable and edit it at runtime
body: <p>I'm trying to calculate the end value of a specific variable that depends whether a checkbox is checked. This is however dynamically done. </p>
  
  <p>What I'm trying to achieve:</p>
  
  <p><a href="https://i.stack.imgur.com/j6Tf5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j6Tf5.png" alt="enter image description here"></a></p>
  
  <p>The example above works fine but it starts going wrong when I start to uncheck the check boxes. It subtracts by the double amount of the time and I have no idea why:</p>
  
  <p><a href="https://i.stack.imgur.com/fNJA1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fNJA1.png" alt="enter image description here"></a></p>
  
  <p>My Code (<code>TimeSpent</code> is the variable that I want to have at the end):</p>
  
  <p>Class</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>GUI:</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>Why is subtracting not done correctly? </p>