How to Make a Scientific Calculator in Visual Basic

Visual Basic is a powerful programming language created to Microsoft to make it easier for users without computer programming training to develop software for Windows computers. Creating a scientific calculator provides an overview of how to develop software for Visual Basic.

Setting Up the Project

Step

Create a new project by clicking "File" and "New Project." When prompted, give it the name "Scientific Calculator."

Step

A blank user interface form will appear for your calculator, add graphical user interface (GUI) components to it from the toolbox on your left. You will use two types of components: a textbox to display results and a bunch of buttons.

Step

Drag a textbox to the top of your calculator from the toolbox on the left. This will be where the numbers appear and where the user will type numbers for later computation.

Step

Drag a button into your form from the Toolbox. In the properties box, find the "text" property and change it to "sin." Find the name property and change it to "sinButton." Click the "sin" button in the form and press "Ctrl-C" to make a copy of the button in your keyboard. Press Ctrl-V key 11 times to create the 11 new buttons for your form. For each button, change the button text to the following: cos, tan, pi, exp, sqrt, log, C, +, -, X, = and /, and give it a name that describes it. For example, for the "/" button, change its name property to "divideButton."

Doing the Programming

Step

Double-click the "sin" button. This will move you to the source code and create an event method for handling sin events and place your cursor within it. Paste the following there:

Step

Double-click the "cos" button. Once again, this will move you to the source code and create an event method. Paste the following into it:

Step

Double-click the "tan" button and paste the following, as in the previous steps:

Step

Double-click the "pi" button and paste the following:

Step

Double-click the "C" button and paste:

Programming Continued

Step

Paste the following at the very top of the source code editor:

Step

This will hold numbers that are needed in binary operations and the operation to be performed, such as division or multiplication.

Step

Double-click each of the binary operation buttons (+,-,X,/, and exp) in turn and paste the following code:

Step

Replace the operation text with the operation. For example, for the "X" button, replace "+" with "X."

Step

Double-click the "=" button and paste the following:

Step

Double-click the "log" button and paste:

Step

Double-click the "sqrt" button and paste:

Step

Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click TextBox1.Text = Math.Sqrt(TextBox1.Text) End Sub