Jump to content

Need help with VB.Net code

WhiteSkyMage

Hey guys,

 

I am making a project on VB.net and i am having a very difficult time solving a problem i know how it is supposed to be solved but I just have no idea how to write it...

I am basically trying to recreate the windows calculator and that's the problem:

whenever I repeat a certain operation it would shows a wrong answer like:

 

8-2-2=8 (has to be 4)

5*5*5=50 (has to be 125)

27/3/3=10 (has to be 3)

 

I've tried a lot of stuff but it is maybe that I do not have any idea how to use Do While loop (which i think it's the one i need).

For now i can do just this:

1. I enter first number and click "BtnMinus"

2. It stores this number into the "totalOne" variable and clears the Display

3. I enter the 2nd number and click = button

4. It does the addition/subtraction/multiplication/division and gives the correct answer

but if I am to press again BtnMinus after entering the 2nd number, instead of equals, then enter a 3rd number, it is going to give a wrong result after the = button.

 

I attached a screenshot....

post-43298-0-33540600-1447014748_thumb.p

Intel Core i9-9900K | Asrock Phantom Gaming miniITX Z390 | 32GB GSkill Trident Z DDR4@3600MHz C17 | EVGA RTX 3090 FTW3 Watercooled | Samsung 970 EVO 1TB M.2 SSD | Crucial MX500 2TB SSD | Seasonic Focus Plus Gold 1000W | anidees AI Crystal Cube White V2 | Corsair M95 | Corsair K50 | Beyerdynamic DT770 Pros 250Ohm

Link to comment
Share on other sites

Link to post
Share on other sites

Maybe check if the value of totalTwo has been set, and if it has, add/subtract/multiply/divide the two and set totalOne to the result?

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
Share on other sites

Link to post
Share on other sites

Maybe check if the value of totalTwo has been set, and if it has, add/subtract/multiply/divide the two and set totalOne to the result?

 

