Solved

# Calculating values

Posted on 2005-04-27
153 Views
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
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,
0

LVL 25

Accepted Solution

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

LVL 1

Author Comment

Duh,

I dont know y i didn't see that.

Thank you,
RonaldBiemans
0

## Featured Post

XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…