Help Making a calculator in VB

Jon Boy

New Member
Hey, im just messing around with Visual Studio 2003 & 2005, trying to get better for my uni course.

And I have already hit a snag making a calculator. So basically this plee goes out to almost anyone who can programme in VB.

My problem is with the souly Equals button (I think)

total2 = total1 + Val(lblDisplay.Text)
lblDisplay.Text = total2
total1 = 0
Thats the code. Its great for addition but as you can see thats because it has the + sign in.

What I want to know is how I would get pressing equals to complete ALL forms of mathamatical equations e.g. Addition, Multiplication, devision etc etc.

total1
total2
I declared at the beggining so everything can access them. Total1 for the addition is "total1 = total1 + Val(lblDisplay.Text)" but obviousely could be abything I wanted it to be, im my case I am using it as a running total.

As far as I can get with the multiply is this
total1 = total1 * Val(lblDisplay.Text)
lblDisplay.Text = ""
Think thats right, so just need help with this equals sign thing.

Thanks

Jon
 
Would you mind posting or PMing me all of your code, or say what exectly you want it to do becouse I some VB. I think I know how
 
Use a case statement to select the operation you want and flags (booleans or integers doesn't really matter) to know what operation you are set for.
 
Hey not really sure what you said cromwell I have only been doing VB for about 2 weeks. I have had an idea which im going to try, basically when anything is inputed, the equation is saved as a s string then the string rather than whatever I was doing is calculated if I it is possible.

But here is my code, well some of it missed out chunks that are the same

Public Class Form1 said:
Dim total1 As Integer
Dim total2 As Integer
A Button (Happens to be number 7) said:
lblDisplay.Text = lblDisplay.Text & "7"
The Display said:
total1 = total1 + Val(lblDisplay.Text)
Addition Button said:
total1 = total1 + Val(lblDisplay.Text)
lblDisplay.Text = ""

Thats all the stuff you need to know, and grrr I destgroyed the programme now, that doesnt even add up properly now, after you press = it goes crazy.
 
It would mess up because the lbldisplay.text is nothing ""


Quick snip of what i would do:
You need to save all the values into the memory so

Dim no1 as single
Dim no2 as single
Dim result as single

after that if you want to add the numbers from the txt box you will have to do this on the button click-


no1 = text1.text
no2 = text2.text
result = txtresult.text

no1 + no2 = result

result = result + txtresult

I have been told to save it to a dim (memory), this is because the system can access it faster than reading it from to field

If you want me to post my whole codeing for it i can do one for you?
 
Last edited:
this is the gist of it

Code:
'vb6 code, hand typed so there may be some errors
option explicit


dim num1 as single
dim num2 as single
dim total as double 'can be single, int, long, whatever
' operation:
' 1 = add
' 2 = subtract
' etc
dim operation as integer

private sub cmd_equals OnClick () 'i dont remember the exact syntax, the onclick function is where this goes

'load up num1 and num2 here, ive left the code out

select case operation
 case 1:
  total = num1 + num2
 case 2:
  total = num1 - num2
 case whatever:
  'do required operation

end sub

private sub cmd_add OnClick () 'same deal as equals, sub dec may be wrong
operation = 1
end sub
 
hey thanks would be more usefull if it was completely the right language. I should have been more specific, I use VB.Net 2005, thats quite abit different to VB 6.

So basically I have got the addition working perfectly now BUT only that, I will walk you through whats going on and why its not working and as VB6 is similar to .Net you may be able to help.



The problem is with the highlighted bit of code.

Basically without it, when I press for example "5 + 5 =" the answer will be 5 as it needs the plus to add the bit of memory to the number. I can get round that by inputting into the calculator "5 + 5 + =" then it works, but I dont want that.

So I put that line which came from the "plus" button in the "Equals" button, this means it does the + for me. But that creates a huge problem as it will not work for multiple minus and divide. Either I will need an "if" statement like
"if the previous plus/minus/divide/multiply button was pressed THEN use the plus/minus/divide/multiply equation" if you get what I mean but I would have no idea how to do that or if its even possible. Any ideas what I can do then? to get all the things except plus working?
 
Either I will need an "if" statement like
"if the previous plus/minus/divide/multiply button was pressed THEN use the plus/minus/divide/multiply equation" if you get what I mean but I would have no idea how to do that or if its even possible.
That is where the Select Case comes in. It works kind of like an If block. It looks like I had a slightly wrong syntax for VBs case statement. You can use strings if you prefer.
Code:
Select Case operation
 Case Is 1 ' or Case Is "Add"
  number1 = number1 + Val(lblDisplay.Text)
 Case Is 2 ' or Case Is "Subtract"
  number1 = number1 - Val(lblDisplay.Text)
 Case Is <whatever>:
  'do required operation
End Select
The format of the Select Case is:
Code:
Select Case <variable>
 Case Is <value>
 ...
 End Select
 
Haha i actually teach this to my class as an intro project to learn how text boxes work and such if you need anyhelp let me know i love vb and use it a lot...
 
Cromwel right, I get where your comming form but don't actually understand what u said.

"Case is 1 or case is add"

what does that mean?
How does it tell what case is which, where does it anywhere deffine a case as 1 ,2 or 3 etc?

Codeman I may ask for your help shortly, as im sure you could make the programe I want in literally 10min. Also OMG your 21 and teaching VB !
 
You would set a variable to the operation you are doing when you press the respective buttons then use the case statement to check what operation you are set to.
 
hopefully this is not for cheating on your homework coz our past programming class had to do this calculator too :D
 
lol no its not for homework dw, all we really do is make colour sliders and stuff. But I think I have almost got there, I think its kinda like what cromwell said.

Basically now when I press +, -, * etc it makes the value "number2" +, -, * etc. Then I use an if statement in the equals button, so

If number2 = "+" Then
number1 = Val(lblDisplay.Text) + number1
end if

Almost finished but that crashes my calculator. Some very helpfull person has volunteered to help me, so I will see if I can gain his assistance. If not keep suggestions comming.
 
Back
Top