C# Windows 窗体 - 从局部变量分配文本框值(数字)

标签 c# .net winforms textbox

此方法会减少 textBox1 的值以减少在 textBox2 中插入的数量。如果textBox1 中没有足够的“钱”,则应从textBox3 中取出多余的金额。最后,方法应该将文本框更新为新值,但只有 textBox2 被清除,textBox1 和 textBox3 保持不变。谁能告诉我为什么 textBox1.text = Account.toString() 不将文本框值分配给变量 AccounttextBox3.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/

相关文章:

c# - 如何在不重启 AppDomain 的情况下在运行时将模块添加到 MVC4

c# - 在 WebControl 中使用 `<%=` 是个坏主意吗

mysql - 这是什么日期格式? (从 Win32_PrintJob 选择 *)

C# 添加项目到 ObjectListView

c# - 在匿名方法中使用 MethodInfo.GetCurrentMethod()

c# - 我如何阻止 visual studio 2017 在保存时构建

c# - 缓存函数结果

.net - 批量上传到 Azure 中的 blob

c# - 一步一步的 oAuth rest C# winform 示例

c# - 从 WinForms 应用程序检查 .Net 框架版本