How to Make a Scientific Calculator in Visual Basic

By Kevin Walker

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.

Things You'll Need

  • Visual Basic 2008 Express or later

Setting Up the Project

Step 1

Create a new project by clicking "File" and "New Project." When prompted, give it the name "Scientific Calculator." 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 2

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 3

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 1

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: Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click TextBox1.Text = Math.Sin(TextBox1.Text) End Sub

Step 2

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: Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click TextBox1.Text = Math.Cos(TextBox1.Text) End Sub

Step 3

Double-click the "tan" button and paste the following, as in the previous steps: Private Sub Button21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button21.Click TextBox1.Text = Math.Tan(TextBox1.Text) End Sub

Step 4

Double-click the "pi" button and paste the following: Private Sub Button22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button22.Click TextBox1.Text = Math.PI End Sub

Step 5

Double-click the "C" button and paste: Private Sub Button23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button23.Click TextBox1.Text = "" memory = 0 End Sub

Programming Continued

Step 1

Paste the following at the very top of the source code editor: Dim memory As Double Dim operation As StringThis will hold numbers that are needed in binary operations and the operation to be performed, such as division or multiplication.

Step 2

Double-click each of the binary operation buttons (+,-,X,/, and exp) in turn and paste the following code: memory = TextBox1.Text operation = "+"Replace the operation text with the operation. For example, for the "X" button, replace "+" with "X."

Step 3

Double-click the "=" button and paste the following: Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If operation.Equals("+") Then TextBox1.Text = memory + TextBox1.Text ElseIf operation.Equals("-") Then TextBox1.Text = memory - TextBox1.Text ElseIf operation.Equals("X") Then TextBox1.Text = memory * TextBox1.Text ElseIf operation.Equals("/") Then TextBox1.Text = memory / TextBox1.Text ElseIf operation.Equals("exp") Then TextBox1.Text = Math.Pow(memory, TextBox1.Text) End If End Sub

Step 4

Double-click the "log" button and paste: Private Sub Button24_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button24.Click TextBox1.Text = Math.Log(TextBox1.Text) End Sub

Step 5

Double-click the "sqrt" button and paste: 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

References & Resources