Visual BASIC Programming

A Simple Calculator

 

History:

 

BASIC was one of the first scientific programming languages widely used on computers beginning in 1964.  BASIC is an abbreviation for Beginner's All-purpose Symbolic Instruction Code.  With the advent of the ‘Windows’ operating system, BASIC faded from the computing world for a while but was reinvented in 1991 as Visual BASIC.  In 1994, Microsoft began bundling a Visual BASIC Editor with Excel and later Microsoft Word.

 

Tonight we will be using the Visual BASIC editor that comes with Microsoft Word XP.

 

Object of tonight’s Exercise:

 

To build a highly regarded and extremely powerful yet simple ‘Calculator’ using the Visual BASIC programming language.  When completed, the calculator will look like this:

 

The calculator contains eight items:

 

Each of these ‘objects’ has a set of ‘properties’ such as color, text size, centering, alignment – all can be modified by ‘you’ the programmer.

 

Getting Started:

 

1 Log onto the computer as you have done before …

 

2 Bring up Microsoft Word – Select Tools, Macro, Security – set this to ‘Medium’ and then select File, Close.

 

3 Select File, New.   Enter the following on the page:

 

            My first Visual BASIC calculator

            By: < your name here >

            Date:  March 4, 2004

 

            Using:   Microsoft Word Visual BASIC Editor

 

4 Save the file onto your Desktop with the name “My Calculator”

 

5 Bring up the Visual BASIC Editor.  Select Tools, Macro, Visual BASIC Editor

 

6 Begin the Project:

 

The Form

 

 

 


·        In the VB Editor – Select Insert, then UserForm.

·        Stretch the form to make it bigger.

·        Look at the ‘properties box’ – Change Caption to “Simple Calculator”

·        In ‘properties’ – click BackColor and select Palette – pick a color.

 

 

 

TextBox

Placement

 

 

 

 

 


·        Find the ‘Toolbox’ – click the Hammer/Wrench symbol if needed.

                  or ( View|Toolbox)

·        Find the TextBox symbol – it’s the ab| symbol

·        Click the symbol – then over on the ‘UserForm’ itself, place the TextBox

·        The properties window will now say ‘TextBox1’ at the top.

·        In this properties window – Scroll down to “Text” and change it to 55

·        Add another TextBox – Scroll down to “Text” and change it to 38.5 (You may need to click in the background area to get the toolbox back.)

·         

·        Add the third TextBox – leave it blank.

·        Adjust each box so it’s the same size.

 

Labels are

Next

 

 

 

 


·        Find the ‘Toolbox’

·        Find the Label symbol – it’s the A symbol

·        Place three ‘labels’ to the left of the TextBoxes.

·        Change the ‘caption’ property of each one so it looks like the picture above.

·        Change the ‘TextAlign’ property of each one to ‘fmTextAlignRight

 

 

Now the

Buttons

 

 

 


·        Back to the ‘Toolbox’ – find the Command Button. 

·        Place two buttons on the UserForm

·        Change the ‘Caption’ property of each button to Add or Subtract

 

F5 - Run

 

 

 


·        Lets’ see what your ‘program’ looks like so far.  Press F5

 

·        You should see the form and components as a ‘running’ program.

 

·        Click the X in the upper right corner to stop the program.

 

Visual BASIC is an ‘event driven’ language.  When a program is started, the user has to ‘do’ something to cause the program to run.  In our case, the user will press a button. The ‘event’ is pressing the button.

 

We placed two buttons – there will be a small program, also called the ‘code’ connected to each button.

The ‘code’

 

 

 

 


·        Back on the form – double left click the ‘Add’ Button.  The code window should open up.

 

                        Private Sub CommandButton1_Click()

                        TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)

                        End Sub

 

·        The top and bottom lines are there – you add the middle line – carefully !

·        We’re ready to test this -  Press the F5 key. This will run the program.

·        ‘Press’ the ‘Add’ button – the sum of the two numbers should appear.

·        Go ahead and enter new numbers directly into the 1st two windows.

·        Press the ‘Add’ button again.

·        Close the program with the X in the upper right corner.

 

·        Back on the form – double left click the ‘Subtract’ Button. Enter the middle line of the following code:

 

                        Private Sub CommandButton2_Click()

                        TextBox3.Text = Val(TextBox1.Text) - Val(TextBox2.Text)

                        End Sub

 

·        Run the Program again ( F5 )

·        Test both the Add and Subtract buttons.

 

You’re now ready to add multiply (*) and divide (/) buttons.  You should be able to figure this out now.

 

Need an additional challenge ?   Turn this into a ‘Scientific’ Calculator.

 

The Trignometric buttons -  Sine, Cosine, Tangent  can be implemented by adding three buttons and appropriate ‘code’ for each button.

 

In Visual BASIC,  an angle that is input in ‘degrees’ must be converted to ‘radians’.

 

Here’s some working ‘code’ to get you started:

 

            Private Sub CommandButton3_Click()

            AngleinRadians = Val(TextBox1.Text) * 3.14159 / 180

            TextBox3.Text = Sin(AngleinRadians)

            End Sub

 

 

 

Need another challenge using JavaScript?  Take a look at:

 

http://web.sunybroome.edu/~biegen_j/js/index.html

 

 

Finally, Windows comes with a nice calculator ( Start|Programs|Accessories) – Look at all the buttons !  But you could do this yourself …..