Mastering VB Properties |
Page 4 of 4
Program - a Mortgage CalculatorThis demonstration program is will give you an estimate of your monthly repayments on a mortgage or an accurate value for most other loans. The reason why it is only an estimate for a mortgage is that it doesn't take any tax relief or other charges into account - so the actual monthly repayment could be a few pounds different. What this program calculates isn't really important. Almost any calculation would illustrate the basic ideas of getting an input values and displaying results using properties. This program uses nothing that hasn't been described in detail - so you should try to understand its workings and see if you can improve on it. The first step is to construct the form. This is made up of four label objects with their captions changed to "Amount of loan", "Term of loan" and so on... These simply serve to label the input text boxes and the result. You also need three Textboxes named Text1 to Text3 and one more Label named Label5 to accept user input and display the final result. These should be arranged as shown on the form but it doesn't really matter what the calculator looks like! Finally you need to add a button called Button1.
We could concentrate on making the form look a little prettier before moving on but let's make it work first. The idea is that the user will type values into the three Textboxes and then click on Button1 to calculate the new result. This means that the event handler Button1_Click has to contain the instructions to retrieve the values from the Textboxes, work out the answer and store this back into the Label. The quickest way to get to the buttons click event handler is to double click on it. When you see the event handler window appear type in the following:Private Sub Button1_Click( The first three instructions retrieve the values that the user has typed into the Textboxes into suitable variables. The annual interest rate has to be converted into a monthly rate and the term in years into months. The actual repayment calculation is performed by one of the financial functions which are supplied with the system. The Pmt function will calculate the repayment on a loan at a given interest rate and term - repayments = pmt(rate,term,-loan,0,0) The minus sign is needed because loan values are assumed to be a negative flow of cash while repayments are positive and at this stage don't worry what the two zeros are. The final instruction simply stores the result into Label5's Text property. If you run the program after typing the instructions in you will find that you can type in values and see the result when you click on Button1. However the program still isn't perfect. For example, it will crash if you type in silly values into the text boxes and it crashes if you want to enter the interest rate with a percentage sign. The whole program looks a lot better if you set the initial values Text properties to sensible values.
The techniques required to stop silly values crashing the program take us a little further into VB than we have reached but it can be done and it isn't difficult. Essentially all you have to do is include instructions which check the values before trying to use them. It is also possible to make the whole calculator look more like a real calculator complete with displays and buttons - but that's just icing on the cake. To access the complete project including the graphics once you have registered, click on CodeBin.
This is the second chapter in an eBook introducing Visual Basic 2010. See also:
|