Alright Ill give a brief explanation on the code i used there.
What i first did to make that bank program was to do my layout of the form.
I added the textboxes, and all the labels.
After that i wrote the Account class, which is self explanatory ( if not just tell me ).
After that I went back to Form1.cs in the designer view, i double clicked the form, standard youŽll get thrown somewhere in the code of Form1. But Visual Studio already made a function for you ( because you double clicked the form ) thatŽs where this code comes from.
Code:
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();
}
Now what does this code do:
When the form gets loaded IŽll have an object created called a, using my own constructor written in the Account class. Thus already setting the limit and the starting balance of the account object. I then call the method getBalance and put the value into the balance label, the same happens for the limit value.
After that I went to see how I could make sure that when you hit enter it will add or subtract from the account.
So i went back to the designer view. I selected one of the two textboxes, at the properties of the selected control i then went to the thingy that looks like a flash ( the ones you have when it thunders ).
Anyway those are the events which can be triggered from within this control - in this case the textbox.
So there i select the KeyDown, and doubleclick.
You should again enter into the code somewhere, visual studio should have made a method called textboxName_Keydown(...). Now do the same thing for the other textbox just dont double click again next to the KeyDown area, you use the selection option to select the methode that was previously created ( in my example the textBox_Keydown method.
Now when either one of these two textboxes is selected - is focussed on. And you hit any key this method will start to run.
First of all I check if the key that has been pressed is the ENTER key. If so we try to execute all the code that is between the
This is just in case you enter a letter and then hit enter, since during the try we will try to convert the value from within the textboxes to floats, if this is a letter or a string it will not work and throw an exception, so if an exception is thrown we catch it - and thus making sure our program will not crash on an error like this.
Code:
catch(FormatException f) {
}
Within this piece of code you basically put everything that is needed to handle the error that is caused.
This method is not 100% watertight though, since when you dont fill anything out in the textboxes, it wont throw an exception ( i think ) - but the cod can easily be modified to also check for this thing.
So back to try block.
Code:
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 = "";
Since we know we hit enter when we are in this block of code we do what is supposed to be done, add the deposit amount and withdraw the withdrawal amount.
Since my method addToBalance accepts a float as argument i need to parse the deposit_Textbox - note that this is also the place where the FormatException can be thrown.
Then we try to subtract the withdraw amount from our balance, if this amount is over our limit then we should tell the user that we exceeded our limit/credit. If not update the balance label with the right balance.
After all this we clear our info label, because, suppose you enter a string in one of the textboxes an exception will be thrown, thus our info label will be updated and the string "Verify that withdraw and deposit are numbers." will sit as text in the label. It will stay there unless we remove it manually, thats why i putted this line:
Code:
info_lbl.Text = "";
So the error wont keep showing although nothing is wrong with our input.
Since this is a long post forgive me if there are any word mis spelled ( my english isnt that good so ...) or any typos.
[edit]
I learned alot myself by putting some controls onto the form, add some events and then go and look into your code, there should be something like
Windows Form Designer generated code
Expand this menu and youŽll see all your controls with the properties you selected in the designer view, it has helped me to understand alot of what is actually being done in the designer view.