bigboywasim
Member
- Joined
- Sep 29, 2006
- Messages
- 18
- Programming Experience
- Beginner
VB.NET:
Option Strict On
Public Class Form1
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Try
If IsValidData() Then
Dim MonthlyInvestment As Decimal = Convert.ToDecimal(txtInvestments.Text)
Dim YearlyInterestRate As Decimal = Convert.ToDecimal(txtInterestRate.Text)
Dim NumberofYears As Integer = Convert.ToInt32(txtYears.Text)
Dim MonthlyInterestRate As Decimal = YearlyInterestRate / 12 / 100
Dim Months As Integer = NumberofYears * 12
Dim FutureValue As Decimal = Me.FutureValue(MonthlyInvestment, MonthlyInterestRate, Months)
txtFutureValue.Text = FormatCurrency(FutureValue)
txtInvestments.Select()
End If
Catch ex As Exception
MessageBox.Show(ex.Message & vbNewLine & vbNewLine & ex.GetType.ToString & vbNewLine & vbNewLine & ex.StackTrace, "Exception")
End Try
End Sub
Private Function FutureValue(ByVal MonthlyInvestment As Decimal, ByVal MonthlyInterestRate As Decimal, ByVal Months As Integer) As Decimal
For i As Integer = 1 To Months
FutureValue = (FutureValue + MonthlyInvestment) * (1 + MonthlyInterestRate)
Next
Return FutureValue
End Function
Private Function IsValidData() As Boolean
Return _
IsPresent(txtInvestments) AndAlso _
ISDecimal(txtInvestments) AndAlso _
InRange(txtInvestments, 1, 1000) AndAlso _
IsPresent(txtInterestRate) AndAlso _
ISDecimal(txtInterestRate) AndAlso _
InRange(txtInterestRate, 1, 15) AndAlso _
IsPresent(txtYears) AndAlso _
IsInt32(txtYears) AndAlso _
InRange(txtYears, 1, 50)
End Function
Private Function IsPresent(ByVal txtBox As TextBox) As Boolean
If txtBox.Text = "" Then
MessageBox.Show("Please do not leave the field blank, empty field error")
txtBox.BackColor = Color.Red
Return False
Else
Return True
End If
End Function
Private Function ISDecimal(ByVal txtBox As TextBox) As Boolean
Try
Convert.ToDecimal(txtBox.Text)
Return True
Catch ex As FormatException
MessageBox.Show("Entry must be a number, entry error")
txtBox.Focus()
txtBox.BackColor = Color.Red
Return False
End Try
End Function
Private Function IsInt32(ByVal txtBox As TextBox) As Boolean
Try
Convert.ToInt32(txtBox.Text)
Return True
Catch ex As FormatException
MessageBox.Show(" Entry must be a Whole Number or Integer, no decimal, entry error")
txtBox.Focus()
txtBox.BackColor = Color.Red
Return False
End Try
End Function
Private Function InRange(ByVal txtBox As TextBox, ByVal Min As Decimal, ByVal Max As Decimal) _
As Boolean
Dim Number As Decimal = Convert.ToDecimal(txtBox.Text)
If Number < Min OrElse Number > Max Then
MessageBox.Show("Range error, Number must be" & "(" & Min & "-" & Max & ")")
txtBox.Focus()
txtBox.BackColor = Color.Red
Return False
Else
Return True
End If
End Function
Private Sub ClearFutureValue(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtInvestments.TextChanged, txtYears.TextChanged, txtInterestRate.TextChanged
txtFutureValue.Text = ""
txtInvestments.BackColor = Color.White
txtInterestRate.BackColor = Color.White
txtYears.BackColor = Color.White
txtFutureValue.BackColor = Color.White
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
I have everything working right. I can change the textbox background color with all exceptions but the final exception in the Private Sub btnCalculate_Click. I understand the logic but I do not know how to code this in VB.Net.