首页 > 解决方案 > 文本框不会更新总数

问题描述

我正在尝试在文本框中显示总计,但是只有在分配给它的值大于最后一个值而不是添加到最后一个值时,文本框输出的变量才会更新。

private void PictureBoxWindFarmMouseClick(object sender, MouseEventArgs e)
{
  decimal captot = 0;
  // Read mouse-click position
  int x = e.X;
  int y = e.Y;
  //collects varibles from the form 
  int hei = _heightTrackBar.Value;
  float rad = (_radiusTrackBar.Value)*0.01f ;
  int bla = Convert.ToInt32(_numberOfBladesUpDown.Value);
  Color pol = colorDialog1.Color;
  Color rot = colorDialog2.Color;
  bool clo = _clockwiseCheckBox.Checked;
  decimal cap = _capacityUpDown.Value;
  captot +=  cap;
  // Create wind turbine at this position, uses collected varibles
  WindTurbine turbine = new WindTurbine( hei, rad, bla, clo, pol, rot, cap, x, y);
  // Add wind turbine to farm list
  _turbines.Add(turbine);
  // Force redraw of the picture box to show changes
  _pictureBoxWindFarm.Refresh();
  _statusLabel.ResetText();
  _statusLabel.Text = ("these"+ _turbines.Count + "wind turbines can generate a total of" + captot + "MW of power.");
}

左下角的文本框

文本框不添加

如果值更大则更新

标签: c#winforms

解决方案


GI1 是对的,只是需要在事件之外声明的变量


推荐阅读