在C#.NET中向TextBox输入值时动态添加多个文本框

时间:2014-09-02 07:01:10

标签: c# forms textbox addition

我正在使用.NET表单(在Visual Studio中)为C#设计薪资表的表单。

在文本框中输入各种值时,应显示最终文本框,并添加所有其他文本框。如果我从任何文本框中删除值,则应显示所有剩余文本框的结果。

代码

float grossalery;
float afterleaves;
float n1, n2, n3, n4, n5, n6, n7;private void txtCCA_TextChanged(object sender, EventArgs e)
{
    if (txtCCA.Text.Length != 0)
    {
        n1 = Convert.ToInt32(txtGTC.Text);
        n2 = Convert.ToInt32(txtPF.Text);
        n3 = Convert.ToInt32(txtbasicsalery.Text);
        n4 = Convert.ToInt32(txthoserent.Text);
        n5 = Convert.ToInt32(txtlicrent.Text);
        n6 = Convert.ToInt32(txtDA.Text);
        n7 = Convert.ToInt32(txtCCA.Text);
        grossalery = n1 + n2 + n3 + n4 + n5 + n6 + n7;
        txtgrosssalery.Text = Convert.ToString(grossalery);
    } else {
        txtCCA.Text = "";
    }
}

2 个答案:

答案 0 :(得分:0)

将所有文本框TextChanged事件设置为txtCCA_TextChanged

private void txtCCA_TextChanged(object sender, EventArgs e)
{
    sumNumbers()
}
private void sumNumbers()
{
    float.TryParse(txtGTC.Text, out n1);
    float.TryParse(txtPF.Text, out n2);
    float.TryParse(txtbasicsalery.Text, out n3);
    float.TryParse(txthoserent.Text, out n4);
    float.TryParse(txtlicrent.Text, out n5);
    float.TryParse(txtDA.Text, out n6);
    float.TryParse(txtCCA.Text, out n7);
    grossalery = n1 + n2 + n3 + n4 + n5 + n6 + n7;
    txtgrosssalery.Text = Convert.ToString(grossalery);
}

答案 1 :(得分:0)

private void sumNumbers()
  {
      float.TryParse(txtGTC.Text, out n1);
      float.TryParse(txtPF.Text, out n2);
      float.TryParse(txtbasicsalery.Text, out n3);
      float.TryParse(txthoserent.Text, out n4);
      float.TryParse(txtlicrent.Text, out n5);
      float.TryParse(txtDA.Text, out n6);
      float.TryParse(txtCCA.Text, out n7);
      grossalery = n1 + n2 + n3 + n4 + n5 + n6 + n7;
      txtgrosssalery.Text = Convert.ToString(grossalery);
  }

private void txtGTC_TextChanged(object sender,EventArgs e)       {           sumNumbers();       }

  private void txtPF_TextChanged(object sender, EventArgs e)
  {
      sumNumbers();
  }

  private void txtbasicsalery_TextChanged(object sender, EventArgs e)
  {
      sumNumbers();
  }

  private void txthoserent_TextChanged(object sender, EventArgs e)
  {
      sumNumbers();
  }

  private void txtlicrent_TextChanged(object sender, EventArgs e)
  {
      sumNumbers();
  }

  private void txtDA_TextChanged(object sender, EventArgs e)
  {
      sumNumbers();
  }