JayBeeOH

Very Simple Calculator

Jul 31st, 2014
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 19.51 KB | None | 0 0
  1. 'Very Simple Calculator - Documentation:
  2. '   YouTube video tutorial at   -- http://www.youtube.com/user/bolenpresents
  3. '   Form's screen capture is at -- http://imgur.com/m3H4SHx
  4. '   Visual Basic .Net codes is at   -- http://pastebin.com/PcJ9nRdZ
  5. '------------------------------------------------------------------------------------------
  6. '           Notice of My Copyright and Intellectual Property Rights
  7. '
  8. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  9. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  10. ' publish or provide such intellectual property to any other person or entity for any
  11. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  12. '
  13. '                 Copyright © 2014. All rights reserved.
  14. '        All trademarks remain the property of their respective owners.
  15. '-------------------------------------------------------------------------------------------
  16. ' Program Name:   Very Simple Calculator
  17. ' Author:         Joseph L. Bolen
  18. ' Date Created:   Jul 2014
  19. '
  20. ' Description:    A very simple calculator demonstration in Visual
  21. '                 Basic .NET to emphasis the need to document one's
  22. '                 program.
  23. '
  24. '-------------------------------------------------------------------------------------------
  25. ' Modification History:
  26. '
  27. ' Date          By      Description
  28. ' 07/02/2014    jlb1    Cosmetic fix to output. ToString("N3")
  29. ' 07/29/2014    jlb2    Data validation with ErrorProvider & Clear on Input TextChanged.
  30. ' 07/31/2014    jlb3    Data validation using the validating event. Also, form's AutoValidate
  31. '                       property changed to EnableAllowFocusChange.
  32. '-------------------------------------------------------------------------------------------
  33.  
  34. Option Strict On
  35.  
  36. Public Class MainForm
  37.  
  38.     ' Add first number to second number and show the sum.
  39.     Private Sub AddButton_Click(sender As Object, e As EventArgs) _
  40.         Handles AddButton.Click
  41.  
  42.         ' Declare variables.
  43.         Dim firstNumber As Double
  44.         Dim secondNumber As Double
  45.         Dim sum As Double
  46.  
  47.         ' Validate input - jlb3.
  48.         If Not ValidateChildren() Then
  49.             My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
  50.             Exit Sub
  51.         End If
  52.  
  53.         ' Convert text/string to a numeric data type.
  54.         firstNumber = CDbl(FirstNumberTextBox.Text)
  55.         secondNumber = CDbl(SecondNumberTextBox.Text)
  56.  
  57.         ' Process / Calculate sum.
  58.         sum = firstNumber + secondNumber
  59.  
  60.         ' Convert number to string and output it to a label.
  61.         'ResultValueLabel.Text = CStr(sum)                       ' Visual Basic
  62.         'ResultValueLabel.Text = String.Format("{0:N3}", sum)    ' Visual Basic
  63.         ResultValueLabel.Text = sum.ToString("N3")             ' .NET Framework - JLB1
  64.  
  65.         ' Place cursor in first input textbox.
  66.         FirstNumberTextBox.Focus()
  67.         FirstNumberTextBox.SelectAll()
  68.     End Sub
  69.  
  70.     ' Subtract the second number from first number and show the difference.
  71.     Private Sub SubtractButton_Click(sender As Object, e As EventArgs) _
  72.         Handles SubtractButton.Click
  73.  
  74.         ' Declare variables.
  75.         Dim firstNumber As Double
  76.         Dim secondNumber As Double
  77.         Dim difference As Double
  78.  
  79.         ' Validate input - jlb3.
  80.         If Not ValidateChildren() Then
  81.             My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
  82.             Exit Sub
  83.         End If
  84.  
  85.         ' Convert text/string to a numeric data type.
  86.         firstNumber = CDbl(FirstNumberTextBox.Text)
  87.         secondNumber = CDbl(SecondNumberTextBox.Text)
  88.  
  89.         ' Process / Calculate difference.
  90.         difference = firstNumber - secondNumber
  91.  
  92.         ' Convert number to string and output it to a label - jlb1.
  93.         ResultValueLabel.Text = difference.ToString("N3")
  94.  
  95.         ' Place cursor in first input textbox.
  96.         FirstNumberTextBox.Focus()
  97.         FirstNumberTextBox.SelectAll()
  98.     End Sub
  99.  
  100.     ' Multiply the first number by second number and show the product.
  101.     Private Sub MultiplyButton_Click(sender As Object, e As EventArgs) _
  102.         Handles MultiplyButton.Click
  103.  
  104.         ' Declare variables.
  105.         Dim firstNumber As Double
  106.         Dim secondNumber As Double
  107.         Dim product As Double
  108.  
  109.         ' Validate input - jlb3.
  110.         If Not ValidateChildren() Then
  111.             My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
  112.             Exit Sub
  113.         End If
  114.  
  115.         ' Convert text/string to a numeric data type.
  116.         firstNumber = CDbl(FirstNumberTextBox.Text)
  117.         secondNumber = CDbl(SecondNumberTextBox.Text)
  118.  
  119.         ' Process / Calculate product.
  120.         product = firstNumber * secondNumber
  121.  
  122.         ' Convert number to string and output it to a label - jlb1.
  123.         ResultValueLabel.Text = product.ToString("N3")
  124.  
  125.         ' Place cursor in first input textbox.
  126.         FirstNumberTextBox.Focus()
  127.         FirstNumberTextBox.SelectAll()
  128.     End Sub
  129.  
  130.     ' Divide the first number by the second number and show the quotient.
  131.     Private Sub DivideButton_Click(sender As Object, e As EventArgs) _
  132.         Handles DivideButton.Click
  133.  
  134.         ' Declare variables.
  135.         Dim firstNumber As Double
  136.         Dim secondNumber As Double
  137.         Dim quotient As Double
  138.  
  139.         ' Validate input - jlb3.
  140.         If Not ValidateChildren() Then
  141.             My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
  142.             Exit Sub
  143.         End If
  144.  
  145.         ' Convert text/string to a numeric data type.
  146.         firstNumber = CDbl(FirstNumberTextBox.Text)
  147.         secondNumber = CDbl(SecondNumberTextBox.Text)
  148.  
  149.         ' Process / Calculate quotient.
  150.         quotient = firstNumber / secondNumber
  151.  
  152.         ' Convert number to string and output it to a label - jlb1.
  153.         ResultValueLabel.Text = quotient.ToString("N3")
  154.  
  155.         ' Place cursor in first input textbox.
  156.         FirstNumberTextBox.Focus()
  157.         FirstNumberTextBox.SelectAll()
  158.     End Sub
  159.  
  160.     ' Clear the output controls and place cursor in first textbox.
  161.     Private Sub ClearButton_Click(sender As Object, e As EventArgs) _
  162.         Handles ClearButton.Click
  163.  
  164.         ' Clear output controls.
  165.         ResultValueLabel.Text = String.Empty
  166.         FirstNumberTextBox.Clear()
  167.         SecondNumberTextBox.Clear()
  168.  
  169.         ' Clear ErrorProvider
  170.         ErrorProvider1.Clear()
  171.  
  172.         ' Position cursor in first textbox.
  173.         FirstNumberTextBox.Focus()
  174.         FirstNumberTextBox.SelectAll()
  175.     End Sub
  176.  
  177.     ' Clear ResultValueLabel when either first or second
  178.     ' textbox value changes.
  179.     Private Sub ClearResult(sender As Object, e As EventArgs) _
  180.         Handles FirstNumberTextBox.TextChanged,
  181.                 SecondNumberTextBox.TextChanged
  182.  
  183.         ResultValueLabel.Text = String.Empty
  184.     End Sub
  185.  
  186.     ' Validate FirstNumberTextBox input - jlb3.
  187.     Private Sub FirstNumberTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
  188.         Handles FirstNumberTextBox.Validating
  189.  
  190.         If Not IsNumeric(FirstNumberTextBox.Text) Then
  191.             ErrorProvider1.SetError(FirstNumberTextBox, "Enter only a numeric value.")
  192.             e.Cancel = True
  193.             'ElseIf
  194.         Else
  195.             ErrorProvider1.SetError(FirstNumberTextBox, String.Empty)
  196.         End If
  197.  
  198.     End Sub
  199.  
  200.     ' Validate SecondNumberTextBox input - jlb3.
  201.     Private Sub SecondNumberTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
  202.         Handles SecondNumberTextBox.Validating
  203.  
  204.         If Not IsNumeric(SecondNumberTextBox.Text) Then
  205.             ErrorProvider1.SetError(SecondNumberTextBox, "Enter only a numeric value.")
  206.             e.Cancel = True
  207.             'ElseIf
  208.         Else
  209.             ErrorProvider1.SetError(SecondNumberTextBox, String.Empty)
  210.         End If
  211.     End Sub
  212.  
  213.     ' Allow form to close even if input errors exist - jlb3.
  214.     Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
  215.         Handles Me.FormClosing
  216.  
  217.         e.Cancel = False
  218.     End Sub
  219. End Class
  220.  
  221. ' ======================================================================
  222. ' Comments and Pseudocode
  223. ' ======================================================================
  224.  ' Add first number to second number and show the sum.
  225.         'TODO:  Declare variables.
  226.         'TODO:  Convert text/string to a numeric data type.
  227.         'TODO:  Process / Calculate sum.
  228.         'TODO:  Convert number to string and output it to a label.
  229.  
  230.        'ResultValueLabel.Text = CStr(sum)                       ' Visual Basic
  231.        'ResultValueLabel.Text = String.Format("{0:N3}", sum)    ' Visual Basic
  232.        'ResultValueLabel.Text = sum.ToString("N3")             ' .NET Framework
  233.  
  234.     ' Subtract the second number from first number and show the difference.
  235.         'TODO:  Declare variables.
  236.         'TODO:  Convert text/string to a numeric data type.
  237.         'TODO:  Process / Calculate difference.
  238.         'TODO:  Convert number to string and output it to a label.
  239.  
  240.     ' Multiply the first number by second number and show the product.
  241.         'TODO:  Declare variables.
  242.         'TODO:  Convert text/string to a numeric data type.
  243.         'TODO:  Process / Calculate product.
  244.         'TODO:  Convert number to string and output it to a label.
  245.  
  246.     ' Divide the first number by the second number and show the quotient.
  247.         'TODO:  Declare variables.
  248.         'TODO:  Convert text/string to a numeric data type.
  249.         'TODO:  Process / Calculate quotient.
  250.         'TODO:  Convert number to string and output it to a label.
  251.  
  252.     ' Clear the output controls and place cursor in first textbox.
  253.         'TODO:  Clear output controls.
  254.         'TODO:  Position cursor in first textbox.
  255. ' ======================================================================
  256. ' Starting Point
  257. ' ======================================================================
  258. '------------------------------------------------------------------------------------------
  259. '           Notice of My Copyright and Intellectual Property Rights
  260. '
  261. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  262. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  263. ' publish or provide such intellectual property to any other person or entity for any
  264. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  265. '
  266. '                 Copyright © 2014. All rights reserved.
  267. '        All trademarks remain the property of their respective owners.
  268. '-------------------------------------------------------------------------------------------
  269. ' Program Name:   Very Simple Calculator
  270. ' Author:         Joseph L. Bolen
  271. ' Date Created:   Jul 2014
  272. '
  273. ' Description:    A very simple calculator demonstration in Visual
  274. '                 Basic .NET to emphasis the need to document one's
  275. '                 program.
  276. '
  277. '-------------------------------------------------------------------------------------------
  278. ' Modification History:
  279. '
  280. ' Date          By      Description
  281. ' 07/02/2014    jlb1    Cosmetic fix to output. ToString("N3")
  282. '-------------------------------------------------------------------------------------------
  283.  
  284. Option Strict On
  285.  
  286. Public Class MainForm
  287.  
  288.     ' Add first number to second number and show the sum.
  289.     Private Sub AddButton_Click(sender As Object, e As EventArgs) _
  290.         Handles AddButton.Click
  291.  
  292.         ' Declare variables.
  293.         Dim firstNumber As Double
  294.         Dim secondNumber As Double
  295.         Dim sum As Double
  296.  
  297.         ' Convert text/string to a numeric data type.
  298.         Double.TryParse(FirstNumberTextBox.Text, firstNumber)
  299.         Double.TryParse(SecondNumberTextBox.Text, secondNumber)
  300.  
  301.         ' Process / Calculate sum.
  302.         sum = firstNumber + secondNumber
  303.  
  304.         ' Convert number to string and output it to a label.
  305.         'ResultValueLabel.Text = CStr(sum)                       ' Visual Basic
  306.         'ResultValueLabel.Text = String.Format("{0:N3}", sum)    ' Visual Basic
  307.         ResultValueLabel.Text = sum.ToString("N3")             ' .NET Framework - jlb1
  308.  
  309.         ' Place cursor in first input textbox.
  310.         FirstNumberTextBox.Focus()
  311.         FirstNumberTextBox.SelectAll()
  312.     End Sub
  313.  
  314.     ' Subtract the second number from first number and show the difference.
  315.     Private Sub SubtractButton_Click(sender As Object, e As EventArgs) _
  316.         Handles SubtractButton.Click
  317.  
  318.         ' Declare variables.
  319.         Dim firstNumber As Double
  320.         Dim secondNumber As Double
  321.         Dim difference As Double
  322.  
  323.         ' Convert text/string to a numeric data type.
  324.         Double.TryParse(FirstNumberTextBox.Text, firstNumber)
  325.         Double.TryParse(SecondNumberTextBox.Text, secondNumber)
  326.  
  327.         ' Process / Calculate difference.
  328.         difference = firstNumber - secondNumber
  329.  
  330.         ' Convert number to string and output it to a label.
  331.         ResultValueLabel.Text = difference.ToString("N3")
  332.  
  333.         ' Place cursor in first input textbox.
  334.         FirstNumberTextBox.Focus()
  335.         FirstNumberTextBox.SelectAll()
  336.     End Sub
  337.  
  338.     ' Multiply the first number by second number and show the product.
  339.     Private Sub MultiplyButton_Click(sender As Object, e As EventArgs) _
  340.         Handles MultiplyButton.Click
  341.  
  342.         ' Declare variables.
  343.         Dim firstNumber As Double
  344.         Dim secondNumber As Double
  345.         Dim product As Double
  346.  
  347.         ' Convert text/string to a numeric data type.
  348.         Double.TryParse(FirstNumberTextBox.Text, firstNumber)
  349.         Double.TryParse(SecondNumberTextBox.Text, secondNumber)
  350.  
  351.         ' Process / Calculate product.
  352.         product = firstNumber * secondNumber
  353.  
  354.         ' Convert number to string and output it to a label.
  355.         ResultValueLabel.Text = product.ToString("N3")
  356.  
  357.         ' Place cursor in first input textbox.
  358.         FirstNumberTextBox.Focus()
  359.         FirstNumberTextBox.SelectAll()
  360.     End Sub
  361.  
  362.     ' Divide the first number by the second number and show the quotient.
  363.     Private Sub DivideButton_Click(sender As Object, e As EventArgs) _
  364.         Handles DivideButton.Click
  365.  
  366.         ' Declare variables.
  367.         Dim firstNumber As Double
  368.         Dim secondNumber As Double
  369.         Dim quotient As Double
  370.  
  371.         ' Convert text/string to a numeric data type.
  372.         Double.TryParse(FirstNumberTextBox.Text, firstNumber)
  373.         Double.TryParse(SecondNumberTextBox.Text, secondNumber)
  374.  
  375.         ' Process / Calculate quotient.
  376.         quotient = firstNumber / secondNumber
  377.  
  378.         ' Convert number to string and output it to a label.
  379.         ResultValueLabel.Text = quotient.ToString("N3")
  380.  
  381.         ' Place cursor in first input textbox.
  382.         FirstNumberTextBox.Focus()
  383.         FirstNumberTextBox.SelectAll()
  384.     End Sub
  385.  
  386.     ' Clear the output controls and place cursor in first textbox.
  387.     Private Sub ClearButton_Click(sender As Object, e As EventArgs) _
  388.         Handles ClearButton.Click
  389.  
  390.         ' Clear output controls.
  391.         ResultValueLabel.Text = String.Empty
  392.         FirstNumberTextBox.Clear()
  393.         SecondNumberTextBox.Clear()
  394.  
  395.         ' Position cursor in first textbox.
  396.         FirstNumberTextBox.Focus()
  397.         FirstNumberTextBox.SelectAll()
  398.     End Sub
  399. End Class
  400.  
  401. ' ======================================================================
  402. ' MessageBox and ErrorProvider code.
  403. ' ======================================================================
  404. ' Convert text/string to a numeric data type.
  405.         Double.TryParse(FirstNumberTextBox.Text, firstNumber)
  406.         Double.TryParse(SecondNumberTextBox.Text, secondNumber)
  407.  
  408. '--------------------------------------------------------------------------
  409. ' Validate and convert text/string to a numeric data type with MessageBox.
  410.         If Not Double.TryParse(FirstNumberTextBox.Text, firstNumber) Then
  411.             MessageBox.Show("Enter only a numeric value.",
  412.                             "Input Error",
  413.                             MessageBoxButtons.OK,
  414.                             MessageBoxIcon.Error)
  415.  
  416.             ' Place cursor in first input textbox.
  417.             FirstNumberTextBox.Focus()
  418.             FirstNumberTextBox.SelectAll()
  419.             Exit Sub
  420.         End If
  421.  
  422.         If Not Double.TryParse(SecondNumberTextBox.Text, secondNumber) Then
  423.             MessageBox.Show("Enter only a numeric value.",
  424.                             "Input Error",
  425.                             MessageBoxButtons.OK,
  426.                             MessageBoxIcon.Error)
  427.  
  428.             ' Place cursor in second input textbox.
  429.             SecondNumberTextBox.Focus()
  430.             SecondNumberTextBox.SelectAll()
  431.             Exit Sub
  432.         End If
  433. '----------------------------------------------------------------------------
  434.  
  435. ' Validate and convert text/string to a numeric data type w. ErrorProvider.
  436.         If Not Double.TryParse(FirstNumberTextBox.Text, firstNumber) Then
  437.             ErrorProvider1.SetError(FirstNumberTextBox, "Enter only a numeric value.")
  438.             ' Place cursor in the first textbox and highlight the data.
  439.             FirstNumberTextBox.Focus()
  440.             FirstNumberTextBox.SelectAll()
  441.             Exit Sub
  442.         Else
  443.             ErrorProvider1.SetError(FirstNumberTextBox, String.Empty)
  444.         End If
  445.  
  446.         If Not Double.TryParse(SecondNumberTextBox.Text, secondNumber) Then
  447.             ErrorProvider1.SetError(SecondNumberTextBox, "Enter only a numeric value.")
  448.  
  449.             ' Place cursor in the second textbox and highlight the data.
  450.             SecondNumberTextBox.Focus()
  451.             SecondNumberTextBox.SelectAll()
  452.             Exit Sub
  453.         Else
  454.             ErrorProvider1.SetError(SecondNumberTextBox, String.Empty)
  455.         End If
  456.  
  457. ' ======================================================================
  458. ' Data Validation with the Validating Event
  459. ' ======================================================================
  460.  
  461. 'Form at Design Time:
  462.  
  463.  me.AutoValidate =  Windows.Forms.AutoValidate.EnableAllowFocusChange
  464.  
  465.  
  466. 'Processing Procedure:
  467.  
  468.         If Not ValidateChildren() Then
  469.             My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
  470.             Exit Sub
  471.         End If
  472.    
  473.  
  474. 'Validating Event for the TextBoxes:
  475.  
  476.     ' Validate FirstNumberTextBox input.
  477.     Private Sub FirstNumberTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
  478.         Handles FirstNumberTextBox.Validating
  479.  
  480.         If Not IsNumeric(FirstNumberTextBox.Text) Then
  481.             ErrorProvider1.SetError(FirstNumberTextBox, "Enter only a numeric value.")
  482.             e.Cancel = True
  483.             'ElseIf
  484.         Else
  485.             ErrorProvider1.SetError(FirstNumberTextBox, String.Empty)
  486.         End If
  487.  
  488.     End Sub
  489.  
  490.     ' Validate SecondNumberTextBox input.
  491.     Private Sub SecondNumberTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
  492.         Handles SecondNumberTextBox.Validating
  493.  
  494.         If Not IsNumeric(SecondNumberTextBox.Text) Then
  495.             ErrorProvider1.SetError(SecondNumberTextBox, "Enter only a numeric value.")
  496.             e.Cancel = True
  497.             'ElseIf
  498.         Else
  499.             ErrorProvider1.SetError(SecondNumberTextBox, String.Empty)
  500.         End If
  501.     End Sub
  502.  
  503.  
  504. 'Form Closing Event:
  505.  
  506.     ' Allow form to close even if input errors exist.
  507.     Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
  508.         Handles Me.FormClosing
  509.  
  510.         e.Cancel = False
  511.     End Sub
  512.  
  513. ' ======================================================================
Advertisement
Add Comment
Please, Sign In to add comment