Visual Basic 6.0 Guide
Do My Maths Program
1. Launch Visual Basic 6.0 using the start menu, and select Standard Exe from the New Project dialog box.
2. We will start by changing some of the properties of this form. Change the following properties in the properties window.
|Caption||Do My Maths For Me|
3. We are going to change the name of the project. Click on Project, Project1 Properties and set the project name to maths.
4. Save the project in a new folder in your user space. Call the folder maths.
5. We are going to make the form look like this.
6. Click on the text box tool in the toolbox . Drag a rectangle onto your form to create a text box. Change the following properties.
Yes, the Text property is set to nothing. This is so it is ready to accept input when the program is run. The Maxlength property is set to 8 because large numbers can scare the computer a bit and make your program crash (not good).
7. Add another box with all of the same properties. This time call it txtNum2. Place it like the one in the diagram.
8. Add a frame to the form by clicking on the frame tool in the toolbox. Place it as in the diagram and change the caption property to Choose An Operator.
9. Adding the option buttons needs you to follow these instructions carefully.
- Click on the frame so that it is selected.
- Click on the option button tool in the toolbox. Drag a button inside the frame. Change its name property to optOperator and its caption to +.
- Click on the new option button so that it is selected. Now click Edit, Copy from the menu.
- Click on the frame again so that it is selected.
- Click on Edit, Paste on the menu. When asked if you want to create a control array, say Yes. Place the new button next to the first and change its caption to -.
- Repeat this process until you have all four buttons.
- Select the + button and change its Value property to true.
You have just created a control array. Before when you have added controls (images, scrollbars, timers etc.) they have each had a different name. In this case they all have the same name. In addition to this they have an Index (eg optOperator(1) ) so that you can tell them apart. This will be useful for our code later.
10. Select the Command Button tool from the toolbox and draw a button on the form. Call the button cmdEquals and set its caption to equals.
11. Add a label to the form, just below the equals button. Set its name to lblAnswer, its caption to nothing, its Appearance to Flat, and the BorderStyle to Fixed Single.
12. Take a deep breath. The design is over. Now it's time to do the programming.
13. Click on View, Code to bring up the code window. Type in the following.
Dim intOperator as Integer
This line tells the program that we want to store some information in the computer's memory whilst the program is running. We want to call the information intOperator and we want it to store a whole number (integer). This is called declaring a variable.
14. Go back to the form and double click on any of the space in it where there are no controls. This should return you to the code window but with the code for the event handler that deals with what happen when the form first appears on the screen. Add this code.
Private Sub Form_Load()
intOperator = 0
This sets the value of our variable to 0 when the program first loads. You'll see why in the next step.
15. We want to change the value of intOperator each time someone clicks on one of the option buttons. This way the program will always know whether the person wants to add, subtract etc. So we need to do something with the event that is fired when the option buttons are clicked. Double click on one of the option buttons. If you did things right earlier you should see the code window with the following lines of code.
Private Sub optOperator_Click(Index As Integer)
If it doesn't say this, accelerate the palm of your right hand squarely into the middle of your forehead. Go back to Step 9 and have another go.
16. Add the following code.
Private Sub optOperator_Click(Index As Integer) intOperator = Index End Sub
This code sets the value of our variable to whatever the person clicks. Now our program can always work out what the person wants to do.
17. Now we need to do the part of the program that makes it useful. Double click on the equals button and add the following code.
Private Sub cmdEquals_Click()
Dim myAnswer As Double
Select Case intOperator
myAnswer = Val(txtNum1.Text) + Val(txtNum2.Text)
myAnswer = Val(txtNum1.Text) - Val(txtNum2.Text)
myAnswer = Val(txtNum1.Text) * Val(txtNum2.Text)
myAnswer = Val(txtNum1.Text) / Val(txtNum2.Text)
lblAnswer.Caption = Str(myAnswer)
Line 2 declares a variable myAnswer, to store the answer to the question. Line 3 tells the program to look at the value of our variable intOperator. Line 4 tells the program that, if intOperator equals 0 it should run the code on Line 5 which adds the numbers in the boxes together.
The Val() function is used because the text box is made to contain text (this variable type is called a string); we want to deal with numbers. Lines 6 to 12 show the options for each of the other buttons with line 12 telling the program that this section is over. Line 13 sets the caption property of our label to the answer to the problem. We use the str() function because the label's caption property is a string.
Extending The Do My Maths Program
Think about what you can do so far
In this program you have created text boxes so that you can enter numbers and get the program to do something with them. So far all the program does is basic arithmetic. To make the program more useful, you need to add more features.
Squares, Cubes and Square Roots
You could get the program to calculate square roots, squares and cubes of the numbers you enter. To do this, make the form bigger and add another text box and a button. Set the properties that need to be set up (names, text, caption). Double click the button to bring up the code window like you did before. You will also need another label to display the answer.
To make the program work out the square of a number you need a line of code like this
myAnswer = Val(txtNum3.Text) ^ 2
The ^ symbol raises a number to the power that follows - in this case to the power of 2. A cube would need the number to be raised to the power of 3. If you raise a number to the power of 0.5, you get the square root.
Can you make the program easy to use for these functions?