Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2014. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: Check Writing Demo
- ' Author: Joseph L. Bolen
- ' Date Created: Oct 2014
- '
- ' Description: Check Writing Demo program using multiple modules for functions
- ' and validation. Converts a check amount number and converts it to
- ' word for the "Check Amount Written" line of the check. Program uses
- ' validation with the error provider component to check input.
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/tfB69hB
- ' App's Visual Basic .NET code is at http://pastebin.com/F3GQhGFv
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- Option Strict On
- Public Class CheckForm
- #Region " Class Level Constants and Variables"
- Const PAYER_NAME As String = "John Q. Smith"
- Const PAYER_ADDRESS As String = "123 S MAIN ST"
- Const PAYER_TOWN_STATE_ZIP As String = "ANYTOWN, OH 44699-9999"
- Const ROUTING_NUMBER As String = "044000037"
- Const PAYER_ACCOUNT As String = "987654321"
- Const BANK_NAME As String = "The Bank"
- Const BANK_ADDRESS As String = "The City, OH 43271"
- Const BANK_URL As String = "www.TheBank.com"
- Private checkNumber As Integer = 1145
- #End Region
- #Region " Form Event Methods"
- ' Initialize and set default values.
- Private Sub CheckForm_Load(sender As Object, e As EventArgs) _
- Handles Me.Load
- Me.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange
- ErrorProvider1.BlinkRate = 0
- PayerNameLabel.Text = PAYER_NAME
- PayerAddressLabel.Text = PAYER_ADDRESS
- PayerTownStateZipLabel.Text = PAYER_TOWN_STATE_ZIP
- BankNameLabel.Text = BANK_NAME
- BankAddressLabel.Text = BANK_ADDRESS
- BankURLLabel.Text = BANK_URL
- MIRCRoutingLabel.Text = ROUTING_NUMBER
- MIRCAccountLabel.Text = PAYER_ACCOUNT
- CheckNumberTextBox.Text = checkNumber.ToString("G")
- CheckDateTimePicker.MinDate = Today
- CheckDateTimePicker.MaxDate = Today.AddMonths(12)
- CheckDateTimePicker.Value = Today
- PayeeTextBox.Focus()
- End Sub
- ' Allow close with errors.
- Private Sub CheckForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
- Handles MyBase.FormClosing
- e.Cancel = False
- End Sub
- #End Region
- #Region " Control Event Methods"
- ' Update MIRC Check Number field when Check Number changes.
- Private Sub CheckNumberTextBox_TextChanged(sender As Object, e As EventArgs) _
- Handles CheckNumberTextBox.TextChanged
- MIRCCheckNumberLabel.Text = CheckNumberTextBox.Text
- End Sub
- ' Validate Check Number.
- Private Sub CheckNumberTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
- Handles CheckNumberTextBox.Validating
- ErrorProvider1.SetError(CheckNumberTextBox, String.Empty)
- Try
- ValidateIsRequired(CheckNumberTextBox.Text)
- ValidateIsNumeric(CheckNumberTextBox.Text)
- ValidateInRange(CheckNumberTextBox.Text, 1, 9999)
- Dim number = Integer.Parse(CheckNumberTextBox.Text)
- CheckNumberTextBox.Text = number.ToString("G")
- Catch ex As Exception
- ErrorProvider1.SetError(CheckNumberTextBox, ex.Message)
- e.Cancel = True
- End Try
- End Sub
- ' Validate Payee line.
- Private Sub PayeeTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
- Handles PayeeTextBox.Validating
- ErrorProvider1.SetError(PayeeTextBox, String.Empty)
- Try
- ValidateIsRequired(PayeeTextBox.Text)
- ' Convert to Title Case.
- PayeeTextBox.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(PayeeTextBox.Text)
- Catch ex As Exception
- ErrorProvider1.SetError(PayeeTextBox, ex.Message)
- e.Cancel = True
- End Try
- End Sub
- ' Validate Check Amount.
- Private Sub AmountTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
- Handles AmountTextBox.Validating
- ErrorProvider1.SetError(AmountTextBox, String.Empty)
- AmountTextBox.Text = AmountTextBox.Text.Replace(",", "")
- Try
- ValidateIsRequired(AmountTextBox.Text)
- ValidateIsNumeric(AmountTextBox.Text)
- ValidateInRange(AmountTextBox.Text, 0.01, 999999999.99)
- Catch ex As Exception
- ErrorProvider1.SetError(AmountTextBox, ex.Message)
- e.Cancel = True
- End Try
- End Sub
- ' Update Written Amount Line.
- Private Sub AmountTextBox_Validated(sender As Object, e As EventArgs) _
- Handles AmountTextBox.Validated
- AmountTextBox.Text = AmountTextBox.Text.Replace(",", "")
- Dim checkAmount As Double = Convert.ToDouble(AmountTextBox.Text)
- WrittenAmountLabel.Text = CheckAmountInWords(checkAmount)
- AmountTextBox.Text = checkAmount.ToString("N")
- End Sub
- ' Clear Textboxes, reset default values and update check number.
- Private Sub ClearButton_Click(sender As Object, e As EventArgs) _
- Handles ClearButton.Click
- ErrorProvider1.Clear()
- checkNumber += 1
- CheckNumberTextBox.Text = checkNumber.ToString("G")
- CheckDateTimePicker.Value = Today
- PayeeTextBox.Clear()
- AmountTextBox.Clear()
- WrittenAmountLabel.Text = String.Empty
- MemoTextBox.Clear()
- PayeeTextBox.Focus()
- End Sub
- ' Using PowerPacks, print form.
- Private Sub PrintButton_Click(sender As Object, e As EventArgs) _
- Handles PrintButton.Click
- If Not ValidateChildren() Then
- My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
- Exit Sub
- End If
- Me.ClientSize = New System.Drawing.Size(624, 282)
- PrintButton.Visible = False
- ClearButton.Visible = False
- PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
- PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)
- PrintButton.Visible = True
- ClearButton.Visible = True
- Me.ClientSize = New System.Drawing.Size(624, 322)
- End Sub
- #End Region
- End Class
- '========================================================================================
- ' Module Validator
- '========================================================================================
- Module Validator
- Public Function ValidateIsNumeric(ByVal value As String) As Boolean
- If Not IsNumeric(value) Then
- Throw New InvalidExpressionException("Value must be numeric.")
- Return False
- Else
- Return True
- End If
- End Function
- Public Function ValidateIsRequired(ByVal value As String) As Boolean
- If String.IsNullOrWhiteSpace(value) Then
- Throw New InvalidExpressionException("A value is required.")
- Return False
- Else
- Return True
- End If
- End Function
- Public Function ValidateInRange(ByVal value As String,
- ByVal minValue As Double,
- ByVal maxValue As Double) As Boolean
- Dim valueDouble As Double = 0
- Double.TryParse(value, valueDouble)
- If valueDouble < minValue OrElse valueDouble > maxValue Then
- Throw New InvalidExpressionException(String.Format("Value is out of range, please enter a number between {0} and {1}.", minValue, maxValue))
- Return False
- Else
- Return True
- End If
- End Function
- End Module
- '========================================================================================
- ' Module CheckAmountToWords
- '========================================================================================
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2014. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Module Name: Check Amount To Words (CheckAmountToWords)
- ' Author: Joseph L. Bolen
- ' Date Created: Oct 2014
- '
- ' Description: Converts a check amount number and converts it to word for
- ' the "Check Amount Written" line of the check.
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/tfB69hB
- ' App's Visual Basic .NET code is at http://pastebin.com/F3GQhGFv
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- Module CheckAmountToWords
- #Region " Declare Private Module Level Variables"
- Private onesMap As String() = New String() {"", "one", "two", "three", "four", "five", "six", _
- "seven", "eight", "nine", "ten"}
- Private teensMap As String() = New String() {"ten", "eleven", "twelve", "thirteen", "fourteen", _
- "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
- Private tensMap As String() = New String() {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", _
- "seventy", "eighty", "ninety", "hundred"}
- Private bigPowersMap As String() = New String() {"", "thousand", "million", "billion", "trillion", _
- "quadrillion", "quintillion"}
- #End Region
- #Region " Public Methods"
- Public Function CheckAmountInWords(ByVal value As Double) As String
- Dim myAmount As String = String.Empty
- Dim cents As Integer
- Dim wholeNumber As Long
- Try
- cents = CInt(Math.Round((value * 100) Mod 100))
- wholeNumber = CLng(Math.Truncate(value))
- Catch ex As OverflowException
- Return "Too large a number to convert."
- End Try
- If wholeNumber = 0 Then
- myAmount = "zero"
- ElseIf wholeNumber < 0 Then
- myAmount = "negative "
- wholeNumber = Math.Abs(wholeNumber)
- End If
- If wholeNumber > 999 Then
- For n As Integer = bigPowersMap.GetUpperBound(0) To 1 Step -1
- If (wholeNumber >= 10 ^ (3 * n)) AndAlso (wholeNumber < 10 ^ (3 * n + 3)) Then
- Dim group As Integer = CInt(wholeNumber \ CLng(10 ^ (3 * n)))
- myAmount &= GetGroup(group) & " " & bigPowersMap(n) & " "
- wholeNumber = wholeNumber Mod CLng(10 ^ (3 * n))
- End If
- Next
- End If
- myAmount &= GetGroup(CInt(wholeNumber))
- ' Add the cents amount to the line.
- 'myAmount &= " dollars and " & GetGroup(cents) & " cents"
- myAmount &= " && " & String.Format("{0:00}", cents) & "/100 dollars"
- ' Convert to Title Case.
- myAmount = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(myAmount)
- Return myAmount
- End Function
- #End Region
- #Region " Private Methods"
- Private Function GetGroup(ByVal value As Integer) As String
- Dim returnValue As String = String.Empty
- Dim hundreds As Integer = value \ 100
- Dim tens As Integer = value Mod 100
- Dim ones As Integer = tens Mod 10
- If hundreds > 0 Then
- returnValue = onesMap(hundreds) & " hundred "
- End If
- If tens > 0 Then
- If tens < 20 Then
- ' process 1 to 19
- Select Case tens
- Case Is < 11
- returnValue &= onesMap(tens) & " "
- Case Is < 20
- returnValue &= teensMap(tens - 10) & " "
- End Select
- Else
- tens = tens \ 10
- If ones > 0 Then
- returnValue &= tensMap(tens) & "-" & onesMap(ones)
- Else
- returnValue &= tensMap(tens)
- End If
- End If
- End If
- Return Trim(returnValue)
- End Function
- #End Region
- End Module
Add Comment
Please, Sign In to add comment