【问题标题】:How to subtract a value from 2 text field and store the value in second text field?如何从 2 个文本字段中减去一个值并将该值存储在第二个文本字段中?
【发布时间】:2019-06-25 16:52:18
【问题描述】:

我有一个项目,其中我有一个字段,如价格、数量、总金额、支付金额、待处理金额和支付的信用金额。现在总金额的计算是通过使用乘法运算符来完成的。现在,当用户输入已支付金额时,会发生以下计算:总金额 - 已支付金额 = 待处理金额。

假设总金额为 40,支付金额为 20,因此待处理金额为 20。几天后,剩余的信用金额将被记入贷方,因此它将为 20,所以我现在尝试的计算是待处理金额 - 信用金额 = 待处理金额.所以最终的金额应该是0。下面是前端截图:

我尝试了以下代码,但未决金额字段未达到 0 或剩余金额。以下是我的代码:

    private void Price_TextChanged(object sender, TextChangedEventArgs e)
    {
        Multiply();
    }
    private void Quantity_TextChanged(object sender, TextChangedEventArgs e)
    {
        Multiply();
    }
    public void Multiply()
    {
        int a, b;
        bool isAValid = int.TryParse(txtprice.Text, out a);
        bool isBValid = int.TryParse(txtquantity.Text, out b);
        if (isAValid && isBValid)
        {
            txttotalamount.Text = (a * b).ToString();
        }
        else
        {
            txttotalamount.Text = "Invalid Input";
        }
    }

    private void TotalAmount_TextChanged(object sender, TextChangedEventArgs e)
    {
        Subtract();
    }

    private void PaidAmount_TextChanged(object sender, TextChangedEventArgs e)
    {
        Subtract();
    }
    private void PendingAmount_TextChanged(object sender, TextChangedEventArgs e)
    {
        Subtract();
    }

    private void txtcreditamountpaid_TextChanged(object sender, TextChangedEventArgs e)
    {
        Subtract();
    }
    public void Subtract()
    {
        int a, b,c,d;
        bool isAValid = int.TryParse(txttotalamount.Text, out a);
        bool isBValid = int.TryParse(txtpaidamount.Text, out b);
        bool isCValid = int.TryParse(txtpendingamount.Text,out c);
        bool isDValid = int.TryParse(txtcreditamountpaid.Text, out d);
        if (isAValid && isBValid)
        {
            string e = (a - b).ToString();
            txtpendingamount.Text = (e).ToString();
        }
        else if (isCValid && isDValid)
        {
            string f=(c-d).ToString();
            txtpendingamount.Text = (f).ToString();
        }  
        else
        {
            txtpendingamount.Text = "invalid Input";
        }
    }

}

我希望未决金额字段中的输出为 0 或在贷方金额字段中输入的剩余金额。我自己尝试过,但它不起作用!

【问题讨论】:

  • "...notworking..." - 这些信息并没有真正的帮助。它没有说明究竟是什么不起作用,即您是否在任何地方收到任何错误消息,您是否尝试调试问题以确保它确实完成了预期的事情。
  • 对我来说看起来像是某人的作业:\您尝试调试吗?
  • 这是问题我没有收到异常或错误我尝试调试我的程序流程是正确的但我的问题是我需要做什么以便我可以在待定金额字段中获得零或剩余金额已输入信用额度@JohnB
  • 约翰在问its not working! 是什么意思。当您在txtcreditamountpaid 值中输入值时会发生什么? txtpendingamount 中输出了什么?
  • 我在上面发布了一个屏幕截图.... 输入 creditamount 时未进行任何计算,待处理金额中的金额保持不变@vasily.sib

标签: c# .net sql-server wpf operators


【解决方案1】:
private void txtcreditamountpaid_TextChanged(object sender, TextChangedEventArgs e)
    {
        Subtract(true);
    }
    public void Subtract(bool isCreditAmount = false)
    {
        int a, b,c,d;
        bool isAValid = int.TryParse(txttotalamount.Text, out a);
        bool isBValid = int.TryParse(txtpaidamount.Text, out b);
        bool isCValid = int.TryParse(txtpendingamount.Text,out c);
        bool isDValid = int.TryParse(txtcreditamountpaid.Text, out d);
        if (isAValid && isBValid && !isCreditAmount)
        {
            string e = (a - b).ToString();
            txtpendingamount.Text = (e).ToString();
        }
        else if (isCValid && isDValid)
        {
            string f=(c-d).ToString();
            txtpendingamount.Text = (f).ToString();
        }  
        else
        {
            txtpendingamount.Text = "invalid Input";
        }
    }

像这样更改您的代码。它会起作用的。

【讨论】:

  • 对不起,我试过了,但还是不行:(!@ganeshkumar
  • 没有@RaoRajnish。一定是工作。检查你的代码。您只需修改这些行。减(真); public void Subtract(bool isCreditAmount = false) 和 if (isAValid && isBValid && !isCreditAmount) 一旦你输入 credit amout 并离开该字段,它就会起作用。请再次检查。
  • 抱歉,我刚刚进行了更改,并且我只是复制并粘贴了您的代码并替换了我的代码@ganeshkumar
【解决方案2】:

试试下面的减法代码:

public void Subtract()
    {
        int a, b,c,d;
        bool isAValid = int.TryParse(txttotalamount.Text, out a);
        bool isBValid = int.TryParse(txtpaidamount.Text, out b);

        if (isAValid && isBValid)
        {
            string e = (a - b).ToString();
            txtpendingamount.Text = (e).ToString();
        }

 bool isCValid = int.TryParse(txtpendingamount.Text,out c);
        bool isDValid = int.TryParse(txtcreditamountpaid.Text, out d);

        if (isCValid && isDValid)
        {
            string f=(c-d).ToString();
            txtpendingamount.Text = (f).ToString();
        }  
        else
        {
            txtpendingamount.Text = "invalid Input";
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多