Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Very Simple Calculator - Documentation:
- ' YouTube video tutorial at -- http://www.youtube.com/user/bolenpresents
- ' Form's screen capture is at -- http://imgur.com/m3H4SHx
- ' Visual Basic .Net codes is at -- http://pastebin.com/PcJ9nRdZ
- '------------------------------------------------------------------------------------------
- ' 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: Very Simple Calculator
- ' Author: Joseph L. Bolen
- ' Date Created: Jul 2014
- '
- ' Description: A very simple calculator demonstration in Visual
- ' Basic .NET to emphasis the need to document one's
- ' program.
- '
- '-------------------------------------------------------------------------------------------
- ' Modification History:
- '
- ' Date By Description
- ' 07/02/2014 jlb1 Cosmetic fix to output. ToString("N3")
- ' 07/29/2014 jlb2 Data validation with ErrorProvider & Clear on Input TextChanged.
- ' 07/31/2014 jlb3 Data validation using the validating event. Also, form's AutoValidate
- ' property changed to EnableAllowFocusChange.
- '-------------------------------------------------------------------------------------------
- Option Strict On
- Public Class MainForm
- ' Add first number to second number and show the sum.
- Private Sub AddButton_Click(sender As Object, e As EventArgs) _
- Handles AddButton.Click
- ' Declare variables.
- Dim firstNumber As Double
- Dim secondNumber As Double
- Dim sum As Double
- ' Validate input - jlb3.
- If Not ValidateChildren() Then
- My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
- Exit Sub
- End If
- ' Convert text/string to a numeric data type.
- firstNumber = CDbl(FirstNumberTextBox.Text)
- secondNumber = CDbl(SecondNumberTextBox.Text)
- ' Process / Calculate sum.
- sum = firstNumber + secondNumber
- ' Convert number to string and output it to a label.
- 'ResultValueLabel.Text = CStr(sum) ' Visual Basic
- 'ResultValueLabel.Text = String.Format("{0:N3}", sum) ' Visual Basic
- ResultValueLabel.Text = sum.ToString("N3") ' .NET Framework - JLB1
- ' 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
- ' Declare variables.
- Dim firstNumber As Double
- Dim secondNumber As Double
- Dim difference As Double
- ' Validate input - jlb3.
- If Not ValidateChildren() Then
- My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
- Exit Sub
- End If
- ' Convert text/string to a numeric data type.
- firstNumber = CDbl(FirstNumberTextBox.Text)
- secondNumber = CDbl(SecondNumberTextBox.Text)
- ' Process / Calculate difference.
- difference = firstNumber - secondNumber
- ' Convert number to string and output it to a label - jlb1.
- ResultValueLabel.Text = difference.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
- ' Declare variables.
- Dim firstNumber As Double
- Dim secondNumber As Double
- Dim product As Double
- ' Validate input - jlb3.
- If Not ValidateChildren() Then
- My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
- Exit Sub
- End If
- ' Convert text/string to a numeric data type.
- firstNumber = CDbl(FirstNumberTextBox.Text)
- secondNumber = CDbl(SecondNumberTextBox.Text)
- ' Process / Calculate product.
- product = firstNumber * secondNumber
- ' Convert number to string and output it to a label - jlb1.
- ResultValueLabel.Text = product.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
- ' Declare variables.
- Dim firstNumber As Double
- Dim secondNumber As Double
- Dim quotient As Double
- ' Validate input - jlb3.
- If Not ValidateChildren() Then
- My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
- Exit Sub
- End If
- ' Convert text/string to a numeric data type.
- firstNumber = CDbl(FirstNumberTextBox.Text)
- secondNumber = CDbl(SecondNumberTextBox.Text)
- ' Process / Calculate quotient.
- quotient = firstNumber / secondNumber
- ' Convert number to string and output it to a label - jlb1.
- ResultValueLabel.Text = quotient.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 - jlb3.
- Private Sub FirstNumberTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
- Handles FirstNumberTextBox.Validating
- If Not IsNumeric(FirstNumberTextBox.Text) Then
- ErrorProvider1.SetError(FirstNumberTextBox, "Enter only a numeric value.")
- e.Cancel = True
- 'ElseIf
- Else
- ErrorProvider1.SetError(FirstNumberTextBox, String.Empty)
- End If
- End Sub
- ' Validate SecondNumberTextBox input - jlb3.
- Private Sub SecondNumberTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
- Handles SecondNumberTextBox.Validating
- If Not IsNumeric(SecondNumberTextBox.Text) Then
- ErrorProvider1.SetError(SecondNumberTextBox, "Enter only a numeric value.")
- e.Cancel = True
- 'ElseIf
- Else
- ErrorProvider1.SetError(SecondNumberTextBox, String.Empty)
- End If
- End Sub
- ' Allow form to close even if input errors exist - jlb3.
- Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
- Handles Me.FormClosing
- e.Cancel = False
- End Sub
- End Class
- ' ======================================================================
- ' Comments and Pseudocode
- ' ======================================================================
- ' Add first number to second number and show the sum.
- 'TODO: Declare variables.
- 'TODO: Convert text/string to a numeric data type.
- 'TODO: Process / Calculate sum.
- 'TODO: Convert number to string and output it to a label.
- 'ResultValueLabel.Text = CStr(sum) ' Visual Basic
- 'ResultValueLabel.Text = String.Format("{0:N3}", sum) ' Visual Basic
- 'ResultValueLabel.Text = sum.ToString("N3") ' .NET Framework
- ' Subtract the second number from first number and show the difference.
- 'TODO: Declare variables.
- 'TODO: Convert text/string to a numeric data type.
- 'TODO: Process / Calculate difference.
- 'TODO: Convert number to string and output it to a label.
- ' Multiply the first number by second number and show the product.
- 'TODO: Declare variables.
- 'TODO: Convert text/string to a numeric data type.
- 'TODO: Process / Calculate product.
- 'TODO: Convert number to string and output it to a label.
- ' Divide the first number by the second number and show the quotient.
- 'TODO: Declare variables.
- 'TODO: Convert text/string to a numeric data type.
- 'TODO: Process / Calculate quotient.
- 'TODO: Convert number to string and output it to a label.
- ' Clear the output controls and place cursor in first textbox.
- 'TODO: Clear output controls.
- 'TODO: Position cursor in first textbox.
- ' ======================================================================
- ' Starting Point
- ' ======================================================================
- '------------------------------------------------------------------------------------------
- ' 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: Very Simple Calculator
- ' Author: Joseph L. Bolen
- ' Date Created: Jul 2014
- '
- ' Description: A very simple calculator demonstration in Visual
- ' Basic .NET to emphasis the need to document one's
- ' program.
- '
- '-------------------------------------------------------------------------------------------
- ' Modification History:
- '
- ' Date By Description
- ' 07/02/2014 jlb1 Cosmetic fix to output. ToString("N3")
- '-------------------------------------------------------------------------------------------
- Option Strict On
- Public Class MainForm
- ' Add first number to second number and show the sum.
- Private Sub AddButton_Click(sender As Object, e As EventArgs) _
- Handles AddButton.Click
- ' Declare variables.
- Dim firstNumber As Double
- Dim secondNumber As Double
- Dim sum As Double
- ' Convert text/string to a numeric data type.
- Double.TryParse(FirstNumberTextBox.Text, firstNumber)
- Double.TryParse(SecondNumberTextBox.Text, secondNumber)
- ' Process / Calculate sum.
- sum = firstNumber + secondNumber
- ' Convert number to string and output it to a label.
- 'ResultValueLabel.Text = CStr(sum) ' Visual Basic
- 'ResultValueLabel.Text = String.Format("{0:N3}", sum) ' Visual Basic
- ResultValueLabel.Text = sum.ToString("N3") ' .NET Framework - jlb1
- ' 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
- ' Declare variables.
- Dim firstNumber As Double
- Dim secondNumber As Double
- Dim difference As Double
- ' Convert text/string to a numeric data type.
- Double.TryParse(FirstNumberTextBox.Text, firstNumber)
- Double.TryParse(SecondNumberTextBox.Text, secondNumber)
- ' Process / Calculate difference.
- difference = firstNumber - secondNumber
- ' Convert number to string and output it to a label.
- ResultValueLabel.Text = difference.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
- ' Declare variables.
- Dim firstNumber As Double
- Dim secondNumber As Double
- Dim product As Double
- ' Convert text/string to a numeric data type.
- Double.TryParse(FirstNumberTextBox.Text, firstNumber)
- Double.TryParse(SecondNumberTextBox.Text, secondNumber)
- ' Process / Calculate product.
- product = firstNumber * secondNumber
- ' Convert number to string and output it to a label.
- ResultValueLabel.Text = product.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
- ' Declare variables.
- Dim firstNumber As Double
- Dim secondNumber As Double
- Dim quotient As Double
- ' Convert text/string to a numeric data type.
- Double.TryParse(FirstNumberTextBox.Text, firstNumber)
- Double.TryParse(SecondNumberTextBox.Text, secondNumber)
- ' Process / Calculate quotient.
- quotient = firstNumber / secondNumber
- ' Convert number to string and output it to a label.
- ResultValueLabel.Text = quotient.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()
- ' Position cursor in first textbox.
- FirstNumberTextBox.Focus()
- FirstNumberTextBox.SelectAll()
- End Sub
- End Class
- ' ======================================================================
- ' MessageBox and ErrorProvider code.
- ' ======================================================================
- ' Convert text/string to a numeric data type.
- Double.TryParse(FirstNumberTextBox.Text, firstNumber)
- Double.TryParse(SecondNumberTextBox.Text, secondNumber)
- '--------------------------------------------------------------------------
- ' Validate and convert text/string to a numeric data type with MessageBox.
- If Not Double.TryParse(FirstNumberTextBox.Text, firstNumber) Then
- MessageBox.Show("Enter only a numeric value.",
- "Input Error",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- ' Place cursor in first input textbox.
- FirstNumberTextBox.Focus()
- FirstNumberTextBox.SelectAll()
- Exit Sub
- End If
- If Not Double.TryParse(SecondNumberTextBox.Text, secondNumber) Then
- MessageBox.Show("Enter only a numeric value.",
- "Input Error",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- ' Place cursor in second input textbox.
- SecondNumberTextBox.Focus()
- SecondNumberTextBox.SelectAll()
- Exit Sub
- End If
- '----------------------------------------------------------------------------
- ' Validate and convert text/string to a numeric data type w. ErrorProvider.
- If Not Double.TryParse(FirstNumberTextBox.Text, firstNumber) Then
- ErrorProvider1.SetError(FirstNumberTextBox, "Enter only a numeric value.")
- ' Place cursor in the first textbox and highlight the data.
- FirstNumberTextBox.Focus()
- FirstNumberTextBox.SelectAll()
- Exit Sub
- Else
- ErrorProvider1.SetError(FirstNumberTextBox, String.Empty)
- End If
- If Not Double.TryParse(SecondNumberTextBox.Text, secondNumber) Then
- ErrorProvider1.SetError(SecondNumberTextBox, "Enter only a numeric value.")
- ' Place cursor in the second textbox and highlight the data.
- SecondNumberTextBox.Focus()
- SecondNumberTextBox.SelectAll()
- Exit Sub
- Else
- ErrorProvider1.SetError(SecondNumberTextBox, String.Empty)
- End If
- ' ======================================================================
- ' Data Validation with the Validating Event
- ' ======================================================================
- 'Form at Design Time:
- me.AutoValidate = Windows.Forms.AutoValidate.EnableAllowFocusChange
- 'Processing Procedure:
- If Not ValidateChildren() Then
- My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
- Exit Sub
- End If
- 'Validating Event for the TextBoxes:
- ' Validate FirstNumberTextBox input.
- Private Sub FirstNumberTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
- Handles FirstNumberTextBox.Validating
- If Not IsNumeric(FirstNumberTextBox.Text) Then
- ErrorProvider1.SetError(FirstNumberTextBox, "Enter only a numeric value.")
- e.Cancel = True
- 'ElseIf
- Else
- ErrorProvider1.SetError(FirstNumberTextBox, String.Empty)
- End If
- End Sub
- ' Validate SecondNumberTextBox input.
- Private Sub SecondNumberTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
- Handles SecondNumberTextBox.Validating
- If Not IsNumeric(SecondNumberTextBox.Text) Then
- ErrorProvider1.SetError(SecondNumberTextBox, "Enter only a numeric value.")
- e.Cancel = True
- 'ElseIf
- Else
- ErrorProvider1.SetError(SecondNumberTextBox, String.Empty)
- End If
- End Sub
- 'Form Closing Event:
- ' 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
- ' ======================================================================
Advertisement
Add Comment
Please, Sign In to add comment