irwin/mcgraw-hill © the mcgraw-hill companies, inc., 1999 2-1 visual basic projects project...
TRANSCRIPT
2-1
© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill
Visual Basic ProjectsProject Structure and VB’s Programming Tools
chapter TWO
2-2
© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill
Visual Basic ProjectsProject Structure and VB’s Programming Tools
After studying this topic you should be able to:
Design and construct simple complete applications from scratch.
Explain the structure of VB projects.List the characteristics of several VB controls.Identify the purpose of each major component of the
VB environment.Explain basic programming practices that
contribute to the readability of programs.
2-3
© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill
Designmode
Runmode
BreakmodeProgrammer
Project
Visual Basic runs under the Windows operating system.
The programmer uses VB to create the application. The application under construction in VB is called a “project.”
2-4
© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill
The programmer periodically saves the project to disk.
Designmode
Runmode
BreakmodeProgrammer
Project
2-5
© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill
The programmer periodically saves the project to disk.
The completed project is made into an “executable file,” which VB stores on disk. This file contains the finished application program.
Designmode
Runmode
BreakmodeProgrammer
Project
2-6
© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill
The programmer periodically saves the project to disk.
The completed project is made into an “executable file,” which VB stores on disk. This file contains the finished application program.
The application is delivered to the user.
Designmode
Runmode
BreakmodeProgrammer
User
Project
Finished Application
2-7
© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill
START
Programmer
User
Finished Application
The application program runs under the Windows operating system independently of VB.
2-8
© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill
The application program runs under the Windows operating system independently of VB.
The executable file cannot be modified; if changes to the application program are required, the programmer must use VB to open the project files, modify the project, and then make a new executable file.
START
Programmer
User
Finished Application
2-9
© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill
The VB environment
Menu bar
Tool bar
Project Explorer window
Properties window
Form Layout window
Form Designer window
Form
Code window
Toolbox
The VB environmentThe VB environment
2-10
© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill
The Properties window
Object box
Currently selected control is named CCCommand1
Sort tabs
Property list
The setting for Command1’s Caption property is Command1
Description pane
2-11
© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill
The Code window
Object box
Procedures/ Events box
Code areaFull Module View button
Procedure View button
Margin indicator bar
2-12
© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill
Break button switches from run mode to break mode.
End button switches to design mode.
Designmode
Start button switches to run mode.
Runmode
Breakmode
Toolbar buttons for switching VB’s mode
2-13
© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill
The Project Explorer Window
View Code buttonView Object button
Toggle Folders button
Forms, code modules, class modules and resource files belonging to the project
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-14
Structure of VB projects
Project
* The structure of class modules has been omitted for clarity.
Generaldeclarations
section
Forms
Generalsub
procedures
Controls
Properties Eventprocedures
1+
10+ 0+
1+ 0+
Code modules
Generaldeclarations
sectionGeneral
subprocedures
0+
0+1
Class modules*
0+ Resource file
0 or 1
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-15
Structure of VB projects
Project
Forms
Generaldeclarations
sectionGeneral
subprocedures
Controls
Properties Eventprocedures
0+
10+ 1+
1+ 0+
Displayed “windows”
User interface object
Instructions to the computer
The definition of what isincluded in the program.
Code
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-16
Visual Basic Modes• Design
– where most of our time will be spent – Form, the tool bar and tool box are available– Properties window is available– Code window is available
• Run - where we will test & evaluate the program
• Break - where we can debug our problems
• Compile– The program we will give to the user.
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-17
Form frm 53 Properties, 31 Events, 21 Methods
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-18
53 Properties Form Properties
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-19
FormEvents
31 Events
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-20
Form methods 21 Methods
• Circle, Line, Point
• Cls, Hide, Show, Refresh
• Popup menu
• Drag, DragOver
• Move left, [top, [width, [height]]]
• SetFocus, GotFocus, LostFocus
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-21
Properties of Forms
• Caption - Name at the top of the form
• BackColor - Systems colors or Palette
• WindowState - 0 = Just like design(Default)1 = Minimized, 2 = Maximized
• Name used in code also is default file name
• Icon becomes the default exe icon
• Form1.BackColor = vbRed
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-22
Controls7 general categories
• Trigger 3 Timer, Command button
• Input 6 TextBox, Combo, Scroll
• Output 4 Label, Textbox, Image
• Organize 3 Form, Frame
• Graphic 2 Line, Circle
• Data Access 4 Combo, Data
• Integration 1 OLE
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-23
Procedure vs Event programming
• Procedures ... the old way, it starts at the top and runs to the bottom every time - how a traffic light works, cars or no cars
• Event new way store clerks - demand– The program waits, then reacts to some event. – Reacts differently to different events.– Event procedure is the detail code for the
computer to follow
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-24
Events
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-25
Processing code for Command Button
• Note: indentation
2-26
© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill
Command1
Instructions . . .
Instructions . . .
When the user clicks on the command button with the mouse, the computer executes the command button’s Click event procedure.
Command1’s Click event procedure
When the user moves the mouse while the
mouse pointer is positioned over the
command button, the computer executes the
command button’s MouseMove event
procedure.
Command1’s MouseMove event
procedure
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-27
Stopping Visual Basic
• Include an Exit key in every project
• Ctr + Break will stop processing
• Run menu Square
• Alt + R Gets run menu - choose E
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-28
Naming objects
• When projects get big (Project 2 Page 55 uses 14 labels) naming objects with meaningful names is very helpful when writing code and provides documentation.
• Not all objects need to be re-named
• All object names should use the three letter abbreviation of the object type
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-29
File Names
• Windows allows 256 characters – Except only ~ ? * :– But in DOS mode only 1st 6 letters are used– There is an extension
Form including controls and code .FRM Changes to controls on the
form .FRX Project listing forms and others files .VBP
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-30
Standard File Extensions
• VBP VB Project• FRM VB Form• VBW VB Form info • VBX controls on form• BAS VB Module• CLS VB Class• TXT Generic text• EXE Executable
• DAT Data • LOG Recorded event• COM Commands• BAT Batch • BIN Binary • BAK Backup• JPG Graphics• SYS System
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-31
Expanded VP program parts
• Projects - grouping of forms & files & code
• One and only one project .vbp
• Zero or more Forms - usually one .frm– modification of controls on the form .vbw
• Zero or more Code modules -often zero .bas
• Zero or more Class modules -often zero .cls
• Zero or more Resource files -often zero .res
Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999
2-32
Stop Watch Lab Exercise
• Chapter 2 Programming Problem 3– Identify user requirement– Design the application– Construct the application