[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 158
  • Last Modified:

Calculating values

Hello everyone,

I am just starting out in Visual Basic .net and was working on a calculator. I have it so that when you click 1 it puts 1 in the text box and so forth. When you click the + button it does this "741" -> "741 + " and then when I add the numbers up it works. however if I then clear the form and add another set of numbers the computer still adds the previuse numbers in as well. How do I fix this? Most of the code is below.

    Dim action As String = ""
    Dim nums As Array
    Dim temp As Integer
    Dim dec As Boolean

    Private Sub btn_zero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_zero.Click
        result.Text += "0"
    End Sub

    Private Sub btn_pls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_pls.Click
        action = "+"
        result.Text += " + "
    End Sub

    Private Sub btn_eql_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_eql.Click
        If action.Length > 0 Then
            If action = "+" Then
                nums = result.Text.Split("+")
                Dim i As Integer
                For i = 0 To nums.Length - 1
                    Trim(nums(i))
                    temp = temp + CInt(nums(i))
                    nums(i) = 0
                Next
                result.Text = temp.ToString
                action = ""
            End If
            If action = "-" Then
                nums = result.Text.Split("-")
                Dim i As Integer
                Trim(nums(i))
                temp = CInt(nums(0)) - CInt(nums(1))
                If nums(0) > nums(1) Then
                    result.Text = "-" & temp.ToString
                Else
                    result.Text = temp.ToString
                End If
                action = ""
            End If
        Else
            MsgBox("Please select an action.", MsgBoxStyle.OKOnly)
        End If
    End Sub

    Private Sub btn_one_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_one.Click
        result.Text += "1"
    End Sub

    Private Sub btn_two_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_two.Click
        result.Text += "2"
    End Sub

    Private Sub btn_dbl_zero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_dbl_zero.Click
        result.Text += "00"
    End Sub

    Private Sub btn_three_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_three.Click
        result.Text += "3"
    End Sub

    Private Sub btn_six_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_six.Click
        result.Text += "6"
    End Sub

    Private Sub btn_five_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_five.Click
        result.Text += "5"
    End Sub

    Private Sub btn_four_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_four.Click
        result.Text += "4"
    End Sub

    Private Sub btn_seven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_seven.Click
        result.Text += "7"
    End Sub

    Private Sub btn_eight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_eight.Click
        result.Text += "8"
    End Sub

    Private Sub btn_nine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_nine.Click
        result.Text += "9"
    End Sub

    Private Sub btn_clr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_clr.Click
        result.Text = ""
        action = ""
    End Sub

    Private Sub btn_sub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_sub.Click
        action = "-"
        result.Text += " - "
    End Sub

    Private Sub btn_sqr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_sqr.Click
        If action.Length > 0 Then
            MsgBox("You have already selected an action, you may not select an action and square at the same time. Please clear the results and try again.", MsgBoxStyle.OKOnly)
        Else
            result.Text = CInt(result.Text) * CInt(result.Text)
            action = ""
        End If
    End Sub

Thank you,
chadreeves1
0
chadreeves1
Asked:
chadreeves1
1 Solution
 
RonaldBiemansCommented:
also set your temp to 0

like

 Private Sub btn_clr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btl_clr.Click
        result.Text = ""
        action = ""
        temp = 0
  End Sub

If you want, I can show you a more .net way to do what you are doing :-)

0
 
chadreeves1Author Commented:
Duh,

I dont know y i didn't see that.

Thank you,
RonaldBiemans
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now