Practical Learning: Introducing
|
|
- Start Microsoft Visual Basic 2005 Professional or Microsoft Visual
Basic 2005
Express Edition
- To create a new application, on the main menu, click File -> New
-> Project...
- In the Templates list, click Windows Application
- Set the name to WattsALoan2 and click OK
- In the Solution Explorer, right-click Form1.vb and click Rename
- Type LoanPreparation.vb and press Enter
- To create a new form, on the main menu, click Project -> Add
Windows Form...
- In the Templates list, make sure Windows Form is selected.
Set the Name to NewEmployee and click Add
- Design the form as follows:
|
Control |
Text |
Name |
Label |
Employee #: |
|
TextBox |
|
txtEmployeeNumber |
Label |
Employee Name: |
|
TextBox |
|
txtEmployeeName |
Button |
Create |
btnCreate |
Button |
Close |
btnClose |
|
- Right-click the form and click View Code
- In the Class Name combo box, select btnClose
- In the Method Name combo box, select Click
- Change the file as follows:
Imports System.IO
Public Class NewEmployee
Private Sub btnCloseClick(ByVal sender As Object,
ByVal e As System.EventArgs) Handles btnClose.Click
Close()
End Sub
End Class
|
- In the Class Name combo box, select btnCreate
- In the Method Name combo box, select Click
- Implement the event as follows:
Private Sub btnCreateClick(ByVal sender As Object,
ByVal e As System.EventArgs) Handles btnCreate.Click
Dim strFilename As String = "C:\Watts A Loan\Employees.wal"
Dim fiEmployees As FileInfo = New FileInfo(strFilename)
Dim stwEmployees As StreamWriter = Nothing
' Normally, we should have the file already but just in case...
If Not fiEmployees.Exists Then
stwEmployees = fiEmployees.CreateText()
Else ' If the file exists already, then we will only add to it
stwEmployees = fiEmployees.AppendText()
End If
Try
stwEmployees.WriteLine(txtEmployeeNumber.Text)
stwEmployees.WriteLine(txtEmployeeName.Text)
Finally
stwEmployees.Close()
End Try
txtEmployeeNumber.Text = ""
txtEmployeeName.Text = ""
txtEmployeeNumber.Focus()
End Sub
|
- Display the LoanPreparation form
- In the Common Controls section of the Toolbox, click ToolTip and
click the form
- Design the form as follows:
|
Control |
Name |
Text |
ToolTip on toolTip1 |
Label |
|
If this is a new loan, enter a new account number and the name of the customer who is requesting the loan |
|
Label |
|
To open a previously prepared loan, enter its account number and press Tab |
|
Label |
|
Acnt #: |
|
Label |
|
Customer Name: |
|
Label |
|
Customer: |
|
TextBox |
txtAccountNumber |
|
Account number of the customer requesting
the loan |
TextBox |
txtCustomerName |
|
Name of the customer requesting the loan |
Label |
|
Empl #: |
|
Label |
|
Employee Name: |
|
Label |
|
Prepared By: |
|
TextBox |
txtEmployeeNumber |
|
Employee number of the clerk preparing the
loan |
TextBox |
txtEmployeeName |
|
Name of the clerk preparing the loan |
Button |
btnNewEmployee |
|
Used to add a new employee to the company |
Button |
btnNewCustomer |
|
Used to create an account for a new
customer |
Label |
|
Loan Amount: |
|
TextBox |
txtLoanAmount |
|
Amount of loan the customer is requesting |
Label |
|
Interest Rate: |
|
TextBox |
txtInterestRate |
|
Annual percentage rate of the loan |
Label |
|
% |
|
Label |
|
Periods |
|
TextBox |
|
txtPeriods |
The number of months the loan is supposed
to last |
Button |
btnCalculate |
Calculate |
Used to calculate the monthly payment |
Label |
|
Monthly Payment: |
|
TextBox |
txtMonthlyPayment |
|
The minimum amount the customer should pay
every month |
Button |
btnClose |
Close |
Used to close the form |
|
- Right-click the form and click View Code
- In the Class Name combo box, select btnNewEmployee
- In the Method Name combo box, select Click
- Change the file as follows:
Imports System.IO
Public Class LoanPreparation
Private Sub btnNewEmployeeClick(ByVal sender As Object,
ByVal e As System.EventArgs) Handles btnNewEmployee.Click
Dim frmNewEmployee As NewEmployee = New NewEmployee()
frmNewEmployee.ShowDialog()
End Sub
End Class
|
- In the Class Name combo box, select btnCalculate
- In the Method Name combo box, select Click
- Implement the event as
follows:
Private Sub btnCalculateClick(ByVal sender As Object,
ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim LoanAmount As Double = 0.0
Dim MonthlyPayment As Double = 0.0
Dim InterestRate As Double = 0.0
Dim Periods As Double = 0.0
Try
LoanAmount = CDbl(txtLoanAmount.Text)
Catch ex As FormatException
MsgBox("Invalid Loan Amount")
End Try
Try
InterestRate = CDbl(txtInterestRate.Text)
Catch ex As FormatException
MsgBox("Invalid Interest Rate")
End Try
Try
Periods = CDbl(txtPeriods.Text)
Catch ex As FormatException
MsgBox("Invalid Periods Value")
End Try
Try
MonthlyPayment = Pmt(
InterestRate / 12 / 100,
Periods,
-LoanAmount,
0,
DueDate.BegOfPeriod)
txtMonthlyPayment.Text = MonthlyPayment.ToString("F")
Catch ex As FormatException
MsgBox("Invalid Periods Value")
End Try
End Sub
|
- In the Class Name combo box, select (LoanPreparation Events)
- In the Method Name combo box, select Load
- Implement the Load event as follows:
Private Sub LoanPreparationLoad(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.Load
Dim strDirectory As String = "C:\Watts A Loan"
If Not Directory.Exists(strDirectory) Then
Directory.CreateDirectory(strDirectory)
Dim strFilename As String = strDirectory & "\Employees.wal"
Dim fiEmployees As FileInfo = New FileInfo(strFilename)
' If the employees file was not created already,
' then create it
If Not fiEmployees.Exists Then
Dim stwEmployees As StreamWriter = fiEmployees.CreateText()
' And create a John Doe employee
Try
stwEmployees.WriteLine("00-000")
stwEmployees.WriteLine("John Doe")
Finally
stwEmployees.Close()
End Try
End If
End If
End Sub
|
- In the Class Name combo box, select
txtAccountNumber
- In the Method Name combo box, select Leave and implement the event as
follows:
Private Sub txtAccountNumberLeave(ByVal sender As Object,
ByVal e As System.EventArgs) Handles txtAccountNumber.Leave
Dim strPath As String = "C:\Watts A Loan"
Dim diLoans As DirectoryInfo = New DirectoryInfo(strPath)
Dim aryLoans() As FileInfo = diLoans.GetFiles("*",
SearchOption.AllDirectories)
Dim strFilename As String = txtAccountNumber.Text & ".wal"
Dim strFullname As String = strPath & "none.wal"
Dim found As Boolean = False
Dim fle As FileInfo
For Each fle In aryLoans
If fle.Name = strFilename Then
found = True
strFullname = fle.FullName
End If
Next
If found = True Then
Dim stmLoans As FileStream = File.Open(strFullname,
FileMode.Open,
FileAccess.Read)
Dim bnrLoans As BinaryReader = New BinaryReader(stmLoans)
txtAccountNumber.Text = bnrLoans.ReadString()
txtCustomerName.Text = bnrLoans.ReadString()
txtEmployeeNumber.Text = bnrLoans.ReadString()
txtEmployeeName.Text = bnrLoans.ReadString()
txtLoanAmount.Text = bnrLoans.ReadString()
txtInterestRate.Text = bnrLoans.ReadString()
txtPeriods.Text = bnrLoans.ReadString()
txtMonthlyPayment.Text = bnrLoans.ReadString()
bnrLoans.Close()
stmLoans.Close()
End If
End Sub
|
- In the Class Name combo box, select
txtEmployeeNumber
- In the Method Name combo box, select
Leave
- Implement the event as follows:
Private Sub txtEmployeeNumberLeave(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles txtEmployeeNumber.Leave
Dim strFilename As String = "C:\Watts A Loan\Employees.wal"
Dim fiEmployees As FileInfo = New FileInfo(strFilename)
Dim line As String
If fiEmployees.Exists Then
If txtEmployeeNumber.Text = "" Then
txtEmployeeName.Text = ""
Exit Sub
Else
Dim stmEmployees As StreamReader = fiEmployees.OpenText()
Dim strEmployeeNumber As String
Dim strEmployeeName As String
Dim found As Boolean
strEmployeeNumber = ""
found = False
Try
Do
line = stmEmployees.ReadLine()
If line = txtEmployeeNumber.Text Then
strEmployeeName = stmEmployees.ReadLine()
txtEmployeeName.Text = strEmployeeName
found = True
End If
Loop Until line Is Nothing
' When the application has finished checking the file
' if there was no employee with that number,
' let the user know
If found = False Then
MsgBox("No employee with that number was found")
txtEmployeeName.Text = ""
txtEmployeeNumber.Focus()
End If
Finally
stmEmployees.Close()
End Try
End If
End If
End Sub
|
- In the Class Name combo box, select btnSave
- In the Method Name combo box, select Click
- Implement the event as follows:
Private Sub btnSaveClick(ByVal sender As Object,
ByVal e As System.EventArgs) Handles btnSave.Click
Dim strPath As String = "C:\Watts A Loan\" &
txtAccountNumber.Text & ".wal"
Dim stmLoan As FileStream = File.Create(strPath)
Dim bnwLoan As BinaryWriter = New BinaryWriter(stmLoan)
bnwLoan.Write(txtAccountNumber.Text)
bnwLoan.Write(txtCustomerName.Text)
bnwLoan.Write(txtEmployeeNumber.Text)
bnwLoan.Write(txtEmployeeName.Text)
bnwLoan.Write(txtLoanAmount.Text)
bnwLoan.Write(txtInterestRate.Text)
bnwLoan.Write(txtPeriods.Text)
bnwLoan.Write(txtMonthlyPayment.Text)
txtAccountNumber.Text = ""
txtCustomerName.Text = ""
txtEmployeeNumber.Text = ""
txtEmployeeName.Text = ""
txtLoanAmount.Text = ""
txtInterestRate.Text = ""
txtPeriods.Text = ""
txtMonthlyPayment.Text = ""
txtAccountNumber.Focus()
bnwLoan.Close()
stmLoan.Close()
End Sub
|
- In the Class Name combo box, select btnClose
- In the Method Name combo box, select Click
- to the form and double-click the Close button to implement
its event as follows:
Private Sub btnCloseClick(ByVal sender As Object,
ByVal e As System.EventArgs) Handles btnClose.Click
End
End Sub
|
- Execute the application to test it
- First create a few employees as follows:
Employee # |
Employee Name |
42-806 |
Patricia Katts |
75-148 |
Helene Mukoko |
36-222 |
Frank Leandro |
42-808 |
Gertrude Monay |
- Process a few loans
- Close the application
|
|