JayBeeOH

Check Writing Demo

Oct 22nd, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 13.65 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2014. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Check Writing Demo
  13. ' Author:         Joseph L. Bolen
  14. ' Date Created:   Oct 2014
  15. '
  16. ' Description:    Check Writing Demo program using multiple modules for functions
  17. '                 and validation. Converts a check amount number and converts it to
  18. '                 word for the "Check Amount Written" line of the check. Program uses
  19. '                 validation with the error provider component to check input.
  20. '
  21. '                 Documentation is at:
  22. '                   App's screen image is at: http://imgur.com/tfB69hB
  23. '                   App's Visual Basic .NET code is at http://pastebin.com/F3GQhGFv
  24. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  25. '-------------------------------------------------------------------------------------------
  26.  
  27. Option Strict On
  28.  
  29. Public Class CheckForm
  30.  
  31. #Region " Class Level Constants and Variables"
  32.  
  33.     Const PAYER_NAME As String = "John Q. Smith"
  34.     Const PAYER_ADDRESS As String = "123 S MAIN ST"
  35.     Const PAYER_TOWN_STATE_ZIP As String = "ANYTOWN, OH 44699-9999"
  36.     Const ROUTING_NUMBER As String = "044000037"
  37.     Const PAYER_ACCOUNT As String = "987654321"
  38.     Const BANK_NAME As String = "The Bank"
  39.     Const BANK_ADDRESS As String = "The City, OH 43271"
  40.     Const BANK_URL As String = "www.TheBank.com"
  41.  
  42.     Private checkNumber As Integer = 1145
  43.  
  44. #End Region
  45.  
  46. #Region " Form Event Methods"
  47.  
  48.     ' Initialize and set default values.
  49.     Private Sub CheckForm_Load(sender As Object, e As EventArgs) _
  50.        Handles Me.Load
  51.  
  52.         Me.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange
  53.  
  54.         ErrorProvider1.BlinkRate = 0
  55.  
  56.         PayerNameLabel.Text = PAYER_NAME
  57.         PayerAddressLabel.Text = PAYER_ADDRESS
  58.         PayerTownStateZipLabel.Text = PAYER_TOWN_STATE_ZIP
  59.  
  60.         BankNameLabel.Text = BANK_NAME
  61.         BankAddressLabel.Text = BANK_ADDRESS
  62.         BankURLLabel.Text = BANK_URL
  63.  
  64.         MIRCRoutingLabel.Text = ROUTING_NUMBER
  65.         MIRCAccountLabel.Text = PAYER_ACCOUNT
  66.  
  67.         CheckNumberTextBox.Text = checkNumber.ToString("G")
  68.  
  69.         CheckDateTimePicker.MinDate = Today
  70.         CheckDateTimePicker.MaxDate = Today.AddMonths(12)
  71.         CheckDateTimePicker.Value = Today
  72.  
  73.         PayeeTextBox.Focus()
  74.  
  75.     End Sub
  76.  
  77.     ' Allow close with errors.
  78.     Private Sub CheckForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
  79.         Handles MyBase.FormClosing
  80.  
  81.         e.Cancel = False
  82.     End Sub
  83.  
  84. #End Region
  85.  
  86. #Region " Control Event Methods"
  87.  
  88.     ' Update MIRC Check Number field when Check Number changes.
  89.     Private Sub CheckNumberTextBox_TextChanged(sender As Object, e As EventArgs) _
  90.         Handles CheckNumberTextBox.TextChanged
  91.  
  92.         MIRCCheckNumberLabel.Text = CheckNumberTextBox.Text
  93.     End Sub
  94.  
  95.     ' Validate Check Number.
  96.     Private Sub CheckNumberTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
  97.         Handles CheckNumberTextBox.Validating
  98.  
  99.         ErrorProvider1.SetError(CheckNumberTextBox, String.Empty)
  100.         Try
  101.             ValidateIsRequired(CheckNumberTextBox.Text)
  102.             ValidateIsNumeric(CheckNumberTextBox.Text)
  103.             ValidateInRange(CheckNumberTextBox.Text, 1, 9999)
  104.             Dim number = Integer.Parse(CheckNumberTextBox.Text)
  105.             CheckNumberTextBox.Text = number.ToString("G")
  106.         Catch ex As Exception
  107.             ErrorProvider1.SetError(CheckNumberTextBox, ex.Message)
  108.             e.Cancel = True
  109.         End Try
  110.     End Sub
  111.  
  112.     ' Validate Payee line.
  113.     Private Sub PayeeTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
  114.         Handles PayeeTextBox.Validating
  115.  
  116.         ErrorProvider1.SetError(PayeeTextBox, String.Empty)
  117.         Try
  118.             ValidateIsRequired(PayeeTextBox.Text)
  119.             ' Convert to Title Case.
  120.             PayeeTextBox.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(PayeeTextBox.Text)
  121.         Catch ex As Exception
  122.             ErrorProvider1.SetError(PayeeTextBox, ex.Message)
  123.             e.Cancel = True
  124.         End Try
  125.     End Sub
  126.  
  127.     ' Validate Check Amount.
  128.     Private Sub AmountTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
  129.         Handles AmountTextBox.Validating
  130.  
  131.         ErrorProvider1.SetError(AmountTextBox, String.Empty)
  132.         AmountTextBox.Text = AmountTextBox.Text.Replace(",", "")
  133.         Try
  134.             ValidateIsRequired(AmountTextBox.Text)
  135.             ValidateIsNumeric(AmountTextBox.Text)
  136.             ValidateInRange(AmountTextBox.Text, 0.01, 999999999.99)
  137.         Catch ex As Exception
  138.             ErrorProvider1.SetError(AmountTextBox, ex.Message)
  139.             e.Cancel = True
  140.         End Try
  141.     End Sub
  142.  
  143.     ' Update Written Amount Line.
  144.     Private Sub AmountTextBox_Validated(sender As Object, e As EventArgs) _
  145.         Handles AmountTextBox.Validated
  146.  
  147.         AmountTextBox.Text = AmountTextBox.Text.Replace(",", "")
  148.         Dim checkAmount As Double = Convert.ToDouble(AmountTextBox.Text)
  149.         WrittenAmountLabel.Text = CheckAmountInWords(checkAmount)
  150.         AmountTextBox.Text = checkAmount.ToString("N")
  151.     End Sub
  152.  
  153.     ' Clear Textboxes, reset default values and update check number.
  154.     Private Sub ClearButton_Click(sender As Object, e As EventArgs) _
  155.         Handles ClearButton.Click
  156.  
  157.         ErrorProvider1.Clear()
  158.         checkNumber += 1
  159.         CheckNumberTextBox.Text = checkNumber.ToString("G")
  160.         CheckDateTimePicker.Value = Today
  161.         PayeeTextBox.Clear()
  162.         AmountTextBox.Clear()
  163.         WrittenAmountLabel.Text = String.Empty
  164.         MemoTextBox.Clear()
  165.         PayeeTextBox.Focus()
  166.  
  167.     End Sub
  168.  
  169.     ' Using PowerPacks, print form.
  170.     Private Sub PrintButton_Click(sender As Object, e As EventArgs) _
  171.         Handles PrintButton.Click
  172.  
  173.         If Not ValidateChildren() Then
  174.             My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
  175.             Exit Sub
  176.         End If
  177.  
  178.         Me.ClientSize = New System.Drawing.Size(624, 282)
  179.         PrintButton.Visible = False
  180.         ClearButton.Visible = False
  181.         PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
  182.         PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)
  183.         PrintButton.Visible = True
  184.         ClearButton.Visible = True
  185.         Me.ClientSize = New System.Drawing.Size(624, 322)
  186.     End Sub
  187.  
  188. #End Region
  189.  
  190. End Class
  191.  
  192. '========================================================================================
  193. ' Module Validator
  194. '========================================================================================
  195.  
  196. Module Validator
  197.  
  198.     Public Function ValidateIsNumeric(ByVal value As String) As Boolean
  199.  
  200.         If Not IsNumeric(value) Then
  201.             Throw New InvalidExpressionException("Value must be numeric.")
  202.             Return False
  203.         Else
  204.             Return True
  205.         End If
  206.  
  207.     End Function
  208.  
  209.     Public Function ValidateIsRequired(ByVal value As String) As Boolean
  210.  
  211.         If String.IsNullOrWhiteSpace(value) Then
  212.             Throw New InvalidExpressionException("A value is required.")
  213.             Return False
  214.         Else
  215.             Return True
  216.         End If
  217.  
  218.     End Function
  219.  
  220.     Public Function ValidateInRange(ByVal value As String,
  221.                                   ByVal minValue As Double,
  222.                                   ByVal maxValue As Double) As Boolean
  223.  
  224.         Dim valueDouble As Double = 0
  225.         Double.TryParse(value, valueDouble)
  226.         If valueDouble < minValue OrElse valueDouble > maxValue Then
  227.             Throw New InvalidExpressionException(String.Format("Value is out of range, please enter a number between {0} and {1}.", minValue, maxValue))
  228.             Return False
  229.         Else
  230.             Return True
  231.         End If
  232.  
  233.     End Function
  234. End Module
  235.  
  236. '========================================================================================
  237. ' Module CheckAmountToWords
  238. '========================================================================================
  239.  
  240. '------------------------------------------------------------------------------------------
  241. '           Notice of My Copyright and Intellectual Property Rights
  242. '
  243. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  244. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  245. ' publish or provide such intellectual property to any other person or entity for any
  246. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  247. '
  248. '                 Copyright © 2014. All rights reserved.
  249. '        All trademarks remain the property of their respective owners.
  250. '-------------------------------------------------------------------------------------------
  251. ' Module Name:    Check Amount To Words (CheckAmountToWords)
  252.  
  253. ' Author:         Joseph L. Bolen
  254. ' Date Created:   Oct 2014
  255. '
  256. ' Description:    Converts a check amount number and converts it to word for
  257. '                 the "Check Amount Written" line of the check.
  258. '
  259. '                 Documentation is at:
  260. '                   App's screen image is at: http://imgur.com/tfB69hB
  261. '                   App's Visual Basic .NET code is at http://pastebin.com/F3GQhGFv
  262. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  263. '-------------------------------------------------------------------------------------------
  264.  
  265. Module CheckAmountToWords
  266.  
  267. #Region " Declare Private Module Level Variables"
  268.  
  269.     Private onesMap As String() = New String() {"", "one", "two", "three", "four", "five", "six", _
  270.                                                 "seven", "eight", "nine", "ten"}
  271.  
  272.     Private teensMap As String() = New String() {"ten", "eleven", "twelve", "thirteen", "fourteen", _
  273.                                                  "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
  274.  
  275.     Private tensMap As String() = New String() {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", _
  276.                                                 "seventy", "eighty", "ninety", "hundred"}
  277.  
  278.     Private bigPowersMap As String() = New String() {"", "thousand", "million", "billion", "trillion", _
  279.                                                      "quadrillion", "quintillion"}
  280.  
  281. #End Region
  282.  
  283. #Region " Public Methods"
  284.  
  285.     Public Function CheckAmountInWords(ByVal value As Double) As String
  286.  
  287.         Dim myAmount As String = String.Empty
  288.         Dim cents As Integer
  289.         Dim wholeNumber As Long
  290.  
  291.         Try
  292.             cents = CInt(Math.Round((value * 100) Mod 100))
  293.             wholeNumber = CLng(Math.Truncate(value))
  294.         Catch ex As OverflowException
  295.             Return "Too large a number to convert."
  296.         End Try
  297.  
  298.         If wholeNumber = 0 Then
  299.             myAmount = "zero"
  300.         ElseIf wholeNumber < 0 Then
  301.             myAmount = "negative "
  302.             wholeNumber = Math.Abs(wholeNumber)
  303.         End If
  304.  
  305.         If wholeNumber > 999 Then
  306.             For n As Integer = bigPowersMap.GetUpperBound(0) To 1 Step -1
  307.                 If (wholeNumber >= 10 ^ (3 * n)) AndAlso (wholeNumber < 10 ^ (3 * n + 3)) Then
  308.                     Dim group As Integer = CInt(wholeNumber \ CLng(10 ^ (3 * n)))
  309.                     myAmount &= GetGroup(group) & " " & bigPowersMap(n) & " "
  310.                     wholeNumber = wholeNumber Mod CLng(10 ^ (3 * n))
  311.                 End If
  312.             Next
  313.         End If
  314.  
  315.         myAmount &= GetGroup(CInt(wholeNumber))
  316.  
  317.         ' Add the cents amount to the line.
  318.         'myAmount &= " dollars and " & GetGroup(cents) & " cents"
  319.         myAmount &= " && " & String.Format("{0:00}", cents) & "/100 dollars"
  320.  
  321.         ' Convert to Title Case.
  322.         myAmount = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(myAmount)
  323.  
  324.         Return myAmount
  325.     End Function
  326.  
  327. #End Region
  328.  
  329. #Region " Private Methods"
  330.  
  331.     Private Function GetGroup(ByVal value As Integer) As String
  332.  
  333.         Dim returnValue As String = String.Empty
  334.         Dim hundreds As Integer = value \ 100
  335.         Dim tens As Integer = value Mod 100
  336.         Dim ones As Integer = tens Mod 10
  337.  
  338.         If hundreds > 0 Then
  339.             returnValue = onesMap(hundreds) & " hundred "
  340.         End If
  341.  
  342.         If tens > 0 Then
  343.             If tens < 20 Then
  344.                 ' process 1 to 19
  345.                 Select Case tens
  346.                     Case Is < 11
  347.                         returnValue &= onesMap(tens) & " "
  348.                     Case Is < 20
  349.                         returnValue &= teensMap(tens - 10) & " "
  350.                 End Select
  351.             Else
  352.                 tens = tens \ 10
  353.                 If ones > 0 Then
  354.                     returnValue &= tensMap(tens) & "-" & onesMap(ones)
  355.                 Else
  356.                     returnValue &= tensMap(tens)
  357.                 End If
  358.             End If
  359.         End If
  360.  
  361.         Return Trim(returnValue)
  362.     End Function
  363.  
  364. #End Region
  365.  
  366. End Module
Add Comment
Please, Sign In to add comment