next up previous
Next: 8. Problems with TextBoxes Up: VisualBasic Previous: 6. Message Boxes


7. TextBoxes and Labels

Earlier you wrote a simple program which calculated (one of) the solutions of the quadratic equation. However, it is not in a very convenient format because in order to change the coefficients, a,b and c, you have to leave the run mode, and edit the actual basic code itself.

We are now going to learn a far more convenient way to change the values of a,b and c in the above VisualBasic project.

Make sure you are in edit mode, (still with the above project from the previous section) and click on the window containing Form1. Have a look at the left hand side of the VisualBasic Programming Environment window. You will see a number of icons in the Toolbox (including the Command Button icon which we have already used). Move the mouse over the button which looks like a white rectangle with ab| in it (don't click yet!). You should see the text "TextBox" appear as you move your mouse over it. Double click on this white rectangle icon now. You will now see this text box appear on your form. It is probably positioned in an inconvenient place on your form, so just drag it near to the top-left of your form.

Make sure that the text box you have just inserted on your form is highlighted. Now have a look near the bottom-right of the VisualBasic Programming window. You will see a table with two columns. This is the Properties table. In the (Name) row, you will see Text1. Change this to a. Further down in the Properties table, you will see the Text row. Delete the "Text1" which appears in this row (i.e. it is now empty).

What we have done is to set up an "input" box in your form into which you can put any value of a you like. Repeat the above procedure so that we have text boxes for both b and c as well as for a. (i.e. (i) double click on the text box icon, and (ii) in the Properties window, change the Name of the text box to b (or c), and make sure that the Text property is empty.)

Now double click on the Command1 button. This should take you to the code associated with this button which should be something like:

Private Sub Command1_Click()
a = 1
b = 2
c = 8
x = ( -b + Sqr( b^2 - 4 * a * c ) ) / ( 2 * a )
Print "x = "; x
End Sub

Now delete the three lines

a = 1
b = 2
c = 8

You don't need these lines to define the value of a,b and c because these will be defined while in run mode. (Honest!) Now run this project. You will see a form (window) with three empty white boxes in it. Ummm... It is a little confusing because it is not clear which box is a, and which is b and c. VisualBasic has a way of labelling forms (windows).

Go back to edit mode (i.e. hit the stop button). Now have a look at the ToolBox which is the array of icons on the left-hand of the VisualBasic Programming window. You'll see one which is simply the letter A called a Label. Double click on this to add it to your form. Now move it next to the text box which contains a. Have a look at the Properties window of this Label. Change the Caption property from Label1 to a. You will see the character a appear on the form!

Repeat this procedure for both b and c so that their text boxes are now labelled appropriately.

Now run this project one more time. It still doesn't quite look up to scratch because there is an ugly grey button called simply Command1 on it. It would be better to have this called Calculate or whatever. Can you guess how to change this? (Go back to edit mode, highlight the Command1 button and change its Caption property from Command1 to, e.g. Calculate. You may want to change the text font (by altering the Font property) of the background colour (by altering the BackColor property, and then changing the Style property from Standard to Graphical) as well while you are there!

OK. Now run this project once more. Enter in your choice of a,b and c into the text boxes. Then click on the (brightly coloured?) Calculate button. You should see the quadratic solution appear on the screen!

This is almost good enough to submit for Continuous Assessment, but it could be just a little bit better. What we'd like to do is to output the result to a nice text box, rather than to the background part of the form.

Any ideas how to do this?

CHECKPOINT: Modify the above project so that you now have a fourth text box on your form (correctly labelled) which will display the value of x.

text boxes can be used to input information from the user/keyboard directly into our program.

Labels are a way of writing useful words directly onto the form.


next up previous
Next: 8. Problems with TextBoxes Up: VisualBasic Previous: 6. Message Boxes
Chris Allton 2006-10-27