next up previous
Next: 9. Declaring your Variable Up: VisualBasic Previous: 7. TextBoxes and Labels


8. Problems with TextBoxes

The above program seems very straightforward. Once you have set up the Visual (i.e. form) component of the project, the code was amazingly simple. It just consisted of the lines:

Private Sub Command1_Click()
x = ( -b + Sqr( b^2 - 4 * a * c ) ) / ( 2 * a )
End Sub

Now we are going to modify this project to make it even simpler. Edit the form, and highlight the x text box. Delete this by hitting the delete key on your keyboard. (Delete also the Label box next to it.)

Now double click on the Calculate button. Edit the code so that you have simply:

Private Sub Command1_Click()
c = a + b
End Sub

This seems like a backward step! However, trust me! Run this project and enter in your choice of a and b.

What is going wrong??!!

If you try with a few different values of a and b you'll soon see the pattern. It is just treating a and b as character strings and not as numbers. The line in your code c = a + b is simply concatenating a and b (i.e. appending b onto the end of a).

The basic problem is that VisualBasic treats the entries in the TextBoxes as strings rather than as numbers. Since you cannot do algebra (i.e. multiply, divide, add etc.) with strings, we must convert these string variables into numbers first, and then do our algebra on these numbers!

Go back to edit mode and highlight the a text box on the form. Now change the Name property from a to str_a. Similarly change the name of the b's text box to str_b, and the c's text box to str_c. The str_ just underlines the fact that what you enter into the text box associated with a is treated as a character string and not a number.

We now have to modify the code slightly. Double click on the Calculate button to bring up the code. Alter the code to the following:

Private Sub Command1_Click()
a = Val(str_a)
b = Val(str_b)
c = a + b
str_c = Str(c)
End Sub

Note that as you type in the Val( or the Str(, VisualBasic gives you a little caption which helps you complete the line.

What are we doing in the above? Well, we've introduced two new functions, Val and Str. These just convert between the string and numerical values. So

a = Val(str_a)

takes the character string str_a (which is what you will be entering into the a text box when you run this project) and converts it to a numerical value, and stores this in the (numerical) variable, a.

Similarly

str_c = Str(c)

converts the numerical value stored in c into the string str_c (which is the actual text box which you so carefully called str_c above).

To you and I, both the TextBox str_a and the numerical variable a contain the same thing. However, to VisualBasic they are quite different. The str_a is a string variable (and you can't do algebra on string variables) and a is a number which can be used in an algebraic expression.

This seems a little messy, but it is the way you have to proceed!!!

Text boxes always contain text or string variables, and therefore the functions Val and Str must be used to convert to and from numerical values.


next up previous
Next: 9. Declaring your Variable Up: VisualBasic Previous: 7. TextBoxes and Labels
Chris Allton 2006-10-27