Im in a good mood today so:
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Bank
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Account a;
private System.Windows.Forms.Label balance_lbl;
private System.Windows.Forms.TextBox deposit_TextBox;
private System.Windows.Forms.TextBox withdraw_TextBox;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label limit_lbl;
private System.Windows.Forms.Label info_lbl;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.balance_lbl = new System.Windows.Forms.Label();
this.deposit_TextBox = new System.Windows.Forms.TextBox();
this.withdraw_TextBox = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.limit_lbl = new System.Windows.Forms.Label();
this.info_lbl = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// balance_lbl
//
this.balance_lbl.Location = new System.Drawing.Point(64, 200);
this.balance_lbl.Name = "balance_lbl";
this.balance_lbl.TabIndex = 0;
//
// deposit_TextBox
//
this.deposit_TextBox.Location = new System.Drawing.Point(64, 88);
this.deposit_TextBox.Name = "deposit_TextBox";
this.deposit_TextBox.TabIndex = 1;
this.deposit_TextBox.Text = "";
this.deposit_TextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox_KeyDown);
//
// withdraw_TextBox
//
this.withdraw_TextBox.Location = new System.Drawing.Point(216, 88);
this.withdraw_TextBox.Name = "withdraw_TextBox";
this.withdraw_TextBox.TabIndex = 2;
this.withdraw_TextBox.Text = "";
this.withdraw_TextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox_KeyDown);
//
// label2
//
this.label2.Location = new System.Drawing.Point(64, 56);
this.label2.Name = "label2";
this.label2.TabIndex = 3;
this.label2.Text = "Deposit";
//
// label3
//
this.label3.Location = new System.Drawing.Point(216, 56);
this.label3.Name = "label3";
this.label3.TabIndex = 4;
this.label3.Text = "Withdraw";
//
// label1
//
this.label1.Location = new System.Drawing.Point(216, 176);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 24);
this.label1.TabIndex = 5;
this.label1.Text = "Limit";
//
// label4
//
this.label4.Location = new System.Drawing.Point(64, 176);
this.label4.Name = "label4";
this.label4.TabIndex = 6;
this.label4.Text = "Balance";
//
// limit_lbl
//
this.limit_lbl.Location = new System.Drawing.Point(216, 200);
this.limit_lbl.Name = "limit_lbl";
this.limit_lbl.TabIndex = 7;
//
// info_lbl
//
this.info_lbl.Location = new System.Drawing.Point(64, 240);
this.info_lbl.Name = "info_lbl";
this.info_lbl.Size = new System.Drawing.Size(256, 64);
this.info_lbl.TabIndex = 8;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(384, 317);
this.Controls.Add(this.info_lbl);
this.Controls.Add(this.limit_lbl);
this.Controls.Add(this.label4);
this.Controls.Add(this.label1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.withdraw_TextBox);
this.Controls.Add(this.deposit_TextBox);
this.Controls.Add(this.balance_lbl);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void textBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
if(e.KeyCode == Keys.Enter){
try{
a.addToBalance(float.Parse(deposit_TextBox.Text));
if(!a.subtractFromBalance(float.Parse(withdraw_TextBox.Text))){
balance_lbl.Text = "Credit Exceeded";
} else {
balance_lbl.Text = ""+a.getBalance();
}
info_lbl.Text = "";
} catch(FormatException fException){
info_lbl.Text = "Verify that withdraw and deposit are numbers.";
}
}
}
private void Form1_Load(object sender, System.EventArgs e) {
a = new Account(50,100);
balance_lbl.Text = a.getBalance().ToString();
limit_lbl.Text = a.getLimit().ToString();
}
}
}
and Account class:
Code:
using System;
namespace Bank
{
/// <summary>
/// Summary description for Account.
/// </summary>
public class Account
{
private float balance;
private float limit;
public Account(float startBalance,float limit) {
this.balance = startBalance;
this.limit = -limit;
}
public float getBalance() {
return this.balance;
}
public float getLimit() {
return this.limit;
}
public void addToBalance(float amount) {
this.balance += amount;
}
public bool subtractFromBalance(float amount) {
if((this.balance - amount)<this.limit){
return false;
} else {
this.balance -= amount;
return true;
}
}
}
}
Notice how i seperated the account things from the form things...
Also note how I set one event Handler for both my textBoxes.