从文本框使用 textchange 事件到 add/subtract
using textchange event to add/subtract from a textbox
我有 6 个文本框 2 用于需要支付的每种类型的账单:
如果我在两者之一中输入一个数字,它会更新另一个数字:
面板中的文本框将始终添加相同的总和,如果您更改值,它会自动更改其相邻文本框的值,但 RENT 文本框不同。
我正在努力使 RENT 文本框在 3 对其他文本框之间取(差值 / 2)。因此,如果 patti 比 mike 多支付 100 美元的水费,则 mike 必须支付这 100 美元的租金,这也从 patti 的租金中减去 100 美元。
我目前的 code 结果是:
如您所见,它没有正确更新租金文本框。我已经尝试这样做了半天多一点,但我看不出我做错了什么或者我的数学可能出了什么问题。这是我的文本框的文本更改事件的所有代码
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();
}
我还建议您将大部分 parsing/handling 代码移至辅助方法(如上所示),以便更容易跟踪和调试一般逻辑。
编辑:另请注意,代码消除了检查谁支付更多的费用。这一切都在数学上解决了。例如,如果 Mike 支付更多,则 difference
为负数。那么这个负数是 "subtracted" 来自 Patti 的基本租金,有效地增加了它。如果您发现它使您的代码更易于理解、调试和维护,请随时重新引入 if
检查来完成相同的工作。
EDITx2:我原来的回答不是很清楚。 GetPattiWater()
和 GetRequiredMonthlyRent()
等方法应该只是 read/return 当前值:
private double GetPattiWater()
{
double amount;
Double.TryParse(txtPattiWater.Text, out amount);
return amount;
}
private double GetRequiredMonthlyRent()
{
return expenses[3];
}
您可能需要对文本字段中的 temporary/blank/invalid 不可解析的输入做一些特殊处理(上面只是 return 0),但将其视为零可能更简单暂时.
我有 6 个文本框 2 用于需要支付的每种类型的账单:
如果我在两者之一中输入一个数字,它会更新另一个数字:
面板中的文本框将始终添加相同的总和,如果您更改值,它会自动更改其相邻文本框的值,但 RENT 文本框不同。
我正在努力使 RENT 文本框在 3 对其他文本框之间取(差值 / 2)。因此,如果 patti 比 mike 多支付 100 美元的水费,则 mike 必须支付这 100 美元的租金,这也从 patti 的租金中减去 100 美元。
我目前的 code 结果是:
如您所见,它没有正确更新租金文本框。我已经尝试这样做了半天多一点,但我看不出我做错了什么或者我的数学可能出了什么问题。这是我的文本框的文本更改事件的所有代码
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();
}
我还建议您将大部分 parsing/handling 代码移至辅助方法(如上所示),以便更容易跟踪和调试一般逻辑。
编辑:另请注意,代码消除了检查谁支付更多的费用。这一切都在数学上解决了。例如,如果 Mike 支付更多,则 difference
为负数。那么这个负数是 "subtracted" 来自 Patti 的基本租金,有效地增加了它。如果您发现它使您的代码更易于理解、调试和维护,请随时重新引入 if
检查来完成相同的工作。
EDITx2:我原来的回答不是很清楚。 GetPattiWater()
和 GetRequiredMonthlyRent()
等方法应该只是 read/return 当前值:
private double GetPattiWater()
{
double amount;
Double.TryParse(txtPattiWater.Text, out amount);
return amount;
}
private double GetRequiredMonthlyRent()
{
return expenses[3];
}
您可能需要对文本字段中的 temporary/blank/invalid 不可解析的输入做一些特殊处理(上面只是 return 0),但将其视为零可能更简单暂时.