CSCI 220
Topic: Computer Programming
Visual Basic
Week 6: Arrays and Images
Variable Arrays
Definition
List of variables
All of same type, can be any valid variable
Similar meaning
Different values
Indexed
Example
Putt-Putt scores
Related items
Dim Statement to declare
Use of parentheses
Bounds versus index
Index versus value
Frequently used in for-next loop
Upper bound is frequently a constant
Used when:
You want to be able to have multiple values for
properties
(EG use the same label for three different captions)
You are keeping track of a list or table of data
(EG your grades)
Any time multiple values might be used
Control Arrays
Conceptually similar to variable arrays
Array of controls of the same type
All controls in an array share the same properties as a
default
(although properties can be changed through code
individually)
All controls in an array share the same code box.
All events related to a control array automatically pass
an Index parameter
Used when:
Several controls need to do the same thing (eg color
box)
You want to store images off-screen for quick access
Creating them
Create the first object in the regular way
set properties of this object ESPECIALLY name.
Copy object
Paste it.
Dialog box will ask if you want to create an array.
This time say YES.
Move new object wherever you want it.
Continue pasting until you have all you need.
Image boxes
Control designed to handle pictures.
Picture property determines what image is shown
Picture can be copied from the clipboard, loaded from a
disk file, or copied from another object
Automatically resizes itself to picture.
Can be used to scale pictures.
Visible property used to hide pictures "off stage"
Syntax:
ControlName.Visible = True or False
True means control is visible (default for most
controls)
False means control is invisible
Constants
Treated much like variables in code except:
Value of constant cannot change during the execution of
the program.
Constant cannot be given a new value
Uses
To name a number so it is easier to use in code (EX:
Pi = 3.1415927)
To refer to a commonly occurring value in a program
(Ex: NumPics = 3)
If programmer updates program to handle more frames,
She only has to change the constant definition.
Saves time, makes program easier to read.
Syntax:
CONST Identifier = Value
Multitasking as a programming tool
Definition:
The ability to run multiple program simultaneously on
the same processor
Uses:
Commonly used when defining graphic elements of a VB
program.
VB has limited Drawing features of its own
allows use of Windows Paintbrush, other specialized
drawing programs
Also lets programmer define icons, sound effects, other
elements using specialized tools.
How to do it:
SAVE YOUR PROGRAM!!!!! Memory errors are possible
(likely) with multitasking.
Get to the program manager (double click on its icon)
Choose the specialty program (Paintbrush)
Cutting and pasting across programs
Create a picture in paintbrush
Use the selection tool to select that picture
Choose COPY from the EDIT menu
Get back to VB
Select the control you want to store the image in
Choose PASTE from the EDIT menu
VB6 - Array demo
The purpose of this program is to demonstrate use of
arrays for variables and controls.
The user will see a command button, an image area,
and a caption area. When the user clicks on the
button, a picture shows up in the image area, and a
related caption appears under the picture. When the
user presses the button again, a new picture with a
corresponding caption appears on the form. After
the last picture appears, the cycle repeats. There
should be at least 3 pictures.
Strategy:
We will create a standard image box and label for
the user to see, but rather than directly storing
the images and phrases in those controls, we will
use arrays. The images will be copied into a
control array of picture boxes, which will be hidden
from the user during program execution. The phrases
will be stored in a string array. Both arrays will
have the same upper bound (3) and will use the same
form-level variable(PicNum) for display control.
When the user clicks on the command button, the
value of PicNum will be incremented, and the PicNum
values of the two arrays will be displayed. (EG if
PicNum is 2, the user will see image #2 and phrase
#2 If PicNum gets larger than the upper bound of
the arrays, we should reset it to the lower bound.
The lower bound will be one, and we will set the
upper bound via a constant called NumPics. (Number
of pictures)
Pseudocode:
Create the form as shown in the diagram above.
Note: You will need to change the default indexes
on the ImgStore array
Create simple pictures in PAINTBRUSH, copy them to
ImgStore boxes
Declare variables in general declarations area:
NumPics is a constant containing the value 3
PicNum is an integer that represents picture
number
Make a 3 element string array called Phrase. Use
NumPics in the declaration rather than 3
Initialize variables in form_Load
Dimension I as a local utility integer
Use for/Next loop to set Visible property of all
elements of ImgStore array to false
Again use NumPics as the upper limit of the
for/next loop
Set the values of the phrase array elements
Example:
Phrase(1) = "This is your brain"
Each phrase should be the caption for the
corresponding image
Attach code to "CLICK HERE" button:
Increment PicNum by 1
If PicNum is greater than NumPics make picNum
equal to 1
Set the picture property of ImgDisplay equal to
that in ImgStore(PicNum)
Set the Caption property of LblPhrase equal to
Phrase(PicNum)
Test, debug, and save (VB6.Mak, ArrayDmo.Frm)
Return to the syllabus