gpt4 book ai didi

c# - 使用 textchange 事件添加/减去文本框

转载 作者:行者123 更新时间:2023-11-30 12:58:56 24 4
gpt4 key购买 nike

对于需要支付的每种类型的账单,我有 6 个文本框 2:

enter image description here

如果我在两者之一中输入一个数字,它会用不同的值更新另一个: enter image description here

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

我正在努力使 RENT 文本框在 3 对其他文本框之间取(差/2)。因此,如果 patti 比 mike 多支付 100 美元的水费,则 mike 必须偿还这 100 美元的租金,这也会从 patti 的租金中扣除 100 美元。

我当前的 code结果是:

enter image description here

如您所见,它没有正确更新租金文本框。我已经尝试这样做了半天多一点,但我看不出我做错了什么或者我的数学可能出了什么问题。这是我的文本框的文本更改事件的所有代码

    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

最佳答案

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

我建议您创建一个单独的 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();
}

我还建议您将大部分解析/处理代码移至辅助方法(如上所示),以便更轻松地跟踪和调试一般逻辑。

编辑:另请注意,代码消除了检查谁支付更多的费用。这一切都在数学上解决了。例如,如果 Mike 支付更多,则 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),但暂时将其视为零可能更简单。

关于c# - 使用 textchange 事件添加/减去文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27970626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com