如何从2个文本字段中减去一个值并将该值存储在第二个文本字段中?

时间:2019-02-01 06:21:26

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

我有一个项目,其中有一个字段,例如价格,数量,总金额,已付金额,未付金额和已付贷方金额。现在,使用乘法运算符完成了总金额的计算。现在,当用户输入支付金额时,将进行以下计算:总金额-支付金额=待处理金额。

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

enter image description here

我从我的角度尝试了以下代码,但是待处理金额字段未达到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或在“贷方金额”字段中输入的剩余金额。我从我自己尝试过,但是没有用!

2 个答案:

答案 0 :(得分:0)

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";
        }
    }

这样更改代码。它将起作用。

答案 1 :(得分:0)

尝试使用以下代码减去方法:

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";
        }
    }