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: Simple Calculator
- ' Author: Joseph L. Bolen
- ' Date Created: Sep 2014
- '
- ' Description: The program demonstrates the use of a module for shared functions.
- '
- ' Documentation:
- ' Form's screen capture is at -- http://imgur.com/m3H4SHx
- ' Original VB .Net code for Very Simple Calculator is at:
- ' -- http://pastebin.com/PcJ9nRdZ
- ' Visual Basic .Net codes is at -- http://pastebin.com/ZhwtJxZy
- ' Video tutorial at YouTube -- http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- Option Strict On
- Public Class MainForm
- #Region " Form Events"
- ' Allow form to close even if input errors exist.
- Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
- Handles Me.FormClosing
- e.Cancel = False
- End Sub
- #End Region
- #Region " Control Events"
- ' Add first number to second number and show the sum.
- Private Sub AddButton_Click(sender As Object, e As EventArgs) _
- Handles AddButton.Click
- ' Validate input.
- If Not ValidateChildren() Then
- My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
- Exit Sub
- End If
- ' Process operation and output results to label.
- ResultValueLabel.Text = AddTwoNumbers(FirstNumberTextBox.Text, SecondNumberTextBox.Text).ToString("n3")
- ' Place cursor in first input textbox.
- FirstNumberTextBox.Focus()
- FirstNumberTextBox.SelectAll()
- End Sub
- ' Subtract the second number from first number and show the difference.
- Private Sub SubtractButton_Click(sender As Object, e As EventArgs) _
- Handles SubtractButton.Click
- ' Validate input.
- If Not ValidateChildren() Then
- My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
- Exit Sub
- End If
- ' Process validated input and output results to label.
- ResultValueLabel.Text = SubtractTwoNumbers(FirstNumberTextBox.Text, SecondNumberTextBox.Text).ToString("n3")
- ' Place cursor in first input textbox.
- FirstNumberTextBox.Focus()
- FirstNumberTextBox.SelectAll()
- End Sub
- ' Multiply the first number by second number and show the product.
- Private Sub MultiplyButton_Click(sender As Object, e As EventArgs) _
- Handles MultiplyButton.Click
- ' Validate input.
- If Not ValidateChildren() Then
- My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
- Exit Sub
- End If
- ' Process validated input and output results to label.
- ResultValueLabel.Text = MultiplyTwoNumbers(FirstNumberTextBox.Text, SecondNumberTextBox.Text).ToString("n3")
- ' Place cursor in first input textbox.
- FirstNumberTextBox.Focus()
- FirstNumberTextBox.SelectAll()
- End Sub
- ' Divide the first number by the second number and show the quotient.
- Private Sub DivideButton_Click(sender As Object, e As EventArgs) _
- Handles DivideButton.Click
- ' Validate input.
- If Not ValidateChildren() Then
- My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
- Exit Sub
- End If
- ' Process validated input and output results to label.
- ResultValueLabel.Text = DivideTwoNumbers(FirstNumberTextBox.Text, SecondNumberTextBox.Text).ToString("n3")
- ' Place cursor in first input textbox.
- FirstNumberTextBox.Focus()
- FirstNumberTextBox.SelectAll()
- End Sub
- ' Clear the output controls and place cursor in first textbox.
- Private Sub ClearButton_Click(sender As Object, e As EventArgs) _
- Handles ClearButton.Click
- ' Clear output controls.
- ResultValueLabel.Text = String.Empty
- FirstNumberTextBox.Clear()
- SecondNumberTextBox.Clear()
- ' Clear ErrorProvider
- ErrorProvider1.Clear()
- ' Position cursor in first textbox.
- FirstNumberTextBox.Focus()
- FirstNumberTextBox.SelectAll()
- End Sub
- ' Clear ResultValueLabel when either first or second
- ' textbox value changes.
- Private Sub ClearResult(sender As Object, e As EventArgs) _
- Handles FirstNumberTextBox.TextChanged,
- SecondNumberTextBox.TextChanged
- ResultValueLabel.Text = String.Empty
- End Sub
- ' Validate FirstNumberTextBox input.
- Private Sub FirstNumberTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
- Handles FirstNumberTextBox.Validating
- If IsNumeric(FirstNumberTextBox.Text) Then
- ErrorProvider1.SetError(FirstNumberTextBox, String.Empty)
- Else
- ErrorProvider1.SetError(FirstNumberTextBox, "Numeric value required.")
- e.Cancel = True
- End If
- End Sub
- ' Validate SecondNumberTextBox input.
- Private Sub SecondNumberTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
- Handles SecondNumberTextBox.Validating
- If IsNumeric(SecondNumberTextBox.Text) Then
- ErrorProvider1.SetError(SecondNumberTextBox, String.Empty)
- Else
- ErrorProvider1.SetError(SecondNumberTextBox, "Numeric value required.")
- e.Cancel = True
- End If
- End Sub
- #End Region
- End Class
- '=============================================================================================
- ' CalculatorMethods
- '=============================================================================================
- Module CalculatorMethods
- Public Function AddTwoNumbers(ByVal firstOperand As String, ByVal secondOperand As String) As Double
- Dim num1 As Double = Convert.ToDouble(firstOperand)
- Dim num2 As Double = Convert.ToDouble(secondOperand)
- Return (num1 + num2)
- End Function
- Public Function SubtractTwoNumbers(ByVal firstOperand As String, ByVal secondOperand As String) As Double
- Dim num1 As Double = Convert.ToDouble(firstOperand)
- Dim num2 As Double = Convert.ToDouble(secondOperand)
- Return (num1 - num2)
- End Function
- Public Function MultiplyTwoNumbers(ByVal firstOperand As String, ByVal secondOperand As String) As Double
- Dim num1 As Double = Convert.ToDouble(firstOperand)
- Dim num2 As Double = Convert.ToDouble(secondOperand)
- Return (num1 * num2)
- End Function
- Public Function DivideTwoNumbers(ByVal firstOperand As String, ByVal secondOperand As String) As Double
- Dim num1 As Double = Convert.ToDouble(firstOperand)
- Dim num2 As Double = Convert.ToDouble(secondOperand)
- Return (num1 / num2)
- End Function
- End Module
Advertisement
Add Comment
Please, Sign In to add comment