Escolar Documentos
Profissional Documentos
Cultura Documentos
Objects
Characteristics
-building blocks of Visual Basic
- Object types already created - we make
new instances
- Object types ' borrowed' from Windows
environment
- Individual entities that are somewhat
self-contained
- Can interact with other objects
- Some behavior 'built-in'
2. An object has:
Properties - individual characteristics of the
object
Events - stimuli from the user, environment,
or other objects that the object can respond
to
Methods - specialized commands that the
object can do - allows you to manipulate
3. Selected Controls
Form - abbreviation: frm
- main property: varies
- main event: load (activated whenever the
form shows up on screen)
- main uses; serves as a container for all
other controls
- background for the program
- load event used to contain code for early
in the program run
4. Sample Code:
Private Sub Form_Click()
5. Picture box
Main property: Picture
Main event: click
Abbreviation: pic
Main uses: display graphic image on screen
6. Sample Code:
Private Sub picIUPUI_Click()
'when clicked show in the label this is a
'picture box
lblLabel.Caption = "This is a Picture Box"
End Sub
7. Label
Main property: caption
Main event: click
Abbreviation: lbl
Main uses: primary form of text OUTPUT
in VB, display text on screen show text that
the user cannot directly change, works like
print command in other languages
8. Text box
Main property: text
Main event: change
Abbreviation: txt
9. Command Button
Main property: caption
Main event: click
Abbreviation: cmd
Main uses: indicated user's desire to do
something, frequently houses significant
amount of program code, graphically shows
being depressed, easily recognized as
something to click
Characteristics
-building blocks of Visual Basic
- Object types already created - we make new instances
- Object types ' borrowed' from Windows environment
- Individual entities that are somewhat self-contained
- Can interact with other objects
- Some behavior 'built-in'
An object has:
Events - stimuli from the user, environment, or other objects that the object can respond
to
Methods - specialized commands that the object can do - allows you to manipulate
Selected Controls
Sample Code:
Picture box
Abbreviation: pic
Sample Code:
Label
Abbreviation: lbl
Main uses: primary form of text OUTPUT in VB, display text on screen show text that
the user cannot directly change, works like print command in other languages
Text box
Abbreviation: txt
Main uses: primary form of text INPUT in VB, displays CHANGEABLE text on screen,
often works like INPUT command in other languages
Command Button
Abbreviation: cmd
Main uses: indicated user's desire to do something, frequently houses significant amount
of program code, graphically shows being depressed, easily recognized as something to
click
Sample Code:
Scroll bar
Main uses: primary form of INTEGER input in VB, allows user to input numeric value
graphically, range can be determined at design time, allows little opportunity for error
Sample Code:
Option button
Abbreviation: opt
Main uses: allows user to check ONLY ONE of a series of options, grouped in a form or
frame, when one is true all others are false, like a car radio
Check box
Abbreviation: chk
Main uses: allows user to check one OR MORE of a series of options, can also be
grouped, more than one can be checked at a time
outline
Properties
Some properties are specific to only one type of object (Max, Min in scroll bars)
Properties Characteristics
Set up the object so it will do what the programmer wants (Design time)
Change the characteristics of the property while the program is running (Run time)
Design time:
- as the program is being built
- uses the properties box
- All properties will be set this way when the program starts execution
Run time:
- while the program is running
- is accomplished using code
- allows properties to be changed on the fly
- allows response to environmental (user) input
Example:
MyFirst used
- design time to set up the BackColor of the picture boxes
- run time to set up the BackColor of the form
Methods
Method Characteristics
Syntax: Object.Method
Examples
Common Methods
Events
The user or environment activates events in objects, which then activates code
Example:
Read 'When the user CLICKS on the PICTURE1 control, this will happen.'
Common Events
Click, Double-click
Getting Help
Help Menu in VB
Keyword Help
Type or point to a keyword that you are having trouble with in a code window
General Declarations
This is an area that can be used to make comments about your program
Example:
'Create a clock that displays a time in a label.
'Place two horizontal scroll bars on the form,
'one that will change the backcolor of the clock label and one
'that will change the forecolor of the clock label.
Timer
- is invisible at run time
- goes 'off' at specified intervals
- used to replace loops
Timer Properties
Timer Events
Timer
- it has 'gone off'
New Properties
New Functions
QBColor
- gets a color using the Qbasic value
- uses a number between 0 and 15
- is an easy way to get most common colors
Sample Syntax
Time
Gets the current time from the system clock and returns it as a string
Sample Syntax:
lblTime.Caption = Time
or
lblTime.Caption = Time$
End
Syntax Examples
outline
Variable Description
Principal Uses
Type of Variables
Examples:
Text - fixed or dynamic length
Integers - integers or long integers
Real numbers - single and double precision
Currency - 8 digits plus four decimal places
Variant - computer takes its best guess (risky)
Explicit Declaration
Each variable is declared in a DIM statement, using a variable name and the type
Example Syntax:
- Dim UserName as String
Variable should be declared before the code using it in the appropriate code area
Can also be set by typing OPTION EXPLICIT in the General area of the form
More professional
Scope
Description
Variables are only available to those parts of the program that need it
Law Enforcement
Are available to all objects and code windows attached to the form
If the same variable name is used in a different code window, they are DIFFERENT
variables
If only one object will use the information stored in the variable, make it Object Level
If more that one object needs it, make it Form Levelmore later
Inputbox
These are easy ways to get quick input from the user
If - Then - EndIf
IF expression THEN
code
ELSE
more code
END IF
Example:
outline
Conditions
If - Then - Else
Syntax
Block form:
IF expression Then
code
ELSE
more code
END IF
Select Case
Syntax
Example
Do..Loop
DO and LOOP keywords mark the beginning and the end of the loop.
Syntax
WHILE Condition
UNTIL Condition
Loop
3 things to consider
For/Next Loop
You must know how many times you will do the task (either directly or in a variable)
Syntax:
For I = 1 to 10
MsgBox Str (I)
NEXT I
For I = 10 to 1 Step -1
MsgBox Str (I)
Next I
CLS Method
Syntax: ObjectNaame.cls
Print method
outline
Variable Arrays
A list of variables, all of the same type, with similar meaning but different values each
one having an index number
Syntax
Initialization
You, the programmer can explicitly initialize the array with assignment statements as in
the previous slide
Declaring the variable - Dim numbers (5) As Integer - would tell the computer to reserve
6 elements for an Integer array of numbers
- the value 5 defines the upper bound (highest value) of numbers
- the lower bound of numbers defaults to 0
Uses
Used when you want to be able to have multiple values for properties (e.g., the same
label used to display three different captions at different times)
Used when you are keeping track of a list or table of data (e.g., your grades)
Control Arrays
Are an array of controls of the same type and all controls in an array share the same
properties as a default (properties can be changed through code individually)
Used when several controls need to do the same thing (e.g., color box)
Copy the object, paste it - a dialog box will ask you if you want to create an array
Click "YES" and move the new object wherever you want it. Continue this process until
you have all you need.
Constants
Uses of a constant
Example: PI = 3.1415927
Example: NumPics = 3
Additional uses
If the programmer updates the program to handle more frames, he only has to change the
constant definition - this saves time and makes the program easier to read
Syntax
Image boxes
Are a control to handle pictures and the property of the picture determines what image is
shown
The picture can be copied to the clipboard, loaded from a disk file or copied from another
object
The image box automatically resizes itself to the picture size and can be used to scale
pictures
Special property
The image box has a special property - visible that is used to hide pictures "off stage"
Multitasking is the ability to run multiple program simultaneously on the same processor
Visual Basic has limited drawing features of its own but allows use of Windows Opaint
or other specialized drawing programs
It also allows the programmer to define icons, sound effects, and other elements using
specialized tools
outline
Designing Menus
Menus should follow Windows standards (be along the top of the screen)
Have an option for the user to have quick access to an item (alt + underlined letter, i.e.,
File), also known as shortcut keys
They are attached to the form and each menu item is a mini control with only a few
properties
The component "CommonDialog" must be added to the list of tools by choosing Projects
from the menu, then Components, and select the Common Dialog option from the list of
items available
The 'Menu Editor' is located on the horizontal toolbar on the left-hand side
This allows you to create an outline of the menu structure, change properties of the menu
items
The user will see what the developer places in the caption area
Placing an & in front of a letter indicates that the next letter will be underlined and used
for quick access (&File will show File)
Name is like a control name and should use mnuFile naming convention, each menu item
MUST have a name
The Shortcut
This allows you to assign a Control combination or function key to the menu item
Checked: tells whether there is a check mark placed next to the item (TRUE or FALSE)
Enabled shows whether the user can access the item (TRUE or FALSE)
Once you have your menu designed, it will appear at the top of your form, all you will
need to do is click on the menu item's name to access the code window for the name you
have chosen
Items are usually 2-3 items deep, but can be deeper and usually less than 7-10 choices per
menu
When possible, the first letter should be underlined and be related to a shortcut key
Common Dialogs
Many commands are used in various types of programs (Save, Open, Font, etc.)
The Common Dialog Box Control works very differently from most controls
The control is NOT visible on the form (similar to the Timer - it's there but you don't see
it)
Example:
CmDialog1.Action = 3
CmDialog.ShowColor
Same control can 'call' any of the dialogs depending on the value of the action property
Action
1
2
3
4
Type of box
Open
Save
Color
Font
Methods
The common dialog control actions can also be controlled through methods. Many
programmers find these easier to remember than use rather than the action property
Example:
CmDialog1.Action = 1
is exactly the same as
CmDialog1.ShowOpen
Filter - this is used in file dialogs to determine how dialog will isolate files
This is usually located on the backslash key, sometimes it looks like a broken vertical line
The properties of a common dialog are used for communication between the user and the
programmer
They bring up dialog boxes for the user to select these things and they store the user's
choice in the properties of the common dialog object
outline
File Handling
Data is kept in FILES on the disk and a program can read and write information from or
to these files.
These work like a cassette tape or VHS tape. You start at the beginning of the tape and
listen or watch from beginning to end (if it is a good tape or movie)
This type of file access is useful when a flexible structure is more important than quick
access
INPUT: reads information from an existing file (retrieving existing file for input)
OUTPUT: creates a new file, adds information to it, overwrites with new information to
an existing file
Choosing a method
Usually OUTPUT is used only when creating a new file as it will destroy existing files
with the same name
The filenumber
This is unique for each file opened - it tells what file number you want to access
Before you can write to the file, you must have opened the file via OPEN
(Output/Append)
The Syntax
The FileNumber must match the file number on the OPEN statement
This is used to get data from a file and the file must have been opened via the OPEN
statement (Input)
Where the FileNumber is a valid, open file and variables separated by commas
This statement is used to close the file and release its filenumber
Is TRUE if the program has reached the end of the file and is usually used in DO LOOPS
with file handling
EOF (FileNumber)
Example:
DO
..program code
LOOP UNTIL EOF(1)
NOT operator
This sets a Boolean to its opposite value and is commonly used for check boxes and
Boolean properties
Example:
Text1.FontUnderlined = NOT Text1.FontUnderlined
If Font Underlined is true, it will become false, if false it will become true
SETFOCUS method
This method is used in conjunction with an object and that object receives the focus.
Example:
Text1.SetFocus
By doing this the insertion cursor will appear in the text box.
This is used when you anticipate an error in a procedure, especially in file handling
outline
Modules
Description of a module:
- they are like separate files on a disk
- they are like the code part of a form - no objects are attached
- are saved on the disk with a .BAS extension
Global procedures
Because they are global, they can be called from any form, any procedure in the project
Are created by choosing New Module from the File menu or hitting New Module button
Scope map
Global procedures
Syntax
Parameters
Multiple forms
To create use New Form on the File menu or New Form button
Show
Syntax: FormName.Show 1
The FormName is the name of the form you have created in the project
Example
Second.Show 1
This loads Second into memory if it's not there and shows it to the user
Modal switch
If you end the show method with a 1, the form will be modal
The next code that is activated is in the form_load procedure of the second form
The control reverts to the original form after the second is closed
If you do not put a 1 at the end, the form shows up, but does not get the focus
The control of the program moves on to the next line of code in the first form
Hide
Syntax: FormName.Hide
Example: Second.Hide
This hides the named form and the control reverts back to the procedure that showed the
form
Startup form
To change this, use the Project dialog under the Options Menu
outline
Databases
Random Access
Individual pieces of data may be referenced without having to process sequentially all of
the information that precedes that data in the file.
The simplest is to require that all records in a file are of the same fixed length
Using fixed length makes it easy for a program to calculate the exact location of any
record relative to the beginning of the file
Data can be inserted, updated or deleted and each random-access file will have one of
these three access types
TYPE newType
var1 as datatype
var2 as datatype
var3 as datatype
END TYPE
GLOBAL variable AS newType
Description
This is NOT a variable. It is a new variable TYPE (like string, integer, etc.)
Example
Type AddressType
Name as String * 30
Phone as String * 15
Add as String * 50
End Type
Global Address as AddressType
Files (description)
OPEN statement
Example:
OPEN "A:\Address.dat" for Random as #1 LEN = LEN (Address)
The same open statement is used for reading, writing, and appending.
PUT statement
The filename must have been opened with an OPEN for RANDOM command
Syntax:
Example:
to place the current contents of the address variable to the 4th position in the file:
PUT #1, 4, Address
To place the current contents of the address variable in the position in the file contained
in the recNum variable:
PUT #1, recNum, Address
GET statement
Identical to the PUT statement except it transfers information FROM the database TO a
variable
Filename must have been opened with an OPEN for RANDOM command
Syntax:
Note: Filename must have been opened with an OPEN for RANDOM command
Example:
To place the current contents of the 4th position in the file to the address variable
GET #1, 4, Address
Both require an OPEN statement before use and a CLOSE statement after
Sub GetAddress
OPEN FileName for Random as #1 Len = Len(Address)
Get #1, RecNum, Address
Close #1
TxtName.text = Address.Name
TxtPhone.text = Address.Phone
TxtAdd.text = Address.Add
End Sub
Notes:
The previous code assumes access to variables FileName, RecNum and Address (the
Record Variable)
Notice that we first GOT the variable from the file, then copied it to the form
PUT procedure
Parallel Arrays
Loads all records into a record array (record variable) or a series of arrays in memory
Only allows a number of records that will fit into memory at one time
Linked Lists
There is a Pointer field containing the record number of the next/previous record in the
field
Double-linked lists
Binary tree
Indexing
A separate file or array is maintained comparing the record number to the position in a
sorted list
This can be easily created with sorted list box technique in Visual Basic
Relational databases
Allows flexibility
Sorting algorithms
outline
Purpose of the class
Who is it for?
Programming essentials
Specific languages
Emphasis on concepts
Practical applications
- educational
- scientific
- business settings
Why program?
History
Usefulness now
Engineer analogy
Hardware information
Disk
- floppy 1.44 meg
- you may want to have 3 available - one to turn in, two to work on
Computer requirements
ools
lgorithm
mplementation
efinement
History of programming
Sumerian Algorithms
Abu Ja'far Muhammad ibn-Musa Al-Khowarizimi (algorithm is derived from the last
name)
Pascaline 1659
Lady Ada
Windows-general characteristics
Programming differences-DOS
Procedural program design, programmer directly responsible for all device control
Programming differences-WINDOWS
User in control
Menu bar, Tool bar, Tool box, Project window, Properties box
Naming a form
Click on the NAME property to select it (scroll through the list to locate)
Notice change in the properties box - caption of form has not changed
Use the same procedure to change the form's CAPTION property to {your name}'s First
VB Program
Examine other properties of the form, but do not change them randomly
Picture box
Some concepts
Variable = value
NOT equality
Look at the default name given and ALWAYS use the same extension
Press the Play button on the toolbar - you will see your program running