银行帐户计划存款按钮无效

时间:2012-04-05 00:32:47

标签: 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!");
                }



            }

        }
    }


}
}

2 个答案:

答案 0 :(得分:2)

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

撤销按钮有效,因为有一个事件编码来处理点击:

private void btnWithdraw_Click(object sender, EventArgs e)

余额按钮没有任何此类事件。

我推荐一本好书作为参考,你可以很难用Google搜索一些编程内容并请求你的朋友帮助你。编码是艺术,科学,数学和技术的融合,而且很复杂。

答案 1 :(得分:0)

按钮内有两个if语句。其中一个或两个都可能是假的。将断点放在if语句附近,看看在调试时传递了什么值。或者如果Amountleft不大于0,则将else语句与消息框类似于你用过的消息框。你应该得到一些东西。

如果它有助于为所有值设置断点,以查看传递的值。