none
Basic Calculator Percentage function RRS feed

  • Question

  • I've got to create a basic calculator for my programming coursework, and I'm struggling to work out the function of a Percentage button on a calculator. I followed this video to build the basic functions: https://www.youtube.com/watch?v=RhCzj5q6uqA

    But I need to create an additional feature.

    Any advice would be really appreciated as I can't seem to work it out myself

    I'm using the following code: 

    Public Class Form1
    
        Dim Firstnum As Decimal
        Dim Secondnum As Decimal
        Dim operations As Integer
        Dim Operator_selector As Boolean = False
    
    
    
        Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "7"
            Else
                TextBox1.Text += "7"
            End If
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "1"
            Else
                TextBox1.Text += "1"
            End If
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "2"
            Else
                TextBox1.Text += "2"
            End If
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "3"
            Else
                TextBox1.Text += "3"
            End If
        End Sub
    
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "4"
            Else
                TextBox1.Text += "4"
            End If
        End Sub
    
        Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "5"
            Else
                TextBox1.Text += "5"
            End If
        End Sub
    
        Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "6"
            Else
                TextBox1.Text += "6"
            End If
        End Sub
    
        Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "8"
            Else
                TextBox1.Text += "8"
            End If
        End Sub
    
        Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "9"
            Else
                TextBox1.Text += "9"
            End If
        End Sub
    
        Private Sub Button0_Click(sender As Object, e As EventArgs) Handles Button0.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "0"
            End If
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    
        End Sub
    
        Private Sub ButtonClear_Click(sender As Object, e As EventArgs) Handles ButtonClear.Click
            TextBox1.Text = ""
        End Sub
    
        Private Sub ButtonDecimal_Click(sender As Object, e As EventArgs) Handles ButtonDecimal.Click
            If Not (TextBox1.Text.Contains(".")) Then
                TextBox1.Text += "."
            End If
        End Sub
    
        Private Sub ButtonEqual_Click(sender As Object, e As EventArgs) Handles ButtonEqual.Click
            If Operator_selector = True Then
                Secondnum = TextBox1.Text
                If operations = 1 Then
                    TextBox1.Text = Firstnum + Secondnum
                ElseIf operations = 2 Then
                    TextBox1.Text = Firstnum - Secondnum
                ElseIf operations = 3 Then
                    TextBox1.Text = Firstnum * Secondnum
                Else
                    If Secondnum = 0 Then
                        TextBox1.Text = "Error!"
                    Else
                        TextBox1.Text = Firstnum / Secondnum
                    End If
                End If
    
                Operator_selector = False
            End If
        End Sub
    
        Private Sub ButtonPercent_Click(sender As Object, e As EventArgs) Handles ButtonPercent.Click
            
        End Sub
    
        Private Sub ButtonDivide_Click(sender As Object, e As EventArgs) Handles ButtonDivide.Click
            Firstnum = TextBox1.Text
            TextBox1.Text = ""
            Operator_selector = True
            operations = 4 ' = /
        End Sub
    
        Private Sub ButtonMultiply_Click(sender As Object, e As EventArgs) Handles ButtonMultiply.Click
            Firstnum = TextBox1.Text
            TextBox1.Text = ""
            Operator_selector = True
            operations = 3 ' = *
        End Sub
    
        Private Sub ButtonMinus_Click(sender As Object, e As EventArgs) Handles ButtonMinus.Click
            Firstnum = TextBox1.Text
            TextBox1.Text = ""
            Operator_selector = True
            operations = 2 ' = -
        End Sub
    
        Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
            Firstnum = TextBox1.Text
            TextBox1.Text = "0"
            Operator_selector = True
            operations = 1 ' = +
        End Sub
    
        Private Sub PictureBox3_Click(sender As Object, e As EventArgs) Handles PictureBox3.Click
    
        End Sub
    End Class
    


    • Edited by C_Marriott Friday, December 15, 2017 6:43 PM
    Friday, December 15, 2017 6:02 PM

