VB Calculator Help?
VB Calculator Help?
VB Calculator Help?
VB Calculator Help?
VB Calculator Help?
VB Calculator Help? VB Calculator Help? VB Calculator Help? VB Calculator Help? VB Calculator Help? VB Calculator Help? VB Calculator Help? VB Calculator Help?
VB Calculator Help? VB Calculator Help?
VB Calculator Help?
Go Back  Xtreme Visual Basic Talk > > > VB Calculator Help?


Reply
 
Thread Tools Display Modes
  #1  
Old 03-25-2013, 04:36 PM
DaveMc DaveMc is offline
Newcomer
 
Join Date: Mar 2013
Posts: 2
Post VB Calculator Help?


Code:
Public Class Form1

    Private isAdding As Boolean = False
    Private isSubtracting As Boolean = False
    Private isDividing As Boolean = False
    Private isMultiplying As Boolean = False

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If txtBox1.Text.Length <> 0 And txtBox2.Text.Length And isAdding = False Then
            Dim answer As Double = add(CType(txtBox1.Text, Double), CType(txtBox2.Text, Double))
            txtBox3.Text = answer.ToString()
            txtBox1.ReadOnly = True

            isAdding = True

        ElseIf isAdding Then
            Dim ans As Double = add(CType(txtBox1.Text, Double), CType(txtBox3.Text, Double))
            txtBox3.Text = ans.ToString()

        Else
            MessageBox.Show("Please fill all fields.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
        If txtBox1.Text.Length <> 0 And txtBox2.Text.Length <> 0 And isSubtracting = False Then
            Dim answer As Double = subtract(CType(txtBox1.Text, Double), CType(txtBox2.Text, Double))
            txtBox3.Text = answer.ToString()
            txtBox1.ReadOnly = True

            isSubtracting = True

        ElseIf isSubtracting Then
            Dim ans As Double = subtract(CType(txtBox1.Text, Double), CType(txtBox3.Text, Double))
            txtBox3.Text = ans.ToString()
        Else
            MessageBox.Show("Please fill all fields.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
        If txtBox1.Text.Length <> 0 And txtBox2.Text.Length <> 0 And isDividing = False Then
            Dim answer As Integer = divide(CType(txtBox1.Text, Integer), CType(txtBox2.Text, Integer))
            txtBox3.Text = answer.ToString()
            txtBox1.ReadOnly = True

            isDividing = True

        ElseIf isDividing Then
            Dim ans As Integer = divide(CType(txtBox1.Text, Integer), CType(txtBox3.Text, Integer))
            txtBox3.Text = ans.ToString()
        Else
            MessageBox.Show("Please fill all fields.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
        If txtBox1.Text.Length <> 0 And txtBox2.Text.Length <> 0 And isMultiplying = False Then
            Dim answer As Integer = multiply(CType(txtBox1.Text, Integer), CType(txtBox2.Text, Integer))
            txtBox3.Text = answer.ToString()
            txtBox1.ReadOnly = True

            isMultiplying = True

        ElseIf isMultiplying Then
            Dim ans As Integer = multiply(CType(txtBox1.Text, Integer), CType(txtBox3.Text, Integer))
            txtBox3.Text = ans.ToString()
        Else
            MessageBox.Show("Please fill all fields.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Function add(ByVal num1 As Double, ByVal num2 As Double)
        Return num1 + num2
    End Function

    Private Function subtract(ByVal num1 As Double, ByVal num2 As Double)
        Return num1 - num2
    End Function

    Private Function divide(ByVal num1 As Integer, ByVal num2 As Integer)
        Return num1 / num2
    End Function

    Private Function multiply(ByVal num1 As Integer, ByVal num2 As Integer)
        Return num1 * num2
    End Function

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtBox1.ReadOnly = False

        revertBool()

        txtBox1.Clear()
        txtBox2.Clear()
        txtBox3.Clear()
    End Sub

    Private Sub revertBool()
        isAdding = False
        isSubtracting = False
        isDividing = False
        isMultiplying = False

    End Sub
End Class

The above code is for a basic calculator project. Everything works fine except for when I want to add/subtract/multiply/divide ontop of my answer.

E.g 10 + 5 = 15 (this works fine) but,
Ans + 5 should give a value of 20 as 15 + 5 = 20 but it doesn't

Any help would be appreciated
Reply With Quote
  #2  
Old 03-25-2013, 06:13 PM
Gruff's Avatar
GruffVB Calculator Help? Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Welcome to the forum Dave, Don't forget to read the forum Posting Guidelines.

It depends on whether you are creating an RPN or Standard Calculator.

With an RPN you capture x then y followed by the operator, pushing them down a stack.
You apply the operator to the last two entries and put the result on top of the stack as the new y.

A standard calculator requires jockying the operator around but is essentially the same process.

I made a small example using a listbox for the stack and a textbox for the entry.
It only has a plus button but you should get the idea.
Code:
Public Class Form1 Private Sub Plus_Click(sender As System.Object, e As System.EventArgs) Handles cmdPlus.Click With ListBox1 ' Push items onto the top of the listbox .Items.Insert(0, TextBox1.Text) .Items.Insert(0, "+") TextBox1.Text = "" TextBox1.Focus() End With End Sub Private Sub Equals_Click(sender As System.Object, e As System.EventArgs) Handles cmdEquals.Click With ListBox1 .Items.Insert(0, TextBox1.Text) Dim y As Double = CDbl(.Items(0)) Dim op As Char = CChar(.Items(1)) Dim x As Double = CDbl(.Items(2)) Dim ans As Double = 0 Select Case op Case "+"c ans = x + y Case "-"c ans = x - y Case "*"c ans = x * y Case "/"c ans = x / y End Select TextBox1.Text = ans.ToString TextBox1.Focus() End With End Sub End Class
Attached Images
File Type: png Calc.png (15.5 KB, 9 views)
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #3  
Old 03-26-2013, 03:08 AM
DaveMc DaveMc is offline
Newcomer
 
Join Date: Mar 2013
Posts: 2
Default Thanks

Thanks very much gruff, great help Kind of new to the VB language but getting there slowly.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
VB Calculator Help?
VB Calculator Help?
VB Calculator Help? VB Calculator Help?
VB Calculator Help?
VB Calculator Help?
VB Calculator Help? VB Calculator Help? VB Calculator Help? VB Calculator Help? VB Calculator Help? VB Calculator Help? VB Calculator Help?
VB Calculator Help?
VB Calculator Help?
 
VB Calculator Help?
VB Calculator Help?
 
-->