简体   繁体   English

银行应用程序-在C#中从类调用Windows窗体

[英]Banking Application - Calling from a class to a windows form in c#

Im learning Windows Forms in c# and finding it difficult to understand working with forms and classes. 我在C#中学习Windows窗体,发现很难理解使用窗体和类。 I am creating a banking application and want the user to be able to: press a button and see their current balance, enter an amount to debit(take money out) their account, get a success note, or a warning note if they dont have sufficient funds and the balance to update. 我正在创建银行业务应用程序,希望用户能够:按下按钮以查看其当前余额,输入借记金额(取出资金)其帐户,获得成功注释,或者如果没有,则显示警告注释足够的资金和余额来更新。

I dont understand how to connect the account class to the form and designated, labels and buttons. 我不明白如何将帐户类连接到表单和指定的标签和按钮。 I have just been trying different things first the past few hours but getting nowhere. 在过去的几个小时里,我刚开始尝试不同的方法,但是一无所获。 Could someone explain how I would show in a label box that there is a balance of 500 when a button is pressed. 有人可以解释一下当按下按钮时我将如何在标签框中显示余额为500。 Also how I could debit the account by entering an amount in a text box and pressing a button. 另外,我如何通过在文本框中输入金额并按下按钮来借记帐户。 And for a note to be wrote to a label confirming the debit. 然后将便笺写到确认借记的标签上。 I have the 'insufficient funds' message in my class but ideally I would like to have that in the form because I have heard that its better practice that way. 我班上有“资金不足”的信息,但理想情况下,我希望以表格的形式填写,因为我听说过这样更好的做法。

I hope I have been clear enough, but will confirm anything! 我希望我已经足够清楚了,但是会证实一切! I have asked for a lot of help here as I am new to windows forms, but any help or guidance at all would be appreciated! 我在这里寻求很多帮助,因为我是Windows窗体的新手,但是任何帮助或指导都将不胜感激!

class AccountClass
 {

    private decimal balance = 500;


    public AccountClass(decimal myBalance)
    {
        Balance = myBalance;

    }

    public virtual bool Debit(decimal amount)
    {
        if (amount > Balance)
        {
            Console.WriteLine("There is not enough money in your account");
            return false;
        }
        else
        {
            Balance = Balance - amount;
            return true;
        }
    }

Form: 形成:

    //button to see balance
    private void btnAccountSeeBalance_Click(object sender, EventArgs e)
    {
        //label balance should print to
        lblAccountBalance.Text = myBalance.ToString();
    }

    //button to debit account
    private void btnAccountDebit_Click(object sender, EventArgs e)
    {
        //text box to enter amount to debit
        txtAccountDebitAmount

       //label to print confirm/insufficient funds to
       lblAccountDebitSuccess
    }

Your Account class shouldn't be calling anything directly in the form. 您的Account类不应直接在表单中调用任何内容。 Have your Account class generate events or use return values from the functions. 让您的Account类生成事件或使用函数的返回值。 Your windows form should be creating and calling functions in the Account class, then you handle the response in the view. 您的Windows窗体应该在Account类中创建和调用函数,然后在视图中处理响应。 The Account class should not have a clue about the view/form. Account类不应具有有关视图/表单的线索。

Simple implementation of your clicked event: 单击事件的简单实现:

private void btnAccountDebit_Click(object sender, EventArgs e)
{
    var ac = new AccountClass( balance);
    var rtn = ac.Debit( amount);
}

This should help you get started. 这应该可以帮助您入门。 You want to keep all your account logic internal to the Account class and any messages or user interface logic in your form and UI and not inside your Account class. 您希望将所有帐户逻辑都保留在Account类的内部,并将所有消息或用户界面逻辑保留在窗体和UI中,而不是在Account类内部。

public partial class Form1 : Form
{
    private AccountClass account;

    public Form1()
    {
        InitializeComponent();

        account = new AccountClass(500);
    }

    public class AccountClass
    {

        public Decimal Balance
        {
            get;
            private set;
        }


        public AccountClass(decimal initialBalance)
        {
            Balance = initialBalance;
        }

        public void Debit(decimal amount)
        {
            if (amount > Balance)
                throw new InsufficientFundsException();

            Balance -= amount;
        }
    }

    public class InsufficientFundsException : Exception { }

    private void btnGetBalance_Click(object sender, EventArgs e)
    {
        txtBalance.Text = account.Balance.ToString();
    }

    private void btnDebit_Click(object sender, EventArgs e)
    {
        Decimal debitAmount;

        try
        {
            debitAmount = Decimal.Parse(txtAmount.Text);
        }
        catch (Exception)
        {
            MessageBox.Show("Amount is not valid");
            return;
        }

        try
        {
            account.Debit(debitAmount);
            MessageBox.Show(debitAmount + " has been debited from your account");
        }
        catch (InsufficientFundsException)
        {
            MessageBox.Show("There were insufficient funds. Your current account balance is " + account.Balance);
        }
    }

}

All I did was create a Form and drag 2 TextBoxes and Buttons on them. 我所做的就是创建一个Form并在其上拖动2个TextBoxes和Buttons。 If you name them txtBalance and txtAmount, and btnGetBalance and btnDebit and connect them to the click events then you can use this code. 如果将它们命名为txtBalance和txtAmount以及btnGetBalance和btnDebit并将它们连接到click事件,则可以使用此代码。

edit: 编辑:

I put all the code in the Form class for brevity and simplicity. 为了简洁起见,我将所有代码都放在Form类中。 But obviously those extra classes should be separate code files. 但是显然那些额外的类应该是单独的代码文件。 Considering what I said about separating UI and Account logic. 考虑一下我所说的关于分离UI和帐户逻辑的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM