Program 1:
Mortgage Problem w/o Error Checking

Note: If you have not yet done so, I HIGHLY recommend that you complete the two practice exercises BEFORE attempting this program!!!!

Write a mortgage calculation program that provides text boxes (controls) for the user to enter a number of parameters (size of the loan, number of years, percent yearly interest rate ) and then calculates the size of the payment for each period as well as the total interest paid over the life of the loan.  

Use text boxes for each of the input parameters and two labels for displaying the resulting size of the associated payment and the total interest. Use a command button labeled "Calculate Payment Size" to actually perform the calculations and an "Exit" button to exit the program.  By prefacing the button text property with an "&" it allows the button to be activated by using the Alt key in combination with the letter that immediately follows the "&."

The TabIndex property of each control should be set so that when the program begins the focus is in the initial field, and also so that the focus moves from field to field in the proper order whenever the Tab key is hit.   After the results of the calculations have been displayed, use the Exit button's Focus method to place the focus on the Exit button.

Remember to test your program by trying all different possible combinations of input. When testing your program, be sure to enter some incorrect data, such as text instead of numbers, decimal percentage rate instead of integer percentage rate, and unfilled fields to witness how the program behaves.  The next programming assignment will result in a program that handles such problems--this program will not.

In this version of the program you will not be required to do any error checking, but in Part II extensive error checking will be incorporated.  The program is divided into two parts so that you can see the complexity that detailed error checking can produce.  

The following table provides a description of the variables used.  Note that the variable names chosen clearly describe the purpose of the variables.  Be sure to follow this guideline in each of your programs.

Variable Name Type




number of payments per year



amount of loan



size of each payment



percent interest per year



number of years 



intermediate variable



total payments



decimal interest per payment



decimal interest per year



total interest paid


The intermediate variable decIntPerPayment is a number between zero and one, not a percentage.  In most cases the user knows the Annual Interest Rate rather than the decimal interest per payment.   Thus you need a text box named  txtPercentInterestPerYear, from which the program can derive values for the "single" variables decIntPerYr and decIntPerPayment. Use the formulas below:

Variable Formula
totalPayments loanDuration multiplied by paymentsPerYear 
decIntPerYr percentInterest divided by 100
decIntPerPayment decIntPerYr divided by paymentsPerYear
factor 1 - (1 + decIntPerPayment)-totalPayments
paySize loanAmount divided by factor
totalInt (paySize * paymentsPerYear  * loanDuration) - loanAmount

The number of payments per year should be set to 12.

NOTE: many students incorrectly translate the formula for factor into Visual Basic. The entire top portion of the formula is divided by decIntPerPayment. And (1+decintPerPayment) is raised to the power of –totalPayments.

The interface should look as much as possible like the image below.  


Experiment with VB until you learn how to set the font to MS Sans Serif, font style to Bold, and font size to 10.  In addition, figure out how to right-justify text in a text box.  

Be sure that you code is documented thoroughly.  In all programs you should include a comment block at the beginning or your program with your name, the assignment number, and a brief description of the purpose of the program.  You should embed comments in your code whenever the purpose of subsequent lines may be unclear to subsequent reviewers. 

Note that this program follows the same structure that we saw in the warm-up program:

Turn in both a CD and a printout!!!


If you have Option Strict Off, you should not get an error message when you try to read a value from a text field and store it in a local variable. However, in isolated cases students still get a message that a string can't be converted to a decimal (or double, or some other numeric type).  VB normally does implicit type conversion, but if it is not working for you then you may need to perform the type conversion explicitly, using the following conversion functions:
  • CShort - use this function to convert to Short data type
  • CInt - use this function to convert to Integer data type
  • CLng - use this function to convert to Long data type
  • CSng - use this function to convert to Single data type
  • CDbl - use this function to convert to Double data type
  • CDec - use this function to convert to Decimal data type

For example, loanAmount = CDec (txtIoanAmount.Text)

More here

Function FormatCurrency can be used to display a dollar sign, $, two places to the right of the decimal point and commas after every three digits.  The format is txtYourTextBoxName.Text = FormatCurrency(amount, 2).  You ONLY use FormatCurrency when you are outputting the variables to a form field or some other form of output.

FormatNumber and FormatPercent are also useful.

The Exit click event can consist of a single statement: Me.Close()

Strategy:  I recommend first building the interface (the GUI) and then writing the code for the Calculate Payment event.  However, instead of immediately worrying about coding the calculations, first have the button event simply report the values that it read from the text boxes (probably using a message box).  That way you break the task into three steps:  (1) building the interface, (2) determining if you can handle an event and read user input from the screen, and finally (3) performing the required calculations.