well the totaltwo gets set when I am actually making it equal to the totalone +-/* (the 2nd number). Problem is the 3rd number. If I click the = it will give me the correct result, but if i click another operation it is going to give a wrong answer in the end...

 

It's like I have to move the whole subtraction from my = button to the - but if I do that, the whole thing gets messed up. This is a headache...

Basically It has to sense it when I click whatever operator after I have entered the 2nd number...

 

I am extremely confused of how this is supposed to work....

Intel Core i9-9900K | Asrock Phantom Gaming miniITX Z390 | 32GB GSkill Trident Z DDR4@3600MHz C17 | EVGA RTX 3090 FTW3 Watercooled | Samsung 970 EVO 1TB M.2 SSD | Crucial MX500 2TB SSD | Seasonic Focus Plus Gold 1000W | anidees AI Crystal Cube White V2 | Corsair M95 | Corsair K50 | Beyerdynamic DT770 Pros 250Ohm

Link to comment
Share on other sites

Link to post
Share on other sites

So if you go on your own calculator at home and type: 

 

1+6-2*5/5....= something

 

and with every click on a new operator you have your answer from the previous operation...and that's like a hell on earth to do in vb.net.

Intel Core i9-9900K | Asrock Phantom Gaming miniITX Z390 | 32GB GSkill Trident Z DDR4@3600MHz C17 | EVGA RTX 3090 FTW3 Watercooled | Samsung 970 EVO 1TB M.2 SSD | Crucial MX500 2TB SSD | Seasonic Focus Plus Gold 1000W | anidees AI Crystal Cube White V2 | Corsair M95 | Corsair K50 | Beyerdynamic DT770 Pros 250Ohm

Link to comment
Share on other sites

Link to post
Share on other sites

Since you're entering an equation piece by piece and not all at once, you don't have to worry about order of operations. So if you do this

  • Enter Number
  • Click operator
  • Enter Number
  • Click Operator
  • ...

It is just shorthand to

  • Enter Number
  • Click Equals (wouldn't really do anything in the first case)
  • Click Operator
  • Enter Number
  • Click Equals
  • Click Operator
  • ...

So every operator button basically applies equals before it applies the operator part. So just make sure all the variables are updated correctly, and display the result, before applying the next operator.

Link to comment
Share on other sites

Link to post
Share on other sites

Since you're entering an equation piece by piece and not all at once, you don't have to worry about order of operations. So if you do this

  • Enter Number
  • Click operator
  • Enter Number
  • Click Operator
  • ...

It is just shorthand to

  • Enter Number
  • Click Equals (wouldn't really do anything in the first case)
  • Click Operator
  • Enter Number
  • Click Equals
  • Click Operator
  • ...

So every operator button basically applies equals before it applies the operator part. So just make sure all the variables are updated correctly, and display the result, before applying the next operator.

 

Ermmm...ok? I think you just said the same thing i said in the first post...

Except that

  • Enter Number
  • Click operator
  • Enter Number
  • Click Operator
  • ...

doesn't work for me - it would always give a wrong result. I found this guy who did a similar thing https://social.msdn.microsoft.com/Forums/vstudio/en-US/98b41a3a-4916-41ff-81b6-d258e2518a17/struggling-with-multiple-operations-with-a-simple-calculator?forum=vbgeneral

but his program is on a completely different level and I got no idea what he's doing there...

 

Note that i am trying to solve that problem from a week and i am just playing with that code moving it around, trying if/case/for/while loops and always getting errors or wrong answers...so im about to give up on it and move on to finish this project off in another area. 

Intel Core i9-9900K | Asrock Phantom Gaming miniITX Z390 | 32GB GSkill Trident Z DDR4@3600MHz C17 | EVGA RTX 3090 FTW3 Watercooled | Samsung 970 EVO 1TB M.2 SSD | Crucial MX500 2TB SSD | Seasonic Focus Plus Gold 1000W | anidees AI Crystal Cube White V2 | Corsair M95 | Corsair K50 | Beyerdynamic DT770 Pros 250Ohm

Link to comment
Share on other sites

Link to post
Share on other sites

Ermmm...ok? I think you just said the same thing i said in the first post...

 

Well if you already knew that, then you code doesn't reflect that. Your equals button and your operator buttons apply the calculations in two different ways.

Link to comment
Share on other sites

Link to post
Share on other sites

Well if you already knew that, then you code doesn't reflect that. Your equals button and your operator buttons apply the calculations in two different ways.

 

Ok look

 

Private Sub BtnPlus_Click(sender As System.Object, e As System.EventArgs) Handles BtnPlus.Click

        MyOperation = BtnPlus.Text <------------- this is for my "case" loop. On my button text is "+"
        totalOne = totalOne + Val(txtDisplay.Text) <---------- this is where my VB.Net book starts me off  http://www.homeandlearn.co.uk/NET/nets1p19.html (scroll down)
        txtDisplay.Clear()
 
In equalsbutton:

Select Case MyOperation

 Case "+"
                totalTwo = totalOne + Val(txtDisplay.Text) 'The add function: Add the value stored in variable totalOne to the next entered value. 
                txtDisplay.Text = totalTwo                  'TotalTwo takes the result of the added values, and once the "=" button is pressed displays it in the textbox txtDisplay
                totalOne = 0                                        'set the first variable to 0
 
My equals and my operation button do completely different things in that case. All the operations are done when I click the Equals. I know there is something wrong with the operation buttons but I have no idea how to fix it. Do i have to do something like this?

post-43298-0-14052800-1447025941_thumb.p

Intel Core i9-9900K | Asrock Phantom Gaming miniITX Z390 | 32GB GSkill Trident Z DDR4@3600MHz C17 | EVGA RTX 3090 FTW3 Watercooled | Samsung 970 EVO 1TB M.2 SSD | Crucial MX500 2TB SSD | Seasonic Focus Plus Gold 1000W | anidees AI Crystal Cube White V2 | Corsair M95 | Corsair K50 | Beyerdynamic DT770 Pros 250Ohm

Link to comment
Share on other sites

Link to post
Share on other sites

Do i have to do something like this?

 

Given you're just posting screenshots, I can't exactly test it myself, but yes, I believe that would be one way of getting this to work correctly. If you were to copy the code from inside the appropriate case statement and put it above the code in the corresponding operator buttons, it looks like it would work the way you're doing things.

 

Note you will probably also need to account for the case where it's the first time the operator button is clicked, but you seem to have a FirstNumExists Boolean that you can use for that.

Link to comment
Share on other sites

Link to post
Share on other sites

Given you're just posting screenshots, I can't exactly test it myself, but yes, I believe that would be one way of getting this to work correctly. If you were to copy the code from inside the appropriate case statement and put it above the code in the corresponding operator buttons, it looks like it would work the way you're doing things.

 

Note you will probably also need to account for the case where it's the first time the operator button is clicked, but you seem to have a FirstNumExists Boolean that you can use for that.

 

Well to be honest that's just half of it. The other part is all about conversions from decimal to binary, hex and Octal (im still working on it). But I came back to this one cuz I realized something has gone wrong while I tested it... however it seems difficult to solve that one problem...

 

Now I am not sure how will this boolean help me at all... perhaps maybe the first time I click the button, the button will be off, but the 2nd, 3rd and nth time, it will be on... It is however extremely confusing...

 

Otherwise a counter perhaps? I have no damn idea...

Intel Core i9-9900K | Asrock Phantom Gaming miniITX Z390 | 32GB GSkill Trident Z DDR4@3600MHz C17 | EVGA RTX 3090 FTW3 Watercooled | Samsung 970 EVO 1TB M.2 SSD | Crucial MX500 2TB SSD | Seasonic Focus Plus Gold 1000W | anidees AI Crystal Cube White V2 | Corsair M95 | Corsair K50 | Beyerdynamic DT770 Pros 250Ohm

Link to comment
Share on other sites

Link to post
Share on other sites

Now I am not sure how will this boolean help me at all... perhaps maybe the first time I click the button, the button will be off, but the 2nd, 3rd and nth time, it will be on... It is however extremely confusing...

 

FirstNumExists is set to False by default so you don't have to worry about initializing it. Then you'll set it to True at the end of your operator buttons. The C button that clears everything and resets the calculator would be where you'll want to set it back to False.

 

So the operator buttons would start to look more like

If FirstNumExists Then    ' Code from equals switch block 'End If' rest of operator code 'FirstNumExists = True
Link to comment
Share on other sites

Link to post
Share on other sites

 

FirstNumExists is set to False by default so you don't have to worry about initializing it. Then you'll set it to True at the end of your operator buttons. The C button that clears everything and resets the calculator would be where you'll want to set it back to False.

 

So the operator buttons would start to look more like

If FirstNumExists Then    ' Code from equals switch block 'End If' rest of operator code 'FirstNumExists = True

 

Hmm i actually did that with a counter...

here:

counter = counter + 1
        If counter = 1 Then
            MyOperation = BtnPlus.Text
            lblTip.Text = txtDisplay.Text & "+"
            totalOne = totalOne + Val(txtDisplay.Text)
            txtDisplay.Clear()
        ElseIf counter > 1
            totalTwo = totalOne + Val(txtDisplay.Text) 'The add function: Add the value stored in variable totalOne to the next entered value. 
            lblTip.Text = totalTwo 'TotalTwo takes the result of the added values, and once the "=" button is pressed displays it in the textbox txtDisplay
            txtDisplay.Text = ("")
            totalOne = totalTwo
        End If
though again there is something not alright with this and i got the feeling that I should not be using the If, but something else...

Intel Core i9-9900K | Asrock Phantom Gaming miniITX Z390 | 32GB GSkill Trident Z DDR4@3600MHz C17 | EVGA RTX 3090 FTW3 Watercooled | Samsung 970 EVO 1TB M.2 SSD | Crucial MX500 2TB SSD | Seasonic Focus Plus Gold 1000W | anidees AI Crystal Cube White V2 | Corsair M95 | Corsair K50 | Beyerdynamic DT770 Pros 250Ohm

Link to comment
Share on other sites

Link to post
Share on other sites

ah won't work either way, it goes closer with the counter but it's just not there... problem is if i click the minus and then the plus it adds... and it gives wrong answer. 

 

ahh i will have to play with it another day...sorry for wasting your time. It's my project after all...

Intel Core i9-9900K | Asrock Phantom Gaming miniITX Z390 | 32GB GSkill Trident Z DDR4@3600MHz C17 | EVGA RTX 3090 FTW3 Watercooled | Samsung 970 EVO 1TB M.2 SSD | Crucial MX500 2TB SSD | Seasonic Focus Plus Gold 1000W | anidees AI Crystal Cube White V2 | Corsair M95 | Corsair K50 | Beyerdynamic DT770 Pros 250Ohm

Link to comment
Share on other sites

Link to post
Share on other sites

problem is if i click the minus and then the plus it adds... and it gives wrong answer. 

 

So you want the user to be able to change their mind about the current operator if selecting multiple operators in a row? 

 

For example

  • User enters 10
  • User clicks +
  • User clicks - (this takes over)
  • User clicks * (this takes over)
  • User enters 5
  • User clicks equals (or another operator) and the current result is 50
Link to comment
Share on other sites

Link to post
Share on other sites

So you want the user to be able to change their mind about the current operator if selecting multiple operators in a row? 

 

For example

  • User enters 10
  • User clicks +
  • User clicks - (this takes over)
  • User clicks * (this takes over)
  • User enters 5
  • User clicks equals (or another operator) and the current result is 50
Similar to that - basically User enters 8

clicks -

enters 2

clicks +

enters 4

clicks *

enters number ...

with every next operator that is clicked however, before the next number is entered, the answer of the previous operation is showed. So basically you are doing operations and shows the previous answer without you clicking =.

Intel Core i9-9900K | Asrock Phantom Gaming miniITX Z390 | 32GB GSkill Trident Z DDR4@3600MHz C17 | EVGA RTX 3090 FTW3 Watercooled | Samsung 970 EVO 1TB M.2 SSD | Crucial MX500 2TB SSD | Seasonic Focus Plus Gold 1000W | anidees AI Crystal Cube White V2 | Corsair M95 | Corsair K50 | Beyerdynamic DT770 Pros 250Ohm

Link to comment
Share on other sites

Link to post
Share on other sites

Similar to that - basically User enters 8

clicks -

enters 2

clicks +

enters 4

clicks *

enters number ...

with every next operator that is clicked however, before the next number is entered, the answer of the previous operation is showed. So basically you are doing operations and shows the previous answer without you clicking =.

 

I guess I'm not sure what you specifically want the user experience to be so it's hard to give you the correct answers.

 

The Windows 10 calculator (in standard mode) will display the result when the operator button is pressed, which is what I was picturing when I was trying to help before.

  • User enters 8 (Display shows "8")
  • User enters - (Display still shows  "8" showing the equation "8 -" above it)
  • User enters 2 (Display shows "2" still showing the equation "8 -" above it)
  • User enters + (Display shows "6" showing the equation "8 - 2 +" above it)
  • etc

However not every calculator works like that. For example the Android 6 calculator works differently, displaying the result on numeric inputs instead of operations.

  • User enters 8 (Display shows "8")
  • User enters - (Display shows "8-")
  • User enters 2 (Display shows "8-2" showing the result "6" below it)
  • User enters + (Display shows "8-2+" still showing the result "6" below it)
  • etc
Link to comment
Share on other sites

Link to post
Share on other sites

I guess I'm not sure what you specifically want the user experience to be so it's hard to give you the correct answers.

The Windows 10 calculator (in standard mode) will display the result when the operator button is pressed, which is what I was picturing when I was trying to help before.

  • User enters 8 (Display shows "8")
  • User enters - (Display still shows "8" showing the equation "8 -" above it)
  • User enters 2 (Display shows "2" still showing the equation "8 -" above it)
  • User enters + (Display shows "6" showing the equation "8 - 2 +" above it)
  • etc
Yup that's exactly it. I am just doing a remake of the windows 7 calc which is essentially the same... one way or another i will figure it out...

Intel Core i9-9900K | Asrock Phantom Gaming miniITX Z390 | 32GB GSkill Trident Z DDR4@3600MHz C17 | EVGA RTX 3090 FTW3 Watercooled | Samsung 970 EVO 1TB M.2 SSD | Crucial MX500 2TB SSD | Seasonic Focus Plus Gold 1000W | anidees AI Crystal Cube White V2 | Corsair M95 | Corsair K50 | Beyerdynamic DT770 Pros 250Ohm

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×