We help IT Professionals succeed at work.
Get Started

My Calculator

340 Views
Last Modified: 2013-11-27
Im working on a Calculator app in VB and the calculator works great when it comes to the calculations part. However im having a very hard time figuring out how to reset the Property Answer to 0 when clicking the Ce button that way it starts over instead of continuing to keep track of the last answer so i can start new calculations

Code is below along with the Class for the Answer.

Any help is greatly appreciated
Option Explicit On
Option Strict On
Option Infer Off
 
Public Class MainForm
 
    Dim calc As New Calculator
    Dim isValid As Boolean
 
    Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
 
        Dim Input As Decimal
        isValid = Decimal.TryParse(calcTextBox.Text, Input)
        If isValid = True Then
            tapeTextBox.Text = tapeTextBox.Text & "+ " & Input.ToString.PadLeft(6) & ControlChars.NewLine
            calc.Answer = calc.Answer + Input
            calc.AddValue(Input)
        End If
    End Sub
 
    Private Sub equalButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles equalButton.Click
 
        tapeTextBox.Text = tapeTextBox.Text & "---------" & ControlChars.NewLine & "= " & calc.Answer.ToString.PadLeft(6) & ControlChars.NewLine
 
    End Sub
 
    Private Sub subtractButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles subtractButton.Click
 
        Dim Input As Decimal
        isValid = Decimal.TryParse(calcTextBox.Text, Input)
        If isValid = True Then
            tapeTextBox.Text = tapeTextBox.Text & "- " & Input.ToString.PadLeft(6) & ControlChars.NewLine
            calc.Answer = calc.Answer - Input
            calc.SubValue(Input)
        End If
    End Sub
 
    Private Sub MultiplyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MultiplyButton.Click
 
        Dim Input As Decimal
        isValid = Decimal.TryParse(calcTextBox.Text, Input)
        If isValid = True Then
            tapeTextBox.Text = tapeTextBox.Text & "* " & Input.ToString.PadLeft(6) & ControlChars.NewLine
            calc.Answer = calc.Answer * Input
            calc.MultiplyValue(Input)
        End If
    End Sub
 
    Private Sub divideButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles divideButton.Click
 
        Dim Input As Decimal
        isValid = Decimal.TryParse(calcTextBox.Text, Input)
        If isValid = True Then
            tapeTextBox.Text = tapeTextBox.Text & "/ " & Input.ToString.PadLeft(6) & ControlChars.NewLine
            calc.Answer = calc.Answer / Input
            calc.MultiplyValue(Input)
        End If
    End Sub
 
    Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
 
        calcTextBox.Text = String.Empty
        tapeTextBox.Text = String.Empty
        calcTextBox.Focus()
    End Sub
 
    Private Sub clearAllButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearAllButton.Click
 
        calcTextBox.Text = String.Empty
        calcTextBox.Focus()
    End Sub
 
    Private Sub calcTextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
    Handles calcTextBox.KeyPress
 
        If (e.KeyChar < "0" OrElse e.KeyChar > "9" AndAlso e.Handled) _
        AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." Then
            e.Handled = True
        End If
 
    End Sub
 
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Me.Close()
    End Sub
End Class

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE