"I posted this code for future reference"The purposed of this post is to have a reference. For those knowledgeable in VB and has an idea on how to understand this codes, it's up on the reader. For the beginners, kindly search and explore what is all about this code, and how it works. Thanks!
Create Class Decleration
______________________________________________________________
Public Class Validator
Private Shared m_Title As String = "Entry Error"
Public Shared Property Title()
Get
Return m_Title
End Get
Set(ByVal value)
m_Title = value
End Set
End Property
Public Shared Function IsPresent(ByVal textBox As TextBox) As Boolean
If textBox.Text = "" Then
MessageBox.Show(textBox.Tag & " is a required field.", Title)
textBox.Select()
Return False
Else
Return True
End If
End Function
Public Shared Function IsNumeric(ByVal textBox As TextBox) As Boolean
If Microsoft.VisualBasic.IsNumeric(textBox.Text) Then
Return True
Else
MessageBox.Show(textBox.Tag & " must be numeric.", Title)
textBox.Select(0, textBox.Text.Length)
Return False
End If
End Function
Public Shared Function IsInteger(ByVal textBox As TextBox) As Boolean
Try
Convert.ToInt32(textBox.Text)
Return True
Catch ex As FormatException
MessageBox.Show(textBox.Tag & " must be an integer.", Title)
textBox.Select(0, textBox.Text.Length)
Return False
End Try
End Function
Public Shared Function IsWithinRange(ByVal textBox As TextBox, _
ByVal dMinimum As Decimal, ByVal dMaximum As Decimal) As Boolean
Dim dNumber As Decimal = CDec(textBox.Text)
If dNumber < dMinimum OrElse dNumber > dMaximum Then
MessageBox.Show(textBox.Tag & " must be between " & dMinimum _
& " and " & dMaximum & ".", Title)
textBox.Select(0, textBox.Text.Length)
Return False
Else
Return True
End If
End Function
Public Shared Function IsSelected(ByVal comboBox As ComboBox) As Boolean
If comboBox.SelectedIndex < 0 Then
MessageBox.Show("You must select a value for " & comboBox.Tag & ".", Title)
comboBox.Select()
Return False
Else
Return True
End If
End Function
End Class
_________________________________________________
FrmMain
Public Class frmMain
Private Sub btnCalcInvestment_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCalcInvestment.Click
Dim investmentForm As New frmInvestment()
investmentForm.Show()
End Sub
Private Sub btnCalcDepreciation_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCalcDepreciation.Click
Dim depreciationForm As New frmDepreciation()
depreciationForm.Show()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
FrmInvestment
Public Class frmInvestment
Private Sub btnCalculate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCalculate.Click
If IsValidData() Then
If rdoFutureValue.Checked Then
Me.CalculateFutureValue()
txtMonthlyInvestment.Select()
Else
Me.CalculateMonthlyInvestment()
txtInterestRate.Select()
End If
End If
End Sub
Private Function IsValidData() As Boolean
If rdoFutureValue.Checked Then
Return _
Validator.IsPresent(txtMonthlyInvestment) AndAlso _
Validator.IsNumeric(txtMonthlyInvestment) AndAlso _
Validator.IsWithinRange(txtMonthlyInvestment, 100, 1000) AndAlso _
Validator.IsPresent(txtInterestRate) AndAlso _
Validator.IsNumeric(txtInterestRate) AndAlso _
Validator.IsWithinRange(txtInterestRate, 0, 24) AndAlso _
Validator.IsPresent(txtYears) AndAlso _
Validator.IsInteger(txtYears) AndAlso _
Validator.IsWithinRange(txtYears, 1, 65)
Else
Return _
Validator.IsPresent(txtInterestRate) AndAlso _
Validator.IsNumeric(txtInterestRate) AndAlso _
Validator.IsWithinRange(txtInterestRate, 0, 24) AndAlso _
Validator.IsPresent(txtYears) AndAlso _
Validator.IsInteger(txtYears) AndAlso _
Validator.IsWithinRange(txtYears, 1, 65) AndAlso _
Validator.IsPresent(txtFutureValue) AndAlso _
Validator.IsNumeric(txtFutureValue) AndAlso _
Validator.IsWithinRange(txtFutureValue, 1000, 1000000)
End If
End Function
Private Sub CalculateFutureValue()
Dim iMonths As Integer = txtYears.Text * 12
Dim dInterestRate As Decimal = txtInterestRate.Text / 12 / 100
Dim dFutureValue As Decimal = _
FV(dInterestRate, iMonths, -txtMonthlyInvestment.Text, 0, _
DueDate.BegOfPeriod)
txtFutureValue.Text = FormatCurrency(dFutureValue)
End Sub
Private Sub CalculateMonthlyInvestment()
Dim iMonths As Integer = txtYears.Text * 12
Dim dInterestRate As Decimal = txtInterestRate.Text / 12 / 100
Dim dMonthlyInvestment = _
Pmt(dInterestRate, iMonths, 0, -txtFutureValue.Text, DueDate.BegOfPeriod)
txtMonthlyInvestment.Text = FormatCurrency(dMonthlyInvestment)
End Sub
Private Sub rdoFutureValue_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles rdoFutureValue.CheckedChanged
txtFutureValue.ReadOnly = True
txtFutureValue.TabStop = False
txtMonthlyInvestment.ReadOnly = False
txtMonthlyInvestment.TabStop = True
Me.ClearControls()
txtMonthlyInvestment.Select()
End Sub
Private Sub rdoMonthlyInvestment_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles rdoMonthlyInvestment.CheckedChanged
txtFutureValue.ReadOnly = False
txtFutureValue.TabStop = True
txtMonthlyInvestment.ReadOnly = True
txtMonthlyInvestment.TabStop = False
Me.ClearControls()
txtInterestRate.Select()
End Sub
Private Sub ClearControls()
txtMonthlyInvestment.Text = ""
txtInterestRate.Text = ""
txtYears.Text = ""
txtFutureValue.Text = ""
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
frmDepreciation
Public Class frmDepreciation
Private Sub frmDepreciation_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 1 To 40
cboLife.Items.Add(i)
Next
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCalculate.Click
If IsValidData() Then
lstDepreciation.Items.Clear()
For i As Integer = 1 To cboLife.Text
lstDepreciation.Items.Add("Year " & i & ": " _
& FormatCurrency(SYD(txtInitialCost.Text, _
txtFinalValue.Text, cboLife.Text, i)))
Next
txtInitialCost.Select()
End If
End Sub
Private Function IsValidData() As Boolean
If Validator.IsPresent(txtInitialCost) AndAlso _
Validator.IsNumeric(txtInitialCost) AndAlso _
Validator.IsWithinRange(txtInitialCost, 500, 1000000) Then
Dim dMaximum As Decimal = txtInitialCost.Text
If Validator.IsPresent(txtFinalValue) AndAlso _
Validator.IsNumeric(txtFinalValue) AndAlso _
Validator.IsWithinRange(txtFinalValue, 0, dMaximum) AndAlso _
Validator.IsSelected(cboLife) Then
Return True
Else
Return False
End If
Else
Return False
End If
End Function
Private Sub btnClose_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
FrmCustomerMaintenance
Public Class frmCustomerMaintenance
Dim customers As List(Of Customer)
Private Sub frmCustomerMaintenance_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
customers = CustomerDB.GetCustomers
Me.FillCustomerListBox()
End Sub
Private Sub FillCustomerListBox()
lstCustomers.Items.Clear()
For Each c As Customer In customers
lstCustomers.Items.Add(c.GetDisplayText)
Next
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAdd.Click
Dim addCustomerForm As New frmAddCustomer
addCustomerForm.ShowDialog()
If addCustomerForm.customer IsNot Nothing Then
customers.Add(addCustomerForm.customer)
CustomerDB.SaveCustomers(customers)
Me.FillCustomerListBox()
End If
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDelete.Click
Dim i As Integer = lstCustomers.SelectedIndex
If i <> -1 Then
Dim customer As Customer = customers(i)
Dim message As String = "Are you sure you want to delete " _
& customer.FirstName & " " & customer.LastName & "?"
Dim button As DialogResult = MessageBox.Show(message, _
"Confirm Delete", MessageBoxButtons.YesNo)
If button = DialogResult.Yes Then
customers.Remove(customer)
CustomerDB.SaveCustomers(customers)
Me.FillCustomerListBox()
End If
End If
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
============================================================
------------------------------------------------------------
============================================================
============================================================
------------------------------------------------------------
============================================================
Public Class Customer
Private m_FirstName As String
Private m_LastName As String
Private m_Email As String
Public Sub New()
End Sub
Public Sub New(ByVal firstName As String, _
ByVal lastName As String, _
ByVal email As String)
Me.FirstName = firstName
Me.LastName = lastName
Me.Email = email
End Sub
Public Property FirstName() As String
Get
Return m_FirstName
End Get
Set(ByVal value As String)
m_FirstName = value
End Set
End Property
Public Property LastName() As String
Get
Return m_LastName
End Get
Set(ByVal value As String)
m_LastName = value
End Set
End Property
Public Property Email() As String
Get
Return m_Email
End Get
Set(ByVal value As String)
m_Email = value
End Set
End Property
Public Function GetDisplayText() As String
Dim text As String = FirstName & " " _
& LastName & ", " _
& Email
Return text
End Function
End Class
Public Class frmAddCustomer
Public customer As Customer
Private Sub btnSave_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSave.Click
If IsValidData() Then
customer = New Customer(txtFirstName.Text, txtLastName.Text, txtEmail.Text)
Me.Close()
End If
End Sub
Private Function IsValidData() As Boolean
Return Validator.IsPresent(txtFirstName) AndAlso _
Validator.IsPresent(txtLastName) AndAlso _
Validator.IsPresent(txtEmail) AndAlso _
Validator.IsValidEmail(txtEmail)
End Function
Private Sub btnCancel_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Private Sub txtEmail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmail.TextChanged
End Sub
End Class
Public Class frmCustomerMaintenance
Dim customers As List(Of Customer)
Private Sub frmCustomerMaintenance_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
customers = CustomerDB.GetCustomers
Me.FillCustomerListBox()
End Sub
Private Sub FillCustomerListBox()
lstCustomers.Items.Clear()
For Each c As Customer In customers
lstCustomers.Items.Add(c.GetDisplayText)
Next
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAdd.Click
Dim addCustomerForm As New frmAddCustomer
addCustomerForm.ShowDialog()
If addCustomerForm.customer IsNot Nothing Then
customers.Add(addCustomerForm.customer)
CustomerDB.SaveCustomers(customers)
Me.FillCustomerListBox()
End If
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDelete.Click
Dim i As Integer = lstCustomers.SelectedIndex
If i <> -1 Then
Dim customer As Customer = customers(i)
Dim message As String = "Are you sure you want to delete " _
& customer.FirstName & " " & customer.LastName & "?"
Dim button As DialogResult = MessageBox.Show(message, _
"Confirm Delete", MessageBoxButtons.YesNo)
If button = DialogResult.Yes Then
customers.Remove(customer)
CustomerDB.SaveCustomers(customers)
Me.FillCustomerListBox()
End If
End If
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
0 comments:
Post a Comment