此方法会减少 textBox1 的值以减少在 textBox2 中插入的数量。如果textBox1 中没有足够的“钱”,则应从textBox3 中取出多余的金额。最后,方法应该将文本框更新为新值,但只有 textBox2 被清除,textBox1 和 textBox3 保持不变。谁能告诉我为什么 textBox1.text = Account.toString()
不将文本框值分配给变量 Account
和 textBox3.text = Savings.toString 中的值()
未将文本框值分配给变量 Savings
中的值?
public void Debit(decimal amount)
{
decimal Account = Convert.ToDecimal(textBox1.Text);
decimal Savings = Convert.ToDecimal(textBox3.Text);
if ((Account + Savings) < amount)
if (Overdrawn != null)
Owerdrawn(this, new OverdrawnEventArgs (Account, amount));
else if (Account >= amount)
Account -= amount;
else {
amount -= Account;
Account = 0m;
Savings -= amount;
}
textBox1.Text = Account.ToString();
textBox2.Clear();
textBox3.Text = Savings.ToString();
}
最佳答案
您看到这些结果的唯一方法是遇到 overdraw 情况。如果Account + Savings < amount
- 你永远不会改变 Account
的值或Savings
在这种情况下。
在所有其他情况下,您实际上正在更改这两个值,因此文本框值也会发生变化。我很确定您想要该分支的代码是:
public void Debit(decimal amount)
{
decimal Account = Convert.ToDecimal(textBox1.Text);
decimal Savings = Convert.ToDecimal(textBox3.Text);
if ((Account + Savings) < amount)
{
if (Overdrawn != null)
Owerdrawn(this, new OverdrawnEventArgs (Account, amount));
Account = (Account + Savings) - amount;
Savings = 0m;
}
...
textBox1.Text = Account.ToString();
textBox2.Clear();
textBox3.Text = Savings.ToString();
}
关于C# Windows 窗体 - 从局部变量分配文本框值(数字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20973445/