c# - 银行账户计划存款按钮不起作用

标签 c# webforms

该程序是一个具有两个选项卡的 GUI。第一个选项卡上有四个文本框,分别用于名称、ID、年龄和帐户余额。此选项卡上还有一个按钮,可将帐户添加到第二个选项卡上的组合框。在第二个选项卡上,有组合框和四个文本框,分别用于名称、ID、年龄和余额。当我从组合框中选择一个名称时,四个文本框会自动填写其信息。我遇到的问题是,我必须有一个提款和存款按钮,用户可以输入金额并将其减去或添加到文本框中的余额中。我有一些提款按钮的示例代码,有人已经帮助我解决了这个问题。谁能告诉我为什么当我按下按钮时它没有改变平衡?

class BankAccount
{
    //attributes
    public string accountID;
    public string customerName;
    public int customerAge;
    public double balance;
    public const double DEFAULT_BALANCE = 500.00;

    //construct
    public BankAccount()
    {
    }

    public BankAccount(string anID, string aName, int anAge, double aBalance)
    {
        accountID = anID;
        customerName = aName;
        customerAge = anAge;
        balance = aBalance;
        if (aBalance == 0)
        {
            balance = DEFAULT_BALANCE;
        }
        else
        {
            balance = aBalance;
        }
    }

    public BankAccount(string anID, string aName, int anAge)
    {
        accountID = anID;
        customerName = aName;
        customerAge = anAge;
        balance = DEFAULT_BALANCE;
    }






    //accessors
    public void SetID(string anID)
    {
        accountID = anID;
    }

    public void SetName(string aName)
    {
        customerName = aName;
    }

    public void SetAge(int anAge)
    {
        customerAge = anAge;
    }

    public void SetBalance(double aBalance)
    {
        balance = aBalance;
    }

    public string GetID()
    {
        return accountID;
    }

    public string GetName()
    {
        return customerName;
    }

    public int GetAge()
    {
        return customerAge;
    }

    public double GetBalance()
    {
        return balance;
    }


}

}

这是表格

 public partial class Form1 : Form
 {

    private List<BankAccount> account = new List<BankAccount>();

    public Form1()
    {
        InitializeComponent();
    }



    private void btnAddAccount_Click(object sender, EventArgs e)
    {
        BankAccount aBankAccount = new BankAccount(txtAccountID.Text, txtName.Text,
            int.Parse(txtAge.Text), double.Parse(txtBalance.Text));

        account.Add(aBankAccount);
        AddToComboBox();
        ClearText();


    }

    private void AddToComboBox()
    {
        cboAccount.Items.Clear();
        foreach (BankAccount person in account)
        {

            cboAccount.Items.Add(person.GetName());


        }


    }
    private void ClearText()
    {
        txtName.Clear();
        txtAccountID.Clear();
        txtBalance.Clear();
        txtAge.Clear();
        txtAccountID.Focus();


    }

    private void cboAccount_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtNameTab2.Text = account[cboAccount.SelectedIndex].customerName;
        txtAgeTab2.Text = account[cboAccount.SelectedIndex].customerAge.ToString();
        txtAccountIDTab2.Text = account[cboAccount.SelectedIndex].accountID.ToString();
        txtBalanceTab2.Text = account[cboAccount.SelectedIndex].balance.ToString();
    }



    private void btnWithdraw_Click(object sender, EventArgs e)
    {

        double amount = 0;

        if (double.TryParse(txtWithdraw.Text, out amount))
        {
            if (amount > 0)
            {
                BankAccount currentAccount = account[cboAccount.SelectedIndex];
                double currentBalance = currentAccount.GetBalance();
                double amountLeft = currentBalance - amount;

                if (amountLeft >= 0)
                {
                    currentAccount.SetBalance(amountLeft);
                    txtBalanceTab2.Text = amountLeft.ToString("c");
                }
                else
                {
                    MessageBox.Show("You don't have enough money!");
                }



            }

        }
    }


}
}

最佳答案

“谁能告诉我为什么当我按下按钮时它没有改变平衡?”

提款按钮之所以起作用,是因为编码了一个事件来处理点击:

private void btnWithdraw_Click(object sender, EventArgs e)

您没有任何此类平衡按钮事件。

我会推荐一本好书作为引用,通过谷歌搜索来了解一些编程知识并请你的 friend 帮助辅导你可能会很困难。编码是艺术、科学、数学和技术的融合,而且很复杂。

关于c# - 银行账户计划存款按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10021166/

相关文章:

c# - 需要积极地将刷新数据从模型推送到 View 的 WPF 应用程序的任何特定体系结构解决方案?

c# - 有什么理由不使用 XmlSerializer?

c# - Razor 助手中出现奇怪的 NullReferenceException

c# - 如何在仍然使用 ASP.NET MVC 的同时为网站开发快速原型(prototype)?

c# - ModelState.isValid()==False + 必需符号

c# - 拆分字符串并在 C# 中连接所有第一个元素,然后连接第二个元素,依此类推

c# - 在 C# 客户端上接收从 C++ 服务器发送的数据

c# - 系统格式异常 : String was not recognized as a valid DateTime - when trying to convert MM/DD/YYYY

c# - 防止 ItemTemplate 中的 LinkBut​​ton 回发并使用 Evaluated val 作为参数执行 javascript 函数

javascript - 尝试将自定义 Javascript 集成到 ASP.NET webforms 应用程序时遇到麻烦