Calculator Codes in Visual Basic

Only available on StudyMode
  • Topic: 1918, 1925, 1920
  • Pages : 13 (2742 words )
  • Download(s) : 747
  • Published : May 22, 2012
Open Document
Text Preview
Calculator
In this tutoral, Basic Calculator in VB.Net, we will look at creating a basic calculator. The calculator will have the following functionality:

* Addition
* Subtraction
* Division
* Multiplication
* Square Root
* Exponents (Power Of)
* Clear Entry
* Clear All

There will be a 2nd tutorial that will cover some more advanced features such as

* Adding a number to memory
* Removing a number from memory
* Calculating with a number in memory
* Entering numbers by typing

The first thing you need to do is create a new project in Visual Studio (or Visual Basic Express Edition if thats what you use). Once you have created your new project you need to create your user interface, your user interface should look like this:

Your user interface will consist of

* Buttons 0 through 9
* Buttons for

* Addition
* Subtraction
* Division
* Multiplication
* Exponents (x^)
* Inverse (1/x)
* Square Root (sqrt)

* Decimal
* Equals
* Backspace
* CE (Clear Entry)
* C (Clear All)
* ReadOnly TextBox for input (Make sure TabStop is also set to False)

How you setup your user interface is up to you, but remember people are used to a calculator looking a certain way so you may wish to follow my example.

In this tutorial I will show you how to code two of the number buttons (since all 10 are the same except the zero button), how to code the calculations buttons, the clear buttons and the backspace buttons. Before writing any code you need to add the following variables to the top (Globals): 01| 'variables to hold operands|

02| Private valHolder1 As Double|

03| Private valHolder2 As Double|
04| 'Varible to hold temporary values|

05| Private tmpValue As Double|
06| 'True if "." is use else false|

07| Private hasDecimal As Boolean|
08| Private inputStatus As Boolean|

09| Private clearText As Boolean|
10| 'variable to hold Operater|

11| Private calcFunc As String|

These variables will be used through out our program thats why they're globals. Now, before any calculations can be done, the user needs to be able to enter numbers into the input box, so lets take a look at how to do that (Ill use the number 1 key and the zero key).

Number one key:
01| Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles cmd1.Click| 02|      'Check the inputStatus|

03|      If inputStatus Then 'Its True|
04|          'Append values to the value|

05|          'in the input box|
06|          txtInput.Text += cmd1.Text|

07|      Else   'Value is False|
08|          'Set the value to the value of the button|

09|          txtInput.Text = cmd1.Text|
10|          'Toggle inputStatus to True|

11|          inputStatus = True|
12|      End If|

13|     End Sub|

When a user clicks a number button (in this case the number one button) we check the status of the inputStatus flag. If its true then we know we can just append the next value to the end of whats currently in the input box, otherwise we just enter the number into the input box. All the remaining numbers follow this procedure, except the zero button, this one is slightly different as we don't want the user to be able to enter zero as the first number (this is covered more in the decimal button functionality). 

So lets take a look at how we code the zero button:
01| Private Sub cmd0_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles cmd0.Click| 02|      'Check the input status|

03|      If inputStatus Then 'If true|
04|          'Now check to make sure our|

05|          'input box has a value|
06|          If txtInput.Text.Length >= 1 Then|

07|             'Add our zero|
08|             txtInput.Text += cmd0.Text|

09|          End If|
10|...
tracking img