【问题标题】:To get total marks into a textbox将总分输入文本框
【发布时间】:2014-02-06 06:45:26
【问题描述】:

我的代码是:

protected void txttotal_TextChanged(object sender, EventArgs e)
{
     int a = Convert.ToInt32(txtmaths.Text);
     int b = Convert.ToInt32(txtsci.Text);
     int c = Convert.ToInt32(txtenglish.Text);
     int tot = a + b + c;
     txttotal.Text = Convert.ToString(tot);
}  

我正在尝试将总分添加到文本框,但它不起作用。您能帮帮我吗?

【问题讨论】:

  • “不工作”通常不是很有帮助
  • 此事件绑定到哪些控件?从名字听起来好像是绑定到txttotal(然后修改了txttotal里面的文字)...应该是绑定到3个主题输入框...
  • 为什么要在处理任何 TextChanged 事件的事件处理程序中设置新文本?

标签: c# .net event-handling


【解决方案1】:

您正在尝试更改文本正在更改的同一文本框中的文本,您确定这是您想要做的吗?

当数学、科学和英语值发生变化时,您不想更改总计吗?如果是这样,请查看以下内容。

protected void txtmaths_TextChanged(object sender, EventArgs e)
{
    UpdateTotals();
}

protected void txtsci_TextChanged(object sender, EventArgs e)
{
    UpdateTotals();
}

protected void txtenglish_TextChanged(object sender, EventArgs e)
{
    UpdateTotals();
}

protected void UpdateTotals()    
{
   int a = Convert.ToInt32(txtmaths.Text);
   int b = Convert.ToInt32(txtsci.Text);
   int c = Convert.ToInt32(txtenglish.Text);
   int tot = a + b + c;
   txttotal.Text = tot.ToString();
} 

【讨论】:

  • 正如@rhughes 指出的那样,如果文本框中的值是用户可输入的,那么您真的应该使用 int.TryParse()。
【解决方案2】:

看起来此方法是txttotalTextChanged 事件的偶数处理程序。我在这里猜测“不工作”:如果您将处理程序中的控件更改为其更改事件,您可能会遇到循环。

您可能想要检查您是否因为处理程序所做的更改而点击了处理程序。

【讨论】:

    【解决方案3】:

    问题:代码没有问题,但看起来您是在总文本框TextChanged 事件处理程序中编写代码,所以谁将引发总文本框TextChanged 事件。

    解决方案:所以你需要有一些像 Sumbit 这样的按钮,你需要将上面的代码移动到提交按钮单击事件处理程序中,如下所示:

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
         int a = Convert.ToInt32(txtmaths.Text);
         int b = Convert.ToInt32(txtsci.Text);
         int c = Convert.ToInt32(txtenglish.Text);
         int tot = a + b + c;
         txttotal.Text = Convert.ToString(tot);
    } 
    

    【讨论】:

      【解决方案4】:

      您拥有在txtTotal_TextChanged 事件中更新txtTotal 的代码。您需要将您拥有的代码放入某个方法中,并在更改 txtmathstxtscitxtenglish 而不是 txttotal_TextChanged 时调用它

      protected void UpdateTotal()
      {
           txttotal.Text = Convert.ToInt32(txtmaths.Text) + Convert.ToInt32(txtsci.Text) + Convert.ToInt32(txtenglish.Text);
      }  
      
      
      protected void txtmaths_TextChanged(object sender, EventArgs e)
      {
            UpdateTotal();
      }
      

      【讨论】:

      • 除了糟糕的代码结构之外,这如何解决任何问题?它可能结构更好,但在语义上是等效的。
      • 我相信他的代码在错误的事件中,并建议在更改 txtmaths、txtsci、txtenglish 时这样做
      • 我首先关注的是问题,而不是代码优化。
      • 我不是在谈论优化。
      【解决方案5】:

      由于类型异常,我可以看到此错误。

      这是一种更安全且潜在错误更少的方法:

      protected void txttotal_TextChanged(object sender, EventArgs e)
      {
          int a = 0;
          int b = 0;
          int c = 0;
      
          if (!Int32.TryParse(txtmaths.Text, ref a))
              // handle error...
      
          if (!Int32.TryParse(txtsci.Text, ref b))
              // handle error...
      
          if (!Int32.TryParse(txtenglish.Text, ref c))
              // handle error...
      
          int tot = a + b + c;
          txttotal.Text = Convert.ToString(tot);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-07
        • 2020-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-14
        • 2011-05-07
        相关资源
        最近更新 更多