hi all :)

I am now working on a calculator that is mimic to windows calculator.

I have the whole thing done except for the percent button. Have looked everywhere and cannot seem to find out how to do it!

can someone help me here??

Here is a peek at some of what I have so far:

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
        If Status = False Then
            TextBox1.Text = TextBox1.Text + CStr(1)
        Else
            TextBox1.Text = 1
            Status = False
        End If
    End Sub

    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
        If Status = False Then
            TextBox1.Text = TextBox1.Text + CStr(2)
        Else
            TextBox1.Text = 2
            Status = False
        End If
    End Sub

    Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
        If Status = False Then
            TextBox1.Text = TextBox1.Text + CStr(3)
        Else
            TextBox1.Text = 3
            Status = False
        End If
    End Sub

    Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
        If Status = False Then
            If TextBox1.Text.Length > 0 Then
                TextBox1.Text = TextBox1.Text + CStr(0)
            End If
        End If
    End Sub

    Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click
        If Status = False Then
            If TextBox1.Text.Length > 0 Then
                TextBox1.Text = TextBox1.Text + CStr(0)
            End If
        End If
    End Sub

    Private Sub btnSqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSqrt.Click
        If TextBox1.Text.Length <> 0 Then
            Temp = CDbl(TextBox1.Text)
            Temp = System.Math.Sqrt(Temp)
            TextBox1.Text = CStr(Temp)
            period = False
        End If
    End Sub

    Private Sub btnMR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMR.Click
        If btnMemStatus.Text = "M" Then
            TextBox1.Text = CStr(Memory)
            Status = True
        End If
    End Sub

    Private Sub btnC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click
        TextBox1.Text = ""
        Var1 = 0
        var2 = 0
        [Operator] = ""
        period = False
    End Sub

    Private Sub btnCE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCE.Click
        TextBox1.Text = ""
        period = False
    End Sub

    Private Sub btnMC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMC.Click
        Memory = 0
        TextBox1.Text = Nothing

    End Sub

    Private Sub btnMS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMS.Click
        If TextBox1.Text.Length > 0 Then
            Memory = Memory - CDbl(TextBox1.Text)
            btnMemStatus.Text = "M"
        End If
    End Sub

    Private Sub btnMPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMPlus.Click
        If TextBox1.Text.Length > 0 Then
            Memory = Memory + CDbl(TextBox1.Text)
            btnMemStatus.Text = "M"
        End If
    End Sub

    Private Sub btnPlusMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlusMinus.Click
        If Status = False Then
            If TextBox1.Text.Length > 0 Then
                Var1 = -1 * CDbl(TextBox1.Text)
                TextBox1.Text = CStr(Var1)
            End If
        End If
    End Sub

    Private Sub btnAddition_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddition.Click
        If TextBox1.Text.Length <> 0 Then
            If [Operator] = "" Then
                Var1 = CDbl(TextBox1.Text)
                TextBox1.Text = ""
            Else
                Calculate()
            End If
            [Operator] = "Add"
            period = False
        End If
    End Sub

    Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
        If TextBox1.Text.Length <> 0 AndAlso Var1 <> 0 Then
            Calculate()
            [Operator] = ""
            period = False
        End If
    End Sub

    Private Sub btnOneTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOneTime.Click
        If TextBox1.Text.Length <> 0 Then
            Temp = CDbl(TextBox1.Text)
            Temp = 1 / Temp
            TextBox1.Text = CStr(Temp)
            period = False
        End If
    End Sub

    Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
        If TextBox1.Text.Length <> 0 Then
            If [Operator] = "" Then
                Var1 = CDbl(TextBox1.Text)
                TextBox1.Text = ""
            Else
                Calculate()
            End If
            [Operator] = "Sub"
            period = False
        End If
    End Sub

    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
        If TextBox1.Text.Length <> 0 Then
            If [Operator] = "" Then
                Var1 = CDbl(TextBox1.Text)
                TextBox1.Text = ""
            Else
                Calculate()
            End If
            [Operator] = "Mult"
            period = False
        End If
    End Sub

    Private Sub btnPercent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercent.Click
    End Sub

    Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
        If TextBox1.Text.Length <> 0 Then
            If [Operator] = "" Then
                Var1 = CDbl(TextBox1.Text)
                TextBox1.Text = ""
            Else
                Calculate()
            End If
            [Operator] = "Div"
            period = False
        End If
    End Sub

End Class

Recommended Answers

All 12 Replies

okay thank you Kenny, I will go and look at it :)

Any time.

Kenny

well, Kenny your right,,they do go into great detail about how it works and stuff..but they do not give you a clue as to where to start coding for it, other than the fact that the sum is based off of other operators??...

so what i got out of this is that the percentage button is difficult to code due to the fact that you have to use ALL of the operators in order to get it right.
ex:
120+30%=
120-30%=
120x30%=
120/30%=

you have to somehow put all of those operators into the percentage??

I didn't look in detail at your code above so if this doesn't help I will look at your code in more detail.

You should have Value1, Operator and Value2
On the click of the percent key you should calculate:
TempValue = Value1 * (Value2 / 100)
Then set Value2 = TempValue WITHOUT clearing the Operator (+, -, *, etc) chosen or Value1's original value.

So once the = sign is pressed the regular calculation can carry out.

Make sense?

Kenny

Here is an example. If I type in windows calculator: 20 + 5% =
I get: 21

20 * (5 / 100) = 1 <-- becomes value 2

So now value one should still be 20 and value 2 is now 1 but the operator did not change

20 + 1 = 21

Kenny

kinda,,

but am I looking at repeating the process with each and every operator?

Then set Value2 = TempValue WITHOUT clearing the Operator (+, -, *, etc) chosen or Value1's original value

No the operator is not used at all until the equal sign is pressed.
I can't see your calculation routine but I assume your Value2 is whatever is in the textbox? If so every time the percent key is pressed you would do this calculation:

Dim Value2 As Double = CDbl(TextBox1.Text)
TextBox1.Text = Var1 * (Value2 / 100)

The percent key assumes the currennt value in the textbox is a percentage so it must be divided by 100 before using to get it into decimal form. It also assume you want that percentage of the first value so it will always be

Value2 = Value1 * (Value2 / 100)

Kenny

ahhhhhhhh I got it now :)

okay thank Ya !! i was gettin a little bit CoNfUsEd there! LOL

No problem. I actually had no clue how the percent key worked until I came across your question and read that article. It is a bit awkward. Let me know when you get it working :)

Thanks,
Kenny

I will for sure do that :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.