• No results found

Lec02.ppt

N/A
N/A
Protected

Academic year: 2020

Share "Lec02.ppt"

Copied!
28
0
0

Loading.... (view fulltext now)

Full text

(1)

Introduction to Visual Basic (VB)

(2)

Topics

• 1. What is VB?

• 2. What is Event-Driven?

• 3. What is Object-Orientation? • 4. Objects used in VB

• 5. VB objects naming practice • 6. Files in VB

• 7. OOED Programming process • 8. VB statements

(3)

What is VB?

• A windows-based system using objects

responding to events

(4)

Event-Driven

• Procedural languages – run from start to finish with no human intervention

– Basic, COBOL, FORTRAN, C

• Event driven languages - waits for an event to occur before taking any action

• Example of event

– The press of a key on the keyboard – Movement of the mouse

– The click of a mouse button

(5)

Objects

• Reusable software components that model

items in the real world

– e.g. GPA calculator, Tax calculator

• They are self-contained modules that

(6)

Encapsulation

• The capability of an object to hide its

internal workings from other

objects

.

• In VB, programmers does not need to know

what is going on inside the object, but only

need to know how to work with the object’s

properties and methods

(7)

Object-Oriented Event-Driven

Programming (OOED)

• OOED uses objects in the program and runs

only after the

Events

occur

• OOED is easier to work with

• Users can combine multiple objects to

(8)

Starting VB from your computer

• Toolbox

• Project Window useful icons

• Properties windows

• How to Add Controls

• How to Add Code

(9)

VB and IDEs

• The VB environment can be used to create and execute programs. • After creating a program, it is necessary to run or execute it to use it.

• Programs can be executed within the VB environment or created as executable files to be run outside of the environment.

• VB uses the term ‘project’ as an analogy to ‘program’

• The VB integrated development environment (IDE) consist of a number of elements:

(10)

The VB IDE

Toolbox Pull-down menus Tool bar Form

Properties window

Project window

(11)

1

st

Project (ShowName)

• A Form

• Three Command Buttons

– Show Name – Click Me – Exit

(12)

Forms and Controls as Objects

Forms

and

Controls

are two kinds of

objects you may use in VB

• A

form

is a virtual blank space to design

the user interface for a VB application

• The tools that you use to construct the user

interface are

controls

(13)

Some popular components

• Textboxes, buttons, and labels are the most popular components. Textboxes hold text - often user input. • Labels are for labeling other components, usually. • Buttons can be “pressed” to fire an event.

• Picture boxes can “hold” images

• comboboxes allow multiple choices.

• Listboxes are like multi-line textboxes, (Textboxes can also be set to be multiline).

(14)

Examples of Useful Events

• Form

– Form_Load( ) : used to initialise values in a form before it appears on screen

– Order of all Form Events:

Form_Initialize, Form_Load, Form_Resize, Form_Activate, Form_GotFocus, Form_Unload, Form_Terminate etc

• Command Button

– Command_Click( ) : used to execute statements whenever the button is clicked

• - Other Command Events:

(15)

VB objects naming practice:

• Start the name with a standard object abbreviation

– cmd = command button, txt = text box, frm = form.

• Finish the name with a descriptive word of the objects

purpose

• Spaces and special characters are not allowed in an

objects name. (E.g. cmdCancel)

• Examples might be:

(16)

Features of Objects

• The programmer can manipulate the object

through the use of three key object features:

(17)

Object Properties

• A property is a named attribute of an object.

• Using an analogy to English grammar, if an object is thought of as a noun, then a property may be

thought of as an adjective.

• Used to change the appearance of objects.

• An example of the relationship between objects and properties using an everyday object:

(18)

Setting Values of Properties

• During

design time

, properties may be set

in the Properties Window.

– You are in “Design Time” when you are designing the project and adding code

• Some properties may be set or modified

during

run time

.

(19)

Object Methods

• A set of predefined activities that an object can carry out. • The syntax for using an objects method is:

– object.method

• A method is a verb that can be carried out by the object.

• For the various VB objects, there are usually several methods already available.

(20)

Methods Example

• Real life example

– dog.eat – dog.bark – dog.run

• A Visual Basic example

(21)

Object Event

• An action taken by the object when notified by a message

• Or, user actions taken on the object that provokes a response from the object.

• Examples

– Soccer ball: kicking, throwing, holding etc. – Cat: feeding, hitting, calling etc.

– mouse click, form load, or key press. • VB example

Private Sub cmdsubmit_Click() …

(22)

Add graphic

• Retrieve your 1

st

project from your disk

• Use image control

• Select a graphic

• Change the size of graphic

Stretch property of image control

(23)

OOED Programming Process

• A six step process for writing an OOED computer program:

1. Define problem. 2. Create interface

3. Develop logic for action objects

4. Write and test code for action objects 5. Test overall project

(24)

More Practice

(step 1 to 3 only)

1. Circle problem revisit

I: radius

P: calculate circumference, calculate area – O: radius, circumference, area

2. Payroll problem revisit

– I: employee name, pay rate, and hours worked – P: calculate gross pay

(25)

Comments

• To explain the purpose of a program, or a statement, a comment statement is added

– For yourself and others

• Any statement beginning with an apostrophe or REM is a comment

(26)

VB Statements

• A statement is a reserved word

• Statements are instructions that are built into the Visual Basic language

• Some examples are: End

Option Explicit Private Sub

(27)

Visual Basic Help

• There are three types of Help:

– Help menu option

– context-sensitive help – and Auto Help

• In VB 6, Help uses the familiar Internet

Explorer browser interface for the first

two types of help.

(28)

Context-Sensitive and Auto

Help

• With context-sensitive help, pressing the

F1 key provides help on whatever item the

cursor is located.

• With Auto Help, VB tries to help you with

a code statement by providing:

– A list of items to complete the statement – Info on the statement you have started

References

Related documents