使用textchange事件从文本框添加/减去

时间:2015-01-15 18:39:16

标签: c# winforms math textbox

我需要为每种类型的账单支付6个文本框2:

enter image description here

如果我在两个中的一个中输入一个数字,则会更新另一个数字: enter image description here

面板中的文本框将始终添加相同的总和,如果更改值,它会自动更改其相邻文本框的值,但RENT文本框不同。

我试图让RENT文本框取3对其他文本框之间的(差异/ 2)。所以,如果Patti支付的水费比迈克多100美元,那么迈克必须支付100美元的租金,这也会减少100%的租金。

我当前的code会产生这样的结果:

enter image description here

正如您所看到的,它不会正确更新租借文本框。我一直试图这样做半天多一点,我只是看不出我做错了什么或者我的数学可能出了什么问题。以下是我的文本框'textchanged events

的所有代码
    private void txtMikeWater_TextChanged(object sender, EventArgs e)
    {
        double num;
        bool parse = Double.TryParse(txtMikeWater.Text, out num);
        if (txtMikeWater.Focused == true)
            if (parse && num >= 0 && num <= expenses[2])
                txtPattiWater.Text = (expenses[2] - num).ToString();
            else
                textBoxValidator(txtMikeWater, expenses[2], 0);

        double difference = Math.Abs(num - Convert.ToDouble(txtPattiWater.Text)) / 2;
        if (num > Convert.ToDouble(txtPattiWater.Text))
        {
            txtPattiRent.Text = ((expenses[0] / 2) + difference).ToString();
            txtMikeRent.Text = ((expenses[0] / 2) - difference).ToString();
        }
        else
            txtPattiRent.Text = ((expenses[0] / 2) - difference).ToString();
            txtMikeRent.Text = ((expenses[0] / 2) + difference).ToString();
    }

    private void txtMikeElectric_TextChanged(object sender, EventArgs e)
    {
        double num;
        bool parse = Double.TryParse(txtMikeElectric.Text, out num);
        if (txtMikeElectric.Focused)
            if (parse && num >= 0 && num <= expenses[3])
                txtPattiElectric.Text = (expenses[3] - num).ToString();
        double difference = Math.Abs(num - Convert.ToDouble(txtPattiElectric.Text)) / 2;


        if (num > Convert.ToDouble(txtPattiElectric.Text))
        {
            txtPattiRent.Text = ((expenses[0] / 2) + difference).ToString();
            txtMikeRent.Text = ((expenses[0] / 2) - difference).ToString();
        }
        else if (num < Convert.ToDouble(txtPattiElectric.Text))
        {
            txtPattiRent.Text = ((expenses[0] / 2) - difference).ToString();
            txtMikeRent.Text = ((expenses[0] / 2) + difference).ToString();
        }
    }

    private void txtMikeInternet_TextChanged(object sender, EventArgs e)
    {


        double num;
        bool parse = Double.TryParse(txtMikeInternet.Text, out num);
        if (txtMikeInternet.Focused)
            if (parse && num >= 0 && num <= expenses[1])
                txtPattiInternet.Text = (expenses[1] - num).ToString();
        double difference = Math.Abs(num - Convert.ToDouble(txtPattiInternet.Text)) / 2;


        if (num > Convert.ToDouble(txtPattiInternet.Text))
        {
            txtPattiRent.Text = ((expenses[0] / 2) + difference).ToString();
            txtMikeRent.Text = ((expenses[0] / 2) - difference).ToString();
        }
        else if (num < Convert.ToDouble(txtPattiInternet.Text))
        {
            txtPattiRent.Text = ((expenses[0] / 2) - difference).ToString();
            txtMikeRent.Text = ((expenses[0] / 2) + difference).ToString();
        }
    }

    private void txtPattiWater_TextChanged(object sender, EventArgs e)
    {

        double num;
        bool parse = Double.TryParse(txtPattiWater.Text, out num);
        if (txtPattiWater.Focused == true)
            if (parse && num >= 0 && num <= expenses[2])
                txtMikeWater.Text = (expenses[2] - num).ToString();
        double difference = Math.Abs(num - Convert.ToDouble(txtMikeWater.Text)) / 2;


        if (num < Convert.ToDouble(txtMikeWater.Text))
        {
            txtPattiRent.Text = ((expenses[0] / 2) + difference).ToString();
            txtMikeRent.Text = ((expenses[0] / 2) - difference).ToString();
        }
        else
        {
            txtPattiRent.Text = ((expenses[0] / 2) - difference).ToString();
            txtMikeRent.Text = ((expenses[0] / 2) + difference).ToString();
        }

    }

    private void txtPattiElectric_TextChanged(object sender, EventArgs e)
    {

        double num;
        bool parse = Double.TryParse(txtPattiElectric.Text, out num);
        if (txtPattiElectric.Focused)
            if (parse && num >= 0 && num <= expenses[3])
                txtMikeElectric.Text = (expenses[3] - num).ToString();
        double difference = Math.Abs(num - Convert.ToDouble(txtMikeElectric.Text)) / 2;
        if (num < Convert.ToDouble(txtMikeElectric.Text))
        {
            txtPattiRent.Text = ((expenses[0] / 2) + difference).ToString();
            txtMikeRent.Text = ((expenses[0] / 2) - difference).ToString();
        }
        else if (num > Convert.ToDouble(txtMikeElectric.Text))
        {
            txtPattiRent.Text = ((expenses[0] / 2) - difference).ToString();
            txtMikeRent.Text = ((expenses[0] / 2) + difference).ToString();
        }
    }

    private void txtPattiInternet_TextChanged(object sender, EventArgs e)
    {
        double num;
        bool parse = Double.TryParse(txtPattiInternet.Text, out num);
        if (txtPattiInternet.Focused)

            if (parse && num >= 0 && num <= expenses[1])
                txtMikeInternet.Text = (expenses[1] - num).ToString();

        double difference = Math.Abs(num - Convert.ToDouble(txtMikeWater.Text)) / 2;
        if (num < Convert.ToDouble(txtMikeWater.Text))
        {
            txtPattiRent.Text = ((expenses[0] / 2) + difference).ToString();
            txtMikeRent.Text = ((expenses[0] / 2) - difference).ToString();
        }
        else if (num > Convert.ToDouble(txtMikeWater.Text))
        {
            txtPattiRent.Text = ((expenses[0] / 2) - difference).ToString();
            txtMikeRent.Text = ((expenses[0] / 2) + difference).ToString();
        }

    }

