多个文本框值在另一个文本框中求和 [英] multiple Textboxes values sum in another textbox

查看:93
本文介绍了多个文本框值在另一个文本框中求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友



i有多个文本框,我试图在另一个框中计算但是如果任何一个文本框值等于null然后我就无法运行错误消息来了

Dear Friends

i have multiple text boxes and i am trying to calculate in another box but i failed to run if any one text box value equal to null then string error message comes

private void button2_Click(object sender, EventArgs e)
       {
           double a1 = double.Parse(txtRawMm.Text);
           double b1 = double.Parse(txtCsp.Text);
           double c1 = double.Parse(txtPf.Text);
           double d1 = double.Parse(txtSe.Text);
           double e1 = double.Parse(txtIowc.Text);
           //double f1 = double.Parse(txtTotal.Text);
           double i = a1 + b1 + c1 + d1 + e1;
           i = a1 + b1 + c1 + d1 + e1;
           //txtTotal.Text = "Sum is: " + sum;
           //Double f1 = NetIncome / Sales * 100;
           txtTotal.Text = i.ToString("C").Remove(0, 1);

推荐答案

对数据实施一些基本检查。



我会检查以下内容:



1)字符串为null / empty / whitespace(尝试使用String.IsNullOrWhiteSpace())

2)字符串数值是多少?



或者,如果您不想进行多次检查,只需使用 Double.TryParse ,然后查看在你尝试总结它们之前的结果。该方法返回一个布尔值,以指示解析是否有效。它还需要第二个参数,如果它成功,它就是解析的输出。
Implement some basic checks on your data.

I would check for the following:

1) Are strings null/empty/whitespace (Try using String.IsNullOrWhiteSpace())
2) Are the strings numerical values?

Or if you don't want multiple checks like that just use Double.TryParse instead and look at the result before you try and sum them. That method returns a boolean to indicate if the parse worked or not. It also takes a second parameter which is the output of the parse should it be successful.


HI

试试这个希望这将解决你的问题



HI
Try this hope this will solve your problem

<script type="text/javascript">
        function sum() {
            var txtFirstNumberValue = document.getElementById('TextBox1').value;
            var txtSecondNumberValue = document.getElementById('TextBox2').value;
            var txtThridNumberValue = document.getElementById('TextBox3').value;
            var txtFourthNumberValue = document.getElementById('TextBox4').value;

            if (txtFirstNumberValue == "")
                txtFirstNumberValue = 0;

            if (txtSecondNumberValue == "")
                txtSecondNumberValue = 0;

            if (txtThridNumberValue == "")
                txtThridNumberValue = 0;

            if (txtFourthNumberValue == "")
                txtFourthNumberValue = 0;

            var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue) + parseInt(txtThridNumberValue) + parseInt(txtFourthNumberValue);
            if (!isNaN(result)) {
                document.getElementById('TextBox5').value = result;
            }
        }
    </script>







<asp:TextBox ID="TextBox1" runat="server" onkeyup="sum();"></asp:TextBox>
      <asp:TextBox ID="TextBox2" runat="server" onkeyup="sum();"></asp:TextBox>
      <asp:TextBox ID="TextBox3" runat="server" onkeyup="sum();"></asp:TextBox>
      <asp:TextBox ID="TextBox4" runat="server" onkeyup="sum();"></asp:TextBox>
      <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>







检查它是否适合你




check if it works for you


使用 Double.TryParse [ ^ ]方法(或正确捕获 Parse 方法exc eption>

顺便说一下。你应该使用数组(例如一组文本框和另一个双打),你的代码会更清晰。
Use Double.TryParse[^] method (or properly catch the Parse method exception).
By the way. You should use arrays (for instance an array of textboxes and another one of doubles), your code would be cleaner.


这篇关于多个文本框值在另一个文本框中求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