Click here to Skip to main content
14,644,614 members
Rate this:
Please Sign up or sign in to vote.
See more:
HI Everyone,

I've built this (very rudimentary) calculator and can't to do more than one operation at a time (5 +5 =10, 5+5+5=10) as it's only holding the last two numbers entered. I've tried using
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
       ' Get the number, display the number, retun the focus
       Number1 = Number1 + Val(txtDisplay.Text)
       txtDisplay.Text = ""
       txtDisplay.Focus()
       User = "+"

   End Sub

But doesn't help, also tried quite a few other things. I haven't been able to find an answer and was hoping that someone could throw me a hint on this.

Here's most of the code:

Dim Number1 As Double
   Dim Number2 As Double
   Dim Numberx As Double
   Dim conversion As Double
   Dim User As String
   Dim hasDecimal As Boolean
   Dim tmpValue As Double

   Private Sub frmCalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


   End Sub

   Private Sub buttons(sender As Object, e As EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click
       If txtDisplay.Text = "" Then txtDisplay.Text = sender.text Else txtDisplay.Text = txtDisplay.Text & sender.text
       txtDisplay.Focus()
   End Sub

   Private Sub btnCE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCE.Click
       ' Remove last entered number
       txtDisplay.Text = ""
       txtDisplay.Focus()
   End Sub

   Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
       ' Clear all numbers entered and held
       txtDisplay.Text = String.Empty : Number1 = 0.0 : Number2 = 0.0
       txtDisplay.Focus()
   End Sub

   Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click
       ' Add decimal point at point of insertion
       If InStr(txtDisplay.Text, ".") > 0 Then
           Exit Sub
       Else
           txtDisplay.Text = txtDisplay.Text & "."
       End If
   End Sub

   Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
       ' Get the number, display the number, retun the focus
       Number1 = Val(txtDisplay.Text)
       txtDisplay.Text = ""
       txtDisplay.Focus()
       User = "+"

   End Sub

   Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
       ' Get the number, display the number, retun the focus
       Number1 = Val(txtDisplay.Text)
       txtDisplay.Text = ""
       txtDisplay.Focus()
       User = "-"
   End Sub

   Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
       ' Get the number, display the number, retun the focus
       Number1 = Val(txtDisplay.Text)
       txtDisplay.Text = ""
       txtDisplay.Focus()
       User = "*"
   End Sub

   Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
       ' Get the number, display the number, retun the focus
       Number1 = Val(txtDisplay.Text)
       txtDisplay.Text = ""
       txtDisplay.Focus()
       User = "/"

   End Sub


   Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
       Dim Result As Double
       Number2 = txtDisplay.Text

       ' Select math operatons
       Select Case User
           Case "+"
               Result = Number1 + Number2
               txtDisplay.Text = Result.ToString()
           Case "-"
               Result = Number1 - Number2
               txtDisplay.Text = Result.ToString()
           Case "/"
               Result = Number1 / Number2
               txtDisplay.Text = Result.ToString()
           Case "*"
               Result = Number1 * Number2
               txtDisplay.Text = Result.ToString()
           Case "x^"
               Result = Number1 ^ Number2
               txtDisplay.Text = Result.ToString()
           Case "1/x"
               Result = Number1 * 1 / 100
               txtDisplay.Text = Result.ToString()
           Case "PosNeg"
               Result = Number1 * -1
               txtDisplay.Text = Result.ToString()



       End Select

       txtDisplay.Text = Result.ToString()

   End Sub


I know that it's pretty Frankensteined up, but it's what I was able to come up with that works, Well, except for the fact that it won't perform more than one op at a time.

___________________________________________________________

Actually, using:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
       Number1 = Number1 + Val(txtDisplay.Text)
       txtDisplay.Text = ""
       txtDisplay.Focus()
       User = "+"


Works for the addition (5+6+9+4=24), but I can't figure out to handle the subtraction. I've put in many different types of
 Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
num1 - Val(txtDisplay.Text)
( +, -, +-, etc.) all give me a wrong result. Any help at all would be much appreciated!!
Posted
Updated 14-Dec-14 5:01am
v6

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

From looking at your code it looks like it is running the way it was designed.

  1. When I press 5 it is output to the TextBox.
  2. When I press +, Number1 is set to 5; the TextBox is cleared.
  3. I press 5 again and it is output to the TextBox.
  4. When I press +, Number1 is set to 5; the TextBox is cleared. (again)
  5. I press 5 again and it is output to the TextBox.
  6. When I press =, Number2 is set to 5; The arithmetic addition operation is performed on Result = Number1 + Number2 = 10

Each time an operator button is pressed, you need to evaluate and update Result. Lets says there are 3 variables, Result, Number, and Operator.

Result = 0
(5 pressed) 
Number = 5

(+ pressed) 
Operator = +
Result = {Result==0} {Operator==+} {Number==5} = 5

(5 pressed) 
Number = 5

(+ pressed) 
Operator = +
Result = {Result==5} {Operator==+} {Number==5} = 10

(5 pressed) 
Number = 5

(= pressed) 
Operator = + (unchanged)
Result = {Result==10} {Operator==+} {Number==5} = 15
Yes, the way you have approached this problem is... interesting.

Instead of telling you what you did "wrong", I'll let you work on rethinking the problem and how it can be simplified and streamlined. :) It's part of becoming a better programmer. You should be able to reduce all of this to maybe 20 lines of code. Tip: Use Button.Tag to your advantage.
   
v2
Comments
Member 11139267 14-Dec-14 8:26am
   
Hi Yvan,
This is for a calculator I built in Visual Basic 2012, not certain how an HTML button.tag would work with that in this instance, or how to implement it. Regardless, as I said above I know that you have many years exp., me not at all. I have tried adding:
<pre lang="vb">Dim Number3 As Double

Case "+"
Result = Number1 + Number2 + Number3
txtDisplay.Text = Result.ToString()
</pre>
Just to see if it will at least allow to hold up to three numbers, but no.
Also, I'm really not certain how I could possibly get this down to 20 lines of code, that would be interesting to see!
Yvan Rodrigues 14-Dec-14 11:48am
   
Just so I'm clear this is a Windows Forms application?
Member 11139267 14-Dec-14 12:28pm
   
Hi Yvan,
Yes, it is a Windows Form application built using VS Express 2012 for desktop.
Yvan Rodrigues 14-Dec-14 19:23pm
   
I wrote a little article for you.

http://www.codeproject.com/Tips/853405/A-Simple-Calculator-in-Windows-Forms

Sorry, it is in C# (I'm at work and it would take me 3 times longer in VB.NBET because I rarely use it). I'm sure you can get the gist of it.

Essentially, you need fewer variables to do multiple calculations, not more. You do not want to "hold numbers", you want to evaluate at each step. This would be different for a scientific calculator, which allows brackets, and needs to follow Order of Operations. In that case you would use a Queue or a Stack to accumulate operations until they are ready for evaluation.
Member 11139267 15-Dec-14 10:40am
   
HI Yvan,
Thank you so much for your time. As this is my first semester in VB I know that my methods are extremely basic, hopefully I will eventually learn how to tighten up the code. I know nothing of C#, but will definitely try to implement some of the tips you have provided.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100