The program is a GUI that has two tabs. On the first tab there are four textboxes for name, id, age, and account balance. There is also a button on this tab that will add the account to a combobox on the second tab. On the second tab, there is the combobox and four textboxes for name, id, age, and balance. when i select a name from the combobox the four textboxes automatically fill in with their information. The problem i'm running into is that i have to have a withdraw and a deposit button that the user can enter an amount and have it either subtracted or added to the balance in the textbox. I have some example code for the withdraw button that someone already helped me out with. Can anyone tell me why it isn't changing the balance when i hit the button?
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;
}
}
}
and here's the form
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!");
}
}
}
}
}
}
Get and Set methods in your BankAccount class. Properties might be more appropriat - psubsee2003 2012-04-05 00:43
btnWithdraw_Click event handler method? Does the event fire - psubsee2003 2012-04-05 00:46
"Can anyone tell me why it isn't changing the balance when i hit the button?"
The withdraw button works because there is a event coded in to handle the click:
private void btnWithdraw_Click(object sender, EventArgs e)
You dont have any such event for the balance button.
I'd recommend a good book as a reference, it can be hard googling to get your head around some programming things and ask your friend to help tutor you. Coding is a fusion of Art, Science, Math and Technology and it is complex.
You have two if statements inside of your button. One or both of them could be false. Put breakpoints near the if statements and see what value is being passed on debug. Or put else statements with message boxes similar to the one you've used for if Amountleft is not greater then 0. You should get something.
If it helps put breakpoints for all your values to see what values are being passed.