hey, there are loads of ways to code a calculator, but i do it this way, so i dont want to have a talk so lets begin
have an form with the following buttons and text box: (btw, i forgot to add the "+" button on this image, sorry!
I dont care how you call your calculator and what you enter as start value in the textbox
well, double-click on the button with number 1 on it.
now, write the following code: (btw, i assume you take '0,' as starting value)
If Text1.Text = "0," Then Text1.Text = "1" Else Text1.Text = Text1.Text + "1"
explination of the code:
if ( start for asking a question )
text1.text = "0," (the question itself)
then (if value question is true, then it will do the following expression)
text1.text = "1" ( the If True expression )
else (if question asked is false, it performs the following expression)
text1.text = text1.text + "1"
or in other words, this code makes the text "1" if the textbox is "0,"
do the same with all other 8 buttons with the numbers, but replace the number "1" with the value of the button
with the zero button, add this code:
If Not Text1.Text = "0," Then Text1.Text = Text1.Text + "0"
now we set-up our values, so lets make it do stuff
but for this, we need a module
make a module, and add the following code
Global reminder1 As String
Global reminder2 As String
global changer as string
global answer as string
you can also do a private reminder, but i am at this moment to lazy what the simple codes for that are.
now, lets code the rest
now, lets make the devide buttons first ( the slash "/" )
changer = "/"
If Not Val(Text1.Text) = "0" Then reminder1 = Val(Text1.Text) Else MsgBox "Incorrect divide value!!", vbInformation, "Error"
If Val(Text1.Text) = "0" Then changer = ""
Text1.Text = "0"
now, lets make the "=" button work for the slash "/"
If changer = "/" Then GoTo divide
Exit Sub
divide:
If Val(Text1.Text) = "0" Then MsgBox "Incorrect divide value!!", vbInformation, "Error"
If Not Val(Text1.Text) = "0" Then
reminder2 = Val(Text1.Text)
answer = reminder1 / reminder2
Text1.Text = answer
reminder1 = Text1.Text
End If
End Sub
now try it
i wont explain the code, because its better to find out yourself how this code works
now, with that, i want that when i press clear, my screen is 'clear'
so simple, add this code to that button
reminder1 = ""
reminder2 = ""
changer = ""
text1.text = "0"
now, with that, lets start coding the * (multiply ) button by editing its buttons code
changer = "*"
reminder1 = Val(Text1.Text)
Text1.Text = "0"
now, again, lets edit the = button so we have the following code:
If changer = "/" Then GoTo divide
If changer = "*" Then GoTo multiply
Exit Sub
divide:
If Val(Text1.Text) = "0" Then MsgBox "Incorrect divide value!!", vbInformation, "Error"
If Not Val(Text1.Text) = "0" Then
reminder2 = Val(Text1.Text)
answer = reminder1 / reminder2
Text1.Text = answer
reminder1 = Text1.Text
End If
Exit Sub
multiply:
reminder2 = Val(Text1.Text)
answer = reminder1 * reminder2
Text1.Text = answer
reminder1 = Text1.Text
end sub
now try it, they both work
(btw, if you would code a little more, you can have an exact copy of the options as windows calculator, but we arent doing that so)
now, the both work, lets continue with the - and + buttons
the - button and + button:
"-"
changer = "-"
reminder1 = Val(Text1.Text)
Text1.Text = "0"
"+"
changer = "+"
reminder1 = Val(Text1.Text)
Text1.Text = "0"
and once again, lets edit the = button till you got the following code:
If changer = "/" Then GoTo divide
If changer = "*" Then GoTo multiply
If changer = "-" Then GoTo min
If changer = "+" Then GoTo plus
Exit Sub
divide:
If Val(Text1.Text) = "0" Then MsgBox "Incorrect divide value!!", vbInformation, "Error"
If Not Val(Text1.Text) = "0" Then
reminder2 = Val(Text1.Text)
answer = reminder1 / reminder2
Text1.Text = answer
reminder1 = Text1.Text
End If
Exit Sub
multiply:
reminder2 = Val(Text1.Text)
answer = reminder1 * reminder2
Text1.Text = answer
reminder1 = Text1.Text
Exit Sub
min:
reminder2 = Val(Text1.Text)
answer = reminder1 - reminder2
Text1.Text = answer
reminder1 = Text1.Text
Exit Sub
plus:
reminder2 = Val(Text1.Text)
answer = Val(reminder1) + Val(reminder2)
Text1.Text = answer
reminder1 = Text1.Text
End Sub
now try your calculator,
after testing the calculator now should work ( i made this tutorial along with the calculator , so everything is tested by me )
you now got a basic calculator, and you can find out the rest yourself, at least, i guess xD