Chapter 3- Data Types and Operators Boolean Expressions Page 2 3 4

Introduction

The If Statement Page 2 3 4 5 6 7 8 9
Arithmetic Operations Page 2 3 4 5 6 Boolean Operators and Nested If Statements Page 2 3 4 5 6 7
Events and Sequential Processing Page 2 3 4 5 More Examples Page 2 3 4 5 6 7 8 9 10 11 12
Datatypes and Conversions Page 2 3 4 5 6 7 Using Check Box and Option Controls Page 2 3 4 5 6 7 8 9 10
Variable Declarations - Local and Global Page 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Exercises Page 2 3 4 5 6 7 8
Chapter 4- Selection Statements Review Questions
Introduction

3-5: Variable Declarations (continued)

Anticipating User Actions

Normally if the user wants to enter a new initial capital amount (and/or a new interest rate) they should use the Clear button. However, it is possible for the user to simply change the value of the Text property of the Initial Capital textbox directly without using the Clear button. When this happens the calculation should be reset, since to continue when the NextYr button is pressed wouldn't make any sense. Thus we should use the txtInitialCap_TextChanged event shown here to initialise the Year variable and to clear the text boxes.

Private Sub txtInitialCap_TextChanged( ... )
'
' Reinitialise the year variable and the textboxes
'
year = 0
txtYear.Text = vbNullString
txtInterest.Text = vbNullString
txtCapital.Text = vbNullString
End Sub

Note that this event does not reinitialise the value of the capital variable. Why should it be reinitialised and why is it not done here in this event?

The same re-initialising should occur if the user changes the interest rate by simply re-entering the value as opposed to using the Clear button. Thus you should also create the txtRate_TextChanged event shown next.

Note that this event also reinitializes the capital variable. Why must this happen and what problems might arise?

Private Sub txtRate_TextChanged( ... )
capital = Cdbl(txtInitialCap.Text)
year = 0
txtYear.Text = vbNullString
txtInterest.Text = vbNullString
txtCapital.Text = vbNullString
End Sub

You should ask yourself what other (unlikely?) user actions might take place. Consider not just a single user action but also sequences of two or more user actions with objects in the interface. You should easily discover that this simple program does not work correctly! It is rather difficult to create robust event-driven programs, especially since we are just beginning and don't have all the tools of the language at our disposal yet.

In this program it is also possible that the user changes a value in one or more of the Year, Interest or Capital textboxes. Should you allow this to happen? What will be the consequence if you do? Test your program to verify your prediction. Try to discover how you might prevent the user from changing the values in these textboxes. Alternatively, which control object would be better to use to display these values than a textbox?