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.
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.
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.
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."
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:
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:
Double-click the "tan" button and paste the following, as in the previous steps:
Double-click the "pi" button and paste the following:
Double-click the "C" button and paste:
Paste the following at the very top of the source code editor:
This will hold numbers that are needed in binary operations and the operation to be performed, such as division or multiplication.
Double-click each of the binary operation buttons (+,-,X,/, and exp) in turn and paste the following code:
Replace the operation text with the operation. For example, for the "X" button, replace "+" with "X."
Double-click the "=" button and paste the following:
Double-click the "log" button and paste:
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
Things you need
- Visual Basic 2008 Express or later