JayBeeOH

Simple Calculator using Module

Oct 7th, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.58 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:   Simple Calculator
  13. ' Author:         Joseph L. Bolen
  14. ' Date Created:   Sep 2014
  15. '
  16. ' Description:    The program demonstrates the use of a module for shared functions.
  17. '
  18. ' Documentation:
  19. '                 Form's screen capture is at     -- http://imgur.com/m3H4SHx
  20. '                 Original VB .Net code for Very Simple Calculator is at:
  21. '                                                 -- http://pastebin.com/PcJ9nRdZ
  22. '                 Visual Basic .Net codes is at   -- http://pastebin.com/ZhwtJxZy
  23. '                 Video tutorial at YouTube       -- http://www.youtube.com/user/bolenpresents
  24. '-------------------------------------------------------------------------------------------
  25.  
  26. Option Strict On
  27.  
  28. Public Class MainForm
  29.  
  30. #Region " Form Events"
  31.  
  32.     ' Allow form to close even if input errors exist.
  33.     Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
  34.         Handles Me.FormClosing
  35.  
  36.         e.Cancel = False
  37.     End Sub
  38.  
  39. #End Region
  40.  
  41. #Region " Control Events"
  42.  
  43.     ' Add first number to second number and show the sum.
  44.     Private Sub AddButton_Click(sender As Object, e As EventArgs) _
  45.        Handles AddButton.Click
  46.  
  47.         ' Validate input.
  48.         If Not ValidateChildren() Then
  49.             My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
  50.             Exit Sub
  51.         End If
  52.  
  53.         ' Process operation and output results to label.
  54.         ResultValueLabel.Text = AddTwoNumbers(FirstNumberTextBox.Text, SecondNumberTextBox.Text).ToString("n3")
  55.  
  56.         ' Place cursor in first input textbox.
  57.         FirstNumberTextBox.Focus()
  58.         FirstNumberTextBox.SelectAll()
  59.     End Sub
  60.  
  61.     ' Subtract the second number from first number and show the difference.
  62.     Private Sub SubtractButton_Click(sender As Object, e As EventArgs) _
  63.         Handles SubtractButton.Click
  64.  
  65.         ' Validate input.
  66.         If Not ValidateChildren() Then
  67.             My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
  68.             Exit Sub
  69.         End If
  70.  
  71.         ' Process validated input and output results to label.
  72.         ResultValueLabel.Text = SubtractTwoNumbers(FirstNumberTextBox.Text, SecondNumberTextBox.Text).ToString("n3")
  73.  
  74.         ' Place cursor in first input textbox.
  75.         FirstNumberTextBox.Focus()
  76.         FirstNumberTextBox.SelectAll()
  77.  
  78.     End Sub
  79.  
  80.     ' Multiply the first number by second number and show the product.
  81.     Private Sub MultiplyButton_Click(sender As Object, e As EventArgs) _
  82.         Handles MultiplyButton.Click
  83.  
  84.         ' Validate input.
  85.         If Not ValidateChildren() Then
  86.             My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
  87.             Exit Sub
  88.         End If
  89.  
  90.         ' Process validated input and output results to label.
  91.         ResultValueLabel.Text = MultiplyTwoNumbers(FirstNumberTextBox.Text, SecondNumberTextBox.Text).ToString("n3")
  92.  
  93.         ' Place cursor in first input textbox.
  94.         FirstNumberTextBox.Focus()
  95.         FirstNumberTextBox.SelectAll()
  96.  
  97.     End Sub
  98.  
  99.     ' Divide the first number by the second number and show the quotient.
  100.     Private Sub DivideButton_Click(sender As Object, e As EventArgs) _
  101.         Handles DivideButton.Click
  102.  
  103.         ' Validate input.
  104.         If Not ValidateChildren() Then
  105.             My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
  106.             Exit Sub
  107.         End If
  108.  
  109.         ' Process validated input and output results to label.
  110.         ResultValueLabel.Text = DivideTwoNumbers(FirstNumberTextBox.Text, SecondNumberTextBox.Text).ToString("n3")
  111.  
  112.         ' Place cursor in first input textbox.
  113.         FirstNumberTextBox.Focus()
  114.         FirstNumberTextBox.SelectAll()
  115.  
  116.     End Sub
  117.  
  118.     ' Clear the output controls and place cursor in first textbox.
  119.     Private Sub ClearButton_Click(sender As Object, e As EventArgs) _
  120.         Handles ClearButton.Click
  121.  
  122.         ' Clear output controls.
  123.         ResultValueLabel.Text = String.Empty
  124.         FirstNumberTextBox.Clear()
  125.         SecondNumberTextBox.Clear()
  126.  
  127.         ' Clear ErrorProvider
  128.         ErrorProvider1.Clear()
  129.  
  130.         ' Position cursor in first textbox.
  131.         FirstNumberTextBox.Focus()
  132.         FirstNumberTextBox.SelectAll()
  133.     End Sub
  134.  
  135.     ' Clear ResultValueLabel when either first or second
  136.     ' textbox value changes.
  137.     Private Sub ClearResult(sender As Object, e As EventArgs) _
  138.         Handles FirstNumberTextBox.TextChanged,
  139.                 SecondNumberTextBox.TextChanged
  140.  
  141.         ResultValueLabel.Text = String.Empty
  142.     End Sub
  143.  
  144.     ' Validate FirstNumberTextBox input.
  145.     Private Sub FirstNumberTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
  146.         Handles FirstNumberTextBox.Validating
  147.  
  148.         If IsNumeric(FirstNumberTextBox.Text) Then
  149.             ErrorProvider1.SetError(FirstNumberTextBox, String.Empty)
  150.         Else
  151.             ErrorProvider1.SetError(FirstNumberTextBox, "Numeric value required.")
  152.             e.Cancel = True
  153.         End If
  154.     End Sub
  155.  
  156.     ' Validate SecondNumberTextBox input.
  157.     Private Sub SecondNumberTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
  158.         Handles SecondNumberTextBox.Validating
  159.  
  160.  
  161.         If IsNumeric(SecondNumberTextBox.Text) Then
  162.             ErrorProvider1.SetError(SecondNumberTextBox, String.Empty)
  163.         Else
  164.             ErrorProvider1.SetError(SecondNumberTextBox, "Numeric value required.")
  165.             e.Cancel = True
  166.         End If
  167.     End Sub
  168.  
  169. #End Region
  170.  
  171. End Class
  172.  
  173. '=============================================================================================
  174. ' CalculatorMethods
  175. '=============================================================================================
  176. Module CalculatorMethods
  177.  
  178.     Public Function AddTwoNumbers(ByVal firstOperand As String, ByVal secondOperand As String) As Double
  179.         Dim num1 As Double = Convert.ToDouble(firstOperand)
  180.         Dim num2 As Double = Convert.ToDouble(secondOperand)
  181.         Return (num1 + num2)
  182.     End Function
  183.  
  184.     Public Function SubtractTwoNumbers(ByVal firstOperand As String, ByVal secondOperand As String) As Double
  185.         Dim num1 As Double = Convert.ToDouble(firstOperand)
  186.         Dim num2 As Double = Convert.ToDouble(secondOperand)
  187.         Return (num1 - num2)
  188.     End Function
  189.  
  190.     Public Function MultiplyTwoNumbers(ByVal firstOperand As String, ByVal secondOperand As String) As Double
  191.         Dim num1 As Double = Convert.ToDouble(firstOperand)
  192.         Dim num2 As Double = Convert.ToDouble(secondOperand)
  193.         Return (num1 * num2)
  194.     End Function
  195.  
  196.     Public Function DivideTwoNumbers(ByVal firstOperand As String, ByVal secondOperand As String) As Double
  197.         Dim num1 As Double = Convert.ToDouble(firstOperand)
  198.         Dim num2 As Double = Convert.ToDouble(secondOperand)
  199.         Return (num1 / num2)
  200.     End Function
  201.  
  202. End Module
Advertisement
Add Comment
Please, Sign In to add comment