======================================
=Writing A Caclulator In Visual Basic=
======================================
By SKoRM(mwpickle on vbforums)
For help with vb or if you just wanna hang around
goto irc.skorm.aus.cc or via webchat @
www.skorm.aus.cc:7000

Look at the problem
===================

What do we need:
-Basic Functions (+,-,/,*)
-Able to handle negitive number and decimals
-Advanced Feature like Tan, Sin,Cos,^,SQROOT
-Memory
-Clear Button
-Clean Code
Controls:
-Number Buttons (array) - cmdbutton(0-9)
-+,-,/,*,^,tan,cos,sin,=,.,+/- buttons -cmdplus ect....
-Display - txtdisplay
-Clear Button - cmdclear



Coding the numbers
==================
This is one of the most hardest but most vital part of the application. To keep the code clean and tidy I decided to use a

array. Arrays in controls are basically several controls which preform the same function. Arrays in variables are a lot

harder and won't be explained here.

Insert a text box and one command button. Name the text box txtdisplay and set alignment
to '1' and set caption to '0'. Now with the command button, label it cmdbutton and make the caption '0'. Copy and paste

another 9 of these button and If visual basics asks to make a control array click yes. Now change the caption for all these

command box's to the same number as the index number (the number that defines each object in the array, it's the number in

the brackets).

Double click on one of the command buttons to enter the code for the buttons. Code for adding a number to the txt display

is...

txtdisplay = txtdisplay & Index

Index is the controls index number. This code will work but has some serious flaws. Firstly it leaves the '0' at the front.

You could use this was if you wanted to but a much better way would be like this

txtdisplay = txtdisplay * 10 + Index

The code gets the current number and times it by 10 then simply add the number pressed. This code is alright until you add

decimals. We'll leave this code like that until we reach there.


Simple Operations
=================
Coding the plus, minus , divide and times buttons isn’t too hard at all. First create the buttons, with captions like '+' and

names like 'cmdplus' for plus, minus, times, divide and equal. Now a simple layout on how this will work.

1. Save the old figure as a variable
2. Set the display as '0'
3. Set a variable to a number, eg (plus = 1, minus = 2, times = 3, divide =4)

This way the program will know what to when the user press's 'equal'. I ended up with code looking like this.


Dim old As Integer
Dim operation As Integer

Private Sub cmdplus_Click()
old = txtdisplay 'save old number
txtdisplay = 0 'Clears the display
operation = 1 'Set's the operation as addition
End Sub

Now it's is time for the equals button. This was just a case of serval 'If' statements. This code ended up in my cmdequal

button and provided a simple answer to the sums. I've included comment to help explain the operations.

If operation = 1 Then txtdisplay = old + txtdisplay
'This just simply checks if it has to do addition, if so, it does it.
If operation = 2 Then txtdisplay = old - txtdisplay
If operation = 3 Then txtdisplay = old * txtdisplay
If operation = 4 Then txtdisplay = old / txtdisplay
operation = 0 'sets operation as 0
'Sets it at '0' so that the person can't press it twice by accident.
old = 0
'Clear the old variable ready for more sum's.

You now should have a basic calculator which can preform simple operations. Now it is time for decimals and negative numbers

Time to make it funky
=====================
Alright remember how I said that the number button code wouldn't work with these 2 things. Well time to clear that up.

First we'll start with the decimal button. Create the command button and lets add some code. This part of it is fairly simple

so I'll leave it for you to figure out what it does. A few notes though, I declared the variable 'decimala' so the computer

knows that its a decimal which will come in handy in a second.

Dim decimala As Boolean

Private Sub cmddecimal_Click()
txtdisplay = txtdisplay & "."
decimala = True
End Sub

Now this works fine until your try to add numbers after the decimal. This is because the code is still trying to times it by

10. This is why we declared the variable. Now let’s replace our number code with this. Also you should declare 'a'.

If decimala = False Then
txtdisplay = txtdisplay * 10 + Index
Else
a = a * 10
txtdisplay = txtdisplay + Index / a
End If

Basicly, the code check wether or not the its a decimal

If it isn't -
It'll times the number by 10 and plus the 'Index'
If it is -
It'll divide the index by A by 10
Then it'll add index divided by 'a' to txtdisplay

This is fairly hard for people that don't know there basic maths but a few reads over and you should be able to work out how

it works. Just as a check-up you should end up having this at the start of your code.

Dim old As Integer
Dim operation As Integer
Dim a As Integer
Dim decimala As Boolean

Now, since that works you should be easily able to make a "+/-" button. I cheated on the
making of mine and simply done this.

txtdisplay = txtdisplay - (txtdisplay * 2)

By this time you should also have a clear button which can be made by simply going txtdisplay = '0'.Now that, that’s sorted

we can move onto some simple hard stuff.

Some advanced functions
=======================
Ok, you've made it this far though the tutorial, so I think its about time that I stop holding your hand. Basically with the

square, square root, ^ button, and tan, sin cos they are 1 line code. Note with tan, sin, cos, they are worked out in rad,

there are ways to convert to deg but we can do that later.

-Tan-
txtdisplay = tan(txtdisplay)

-Cos-
txtdisplay = cos(txtdisplay)

-Sin-
txtdisplay = sin(txtdisplay)

-Sq-
txtdisplay = txtdisplay ^ 2

-Sqroot-
txtdisplay = sqr(txtdisplay)

-Power-
Ok, it's a little different here, this one is like the plus and minus. Simply for the code for the button do the same

old = txtdisplay
txtdisplay = 0
operation = 5

Now for the code within the eqauls button.

If operation = 4 Then txtdisplay = old ^ txtdisplay

Alright, now for memory

Memory
======
Once again, once you think about it, this part is simple. Declare a variable 'memory'. Now make 3 buttons, 1 to right, 1 to

read and 1 to clear.

-To Right-
memory = txtdisplay
txtdisplay = "0"

-To Read-
txtdisplay = memory

-To Clear-
memory = ""

Conclusion
==========
You now should have a fully functioning calculator with advanced feature and with little code. I hope this helped you learn

some of the basics of visual basics and how simple it is to make code more compact.