expenses变量只存储文本框对应的总和。

如果有人可以请求帮助我会很感激。

编辑只是为了更清楚一些费用变量。

expenses[0] = total rent 
expenses[1] = total internet 
expenses[2] = total water
expenses[3] = total electricity

1 个答案:

答案 0 :(得分:0)

看起来您的租金重新计算只会根据费用的差异而变化,而不是所有汇总的差异。

我建议您创建一个UpdateRent()方法,该方法总结为每个人支付的费用,然后计算差异:

private void txtPattiElectric_TextChanged(object sender, EventArgs e)
{

    double num;
    bool parse = Double.TryParse(txtPattiElectric.Text, out num);
    if (txtPattiElectric.Focused)
        if (parse && num >= 0 && num <= expenses[3])
            txtMikeElectric.Text = (expenses[3] - num).ToString();

    UpdateRent();
}

您的租金计算可能如下:

private void UpdateRent()
{
    double pattiPaid = GetPattiWater() + GetPattiElectric() + GetPattiInternet();
    double mikePaid = GetMikeWater() + GetMikeElectric() + GetMikeInternet();

    double difference = pattiPaid - mikePaid;
    double baseRent = GetRequiredMonthlyRent() / 2;

    double pattiRent = baseRent - difference;
    double mikeRent = baseRent + difference;

    txtPattiRent.Text = pattiRent.ToString();
    txtMikeRent.Text = mikeRent.ToString();
}

我还建议您将大部分解析/处理代码移动到辅助方法(如上所示),以便更容易跟踪和调试逻辑。

编辑:另请注意,该代码消除了检查以查看谁支付了更多费用。这一切都在数学上有用。例如,如果迈克支付更多费用,则difference为负数。然后从帕蒂的基本租金中“减去”负数,从而有效地增加它。如果您发现它使代码更易于理解,调试和维护,请随意重新引入if检查以执行相同的工作。

EDITx2:我原来的回答并不是很清楚。像GetPattiWater()GetRequiredMonthlyRent()这样的方法应该只是读取/返回当前值:

private double GetPattiWater()
{
    double amount;
    Double.TryParse(txtPattiWater.Text, out amount);
    return amount;
}

private double GetRequiredMonthlyRent()
{
    return expenses[3];
}

您可能需要对文本字段中的临时/空白/无效的非可解析输入进行一些特殊处理(上面只返回0),但暂时将其视为零可能更简单。