Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project...

32
2-1 © The McGraw-Hill Companies, Inc., 1999 Irwin/McGraw-Hill Visual Basic Projects Project Structure and VB’s Programming Tools chapte r TWO

Transcript of Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project...

Page 1: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

2-1

© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill

Visual Basic ProjectsProject Structure and VB’s Programming Tools

chapter TWO

Page 2: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project 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.

Page 3: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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.”

Page 4: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

2-4

© The McGraw-Hill Companies, Inc., 1999Irwin/McGraw-Hill

The programmer periodically saves the project to disk.

Designmode

Runmode

BreakmodeProgrammer

Project

Page 5: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 6: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 7: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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.

Page 8: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 9: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 10: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 11: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 12: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 13: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 14: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 15: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 16: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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.

Page 17: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999

2-17

Form frm 53 Properties, 31 Events, 21 Methods

Page 18: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999

2-18

53 Properties Form Properties

Page 19: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999

2-19

FormEvents

31 Events

Page 20: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 21: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 22: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 23: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 24: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999

2-24

Events

Page 25: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999

2-25

Processing code for Command Button

• Note: indentation

Page 26: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 27: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 28: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 29: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 30: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 31: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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

Page 32: Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 2-1 Visual Basic Projects Project Structure and VB’s Programming Tools chapter TWO.

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