All replies

  • Please edit your post and use the code button for your code.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, December 15, 2017 6:06 PM
    Moderator
  • You should break it down into smaller pieces. Get each piece working and understood. Then add them together.

    For example what exactly is this code of yours supposed to do?

            If TextBox1.Text <> "0" Then
                TextBox1.Text += "1"
            Else
                TextBox1.Text += "1"
            End If
    

    Friday, December 15, 2017 6:57 PM
  • But I need to create an additional feature.

    There are three steps in that change: Create the percentage button; Add the code for the button click event; Add the code to do the calculation.   The actual calculation is the last part of that third step, so there is no need to worry about it at the moment.  You should indicate where in that process you have got to, and what is stopping you from moving forward.

    Friday, December 15, 2017 9:10 PM
  • But I need to create an additional feature.

    Any advice would be really appreciated as I can't seem to work it out myself

    Hi C_Marriott,

    You want to add some features, what kinds of feature? Do you want to do this:

    You can take a look the following code:

     'variables to hold operands
        Private valHolder1 As Double
        Private valHolder2 As Double
        'Varible to hold temporary values
        Private tmpValue As Double
        'variable for Memory storage
        Private Memory As Double
        'True if "." is use else false
        Private hasDecimal As Boolean
        Private inputStatus As Boolean
        Private clearText As Boolean
        'variable to hold Operater
        Private calcFunc As String
    
    #Region "Number Buttons "
        Private Sub cmd9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd9.Click
            If inputStatus = False Then
                txtInput.Text += cmd9.Text
            Else
                txtInput.Text = cmd9.Text
                inputStatus = False
            End If
        End Sub
    
        Private Sub cmd8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd8.Click
            If inputStatus = False Then
                txtInput.Text += cmd8.Text
            Else
                txtInput.Text = cmd8.Text
                inputStatus = False
            End If
        End Sub
    
        Private Sub cmd7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd7.Click
            If inputStatus = False Then
                txtInput.Text += cmd7.Text
            Else
                txtInput.Text = cmd7.Text
                inputStatus = False
            End If
        End Sub
    
        Private Sub cmd6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd6.Click
            If inputStatus = False Then
                txtInput.Text += cmd6.Text
            Else
                txtInput.Text = cmd6.Text
                inputStatus = False
            End If
        End Sub
    
        Private Sub cmd5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd5.Click
            If inputStatus = False Then
                txtInput.Text += cmd5.Text
            Else
                txtInput.Text = cmd5.Text
                inputStatus = False
            End If
        End Sub
    
        Private Sub cmd4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd4.Click
            If inputStatus = False Then
                txtInput.Text += cmd4.Text
            Else
                txtInput.Text = cmd4.Text
                inputStatus = False
            End If
        End Sub
    
        Private Sub cmd3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3.Click
            If inputStatus = False Then
                txtInput.Text += cmd3.Text
            Else
                txtInput.Text = cmd3.Text
                inputStatus = False
            End If
        End Sub
    
        Private Sub cmd2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd2.Click
            If inputStatus = False Then
                txtInput.Text += cmd2.Text
            Else
                txtInput.Text = cmd2.Text
                inputStatus = False
            End If
        End Sub
    
        Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click
            If inputStatus = False Then
                txtInput.Text += cmd1.Text
            Else
                txtInput.Text = cmd1.Text
                inputStatus = False
            End If
        End Sub
    
        Private Sub cmd0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd0.Click
            If inputStatus = False Then
                If txtInput.Text.Length >= 1 Then
                    txtInput.Text += cmd0.Text
                End If
            End If
        End Sub
    #End Region
    
    
    #Region " Calculation Buttons "
        Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
            If txtInput.Text.Length <> 0 Then
                If calcFunc = String.Empty Then
                    valHolder1 = CDbl(txtInput.Text)
                    txtInput.Text = String.Empty
                Else
                    CalculateTotals()
                End If
                calcFunc = "Add"
                hasDecimal = False
            End If
        End Sub
    
        Private Sub cmdSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubtract.Click
            If txtInput.Text.Length <> 0 Then
                If calcFunc = String.Empty Then
                    valHolder1 = CDbl(txtInput.Text)
                    txtInput.Text = String.Empty
                Else
                    CalculateTotals()
                End If
                calcFunc = "Subtract"
                hasDecimal = False
            End If
        End Sub
    
        Private Sub cmdDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDivide.Click
            If txtInput.Text.Length <> 0 Then
                If calcFunc = String.Empty Then
                    valHolder1 = CDbl(txtInput.Text)
                    txtInput.Text = String.Empty
                Else
                    CalculateTotals()
                End If
                calcFunc = "Divide"
                hasDecimal = False
            End If
        End Sub
    
        Private Sub cmdMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMultiply.Click
            If txtInput.Text.Length <> 0 Then
                If calcFunc = String.Empty Then
                    valHolder1 = CDbl(txtInput.Text)
                    txtInput.Text = String.Empty
                Else
                    CalculateTotals()
                End If
                calcFunc = "Multiply"
                hasDecimal = False
            End If
        End Sub
    
        Private Sub cmdPowerOf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPowerOf.Click
            If txtInput.Text.Length <> 0 Then
                If calcFunc = String.Empty Then
                    valHolder1 = CDbl(txtInput.Text)
                    txtInput.Text = String.Empty
                Else
                    CalculateTotals()
                End If
                calcFunc = "PowerOf"
                hasDecimal = False
            End If
        End Sub
    
        Private Sub cmdSqrRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSqrRoot.Click
            If txtInput.Text.Length <> 0 Then
                tmpValue = CDbl(txtInput.Text)
                tmpValue = System.Math.Sqrt(tmpValue)
                txtInput.Text = CStr(tmpValue)
                hasDecimal = False
            End If
        End Sub
    
        Private Sub cmdEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEqual.Click
            If txtInput.Text.Length <> 0 AndAlso valHolder1 <> 0 Then
                CalculateTotals()
                calcFunc = ""
                hasDecimal = False
            End If
        End Sub
    
        Private Sub cmdInverse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInverse.Click
            If txtInput.Text.Length <> 0 Then
                tmpValue = CDbl(txtInput.Text)
                tmpValue = 1 / tmpValue
                txtInput.Text = CStr(tmpValue)
                hasDecimal = False
            End If
        End Sub
    
        Private Sub cmdSign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSign.Click
            If inputStatus = False Then
                If txtInput.Text.Length > 0 Then
                    valHolder1 = -1 * CDbl(txtInput.Text)
                    txtInput.Text = CStr(valHolder1)
                End If
            End If
        End Sub
    #End Region
    
    #Region " Other Buttons "
        Private Sub cmdClearEntry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClearEntry.Click
            txtInput.Text = String.Empty
            hasDecimal = False
        End Sub
    
        Private Sub cmdClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClearAll.Click
            txtInput.Text = String.Empty
            valHolder1 = 0
            valHolder2 = 0
            calcFunc = String.Empty
            hasDecimal = False
        End Sub
    
        Private Sub cmdBackspace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBackspace.Click
            Dim str As String
            Dim loc As Integer
            If txtInput.Text.Length > 0 Then
                str = txtInput.Text.Chars(txtInput.Text.Length - 1)
                If str = "." Then
                    hasDecimal = False
                End If
                loc = txtInput.Text.Length
                txtInput.Text = txtInput.Text.Remove(loc - 1, 1)
            End If
        End Sub
    
        Private Sub cmdDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDecimal.Click
            'Check for input status (we want flase)
            If Not inputStatus Then
                'Check if it already has a decimal (if it does then do nothing)
                If Not hasDecimal Then
                    'Check to make sure the length is > than 1
                    'Dont want user to add decimal as first character
                    If txtInput.Text.Length >= 1 Then
                        'Make sure 0 isnt the first number
                        If Not txtInput.Text = "0" Then
                            'It met all our requirements so add the zero
                            txtInput.Text += cmdDecimal.Text
                            'Toggle the flag to true (only 1 decimal per calculation)
                            hasDecimal = True
                        End If
                    Else
                        txtInput.Text = "0."
                    End If
                End If
            End If
        End Sub
    #End Region
    
    #Region " Helpers "
        Private Sub CalculateTotals()
            valHolder2 = CDbl(txtInput.Text)
            Select Case calcFunc
                Case "Add"
                    valHolder1 = valHolder1 + valHolder2
                Case "Subtract"
                    valHolder1 = valHolder1 - valHolder2
                Case "Divide"
                    valHolder1 = valHolder1 / valHolder2
                Case "Multiply"
                    valHolder1 = valHolder1 * valHolder2
                Case "PowerOf"
                    valHolder1 = System.Math.Pow(valHolder1, valHolder2)
            End Select
            txtInput.Text = CStr(valHolder1)
            inputStatus = True
        End Sub
    #End Region

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, December 18, 2017 6:45 AM
    Moderator
  • Cherry,

    Are you doing the same coursework as the OP student and are you both cheating.

    I hope your teacher sees this and will handle this as all teacher do. 


    Success
    Cor

    Monday, December 18, 2017 7:51 AM