Chapter #1 Exercises Exercises 1.1

advertisement
Chapter #1 Exercises
Exercises 1.1
For this exercise you
will required to write an
algorithm for a program
for adding two numbers.
You will be required
to create the following and need to hand in:
IPO Chart
PDT Chart
Flowchart
Pseudocode
Use the following information to code your program:
a) Design your form with 3 textboxes to represent Number 1, Number 2 and Sum. Use 2
labels - the first to give instructions to the user and the second to identify the Sum textbox.
Use 3 command buttons - the first, Add Numbers, to cause the two numbers to be added and
displayed in the sum textbox, the second to Clear all three textboxes after each addition and
the third to Exit.
b) Set the properties
of the textboxes to
be "nothing". The
command buttons
will change the
contents of the text
property of each
textbox. You will do
that in the coding.
Set the Name,
Caption,
Backcolour, etc.
> c) Write the code
for Add Numbers.
Use the predefined
function
Val(<textbox_name
>) to convert the
text to a value so that it can be added:
txtOut = Val(txtNum1) + Val(txtNum2)
This command will assign the sum of the two textboxes, txtNum1 and txtNum2 to the
variable Sum. This statement is equivalent to:
Sum = Val(txtNum1.text) + Val(txtNum2.text)
The first statement leaves out the text property - it is assumed that you are assigning the
value to the text property to be displayed.
d) Write the code for Clear. You need to re-set the text boxes to nothing. Try to do this on
your own.
e) Write the code for Exit.
3. Change the textbox txtOut to a label named lblOut. Which property in a label will allow
you to display text? Instead of txtOut.text = Val(txtNum1.text) + Val(txtNum2.text), what
would the command be? Change the Clear code as well so that it will work with this label.
4. So far, when the user enters information in a
textbox, the cursor will only move to the next
textbox if the user moves it. Ideally, after entering
the first number, the cursor should move to the
next textbox when the user presses enter. We will
use the keypress event to do this.
We need to make the textbox a keypress event which means that the focus of the program
will shift to the next textbox when enter is pressed.
a) Double-click on the txtNum1 textbox. This opens up the code window for the textbox.
Click on the Proc drop-down list on the right of this code window and choose the Keypress
event. You will see the new Private Sub declaration as:
Private Sub txtNum1_Keypress(KeyAscii As Integer)
Notice that our typical Click event is now a Keypress event. KeyAscii is called a
parameter. A parameter is like a courier delivering information into this subroutinge. It will
deliver the key number of the key that the user presses. If that key corresponds to the enter
key, then we want the cursor to move to txtNum2. Insert the following code in this code
window:
If KeyAscii = 13 then
txtNum2.SetFocus
End If
You will learn about if, then, else in Unit 3. If the Ascii code of the key pressed is 13, which
means that it is the enter key, then Visual Basic will shift our focus to the txtNum2 button.
That is accomplished by txtNum2.SetFocus.
Before running this program, let's "Set the focus" to the Add Numbers button, because after
the 2nd number is typed, we want the user to press Add Numbers.
a) You should still be in the code window for txtNum1. If not, go back to there. Click on the
Object drop-down list on the right of the window. You will see the objects for the form.
Click on the object txtNum2. This will bring up the code window for this object so that we
can set the focus.
b) Change this event to KeyPress event. Type the
following code:
If KeyAscii = 13 Then
cmdAdd.SetFocus
End If
This will change the focus to the cmdAdd button.
c) Run the program and note how the focus changes.
d) After the numbers are added, focus should change to the Clear command button. Try to
write the code for this.
Exercise 1.2
Write a program which asks the user to enter the year they were born. The program will
calculate the person's age as of December of the current year.
You will be required to create the following and need to hand in:
IPO Chart
PDT Chart
Flowchart
Pseudocode
Some things you need to consider:
a) What object will be used to ask the question?
b) Into what object will the user type the year?
c) Do you need to display the statement "As of December 1997, you will be this old"?
What object will you use to display this?
d) What object will display the age?
e) What commands do you need buttons for?
Download