session 1 - introduction to visual basic 6.0

26
 Introduction to Visual Basic 6.0 The Visual Basic Desktop

Upload: owe-sagum

Post on 05-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 1/26

 

Introduction to Visual Basic 6.0

The Visual Basic Desktop

Page 2: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 2/26

 

Program Development Cycle

1. Decide what you want the computer to do.

2. Decide how you want your program to look:

Interface design (drawing buttons, forms etc.)

3. Define the properties of these objects:

name, colour, size and appearance.

4. Write and attach VB code (event procedures) to each object.

5. Run and test your program.

Page 3: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 3/26

 

Program Development Cycle

• The code responds to the user interacting with the program

through various events:

• moving the mouse, clicking the mouse or pressing a

key.

• Design before you code.

• Have a clear idea of what you want your program to do

 before going to the PC.

Page 4: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 4/26

 

Toolbox Pull-down menus Tool bar Form

Properties window

Project window

Form layout windowImmediate window

The VB Desktop

Page 5: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 5/26

 

The VB Desktop

•There are 8 main parts to the VB desktop:

1. Pull-down menus - provides access to commands used to

 build application.

2. Toolbar - provides quick access to commonly usedcommands in the programming environment.

3. Toolbox - provides a set of tools used at design time to

 place controls on a form.

4. Project explorer - lists all the files that make up a singleVB program.

Page 6: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 6/26

 

The VB Desktop

5. Properties window - used to set the properties of forms and

controls at design time.

6. Form layout window - enables you to arrange the location

of where your forms appear on the screen.

7. Form - provides a window where you can draw objects for 

the user interface.

8. Immediate window - enables you to debug your VB

 program.

Page 7: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 7/26

 

Pointer 

Picture BoxLabel

Frame

Check Box

Combo Box

Horizontal Scroll bar 

Timer 

Directory list box

Shape

Image

Text Box

Command Button

Option Button

List Box

Vertical Scroll Bar 

Drive List Box

File List Box

Line

Data Control

OLE

Page 8: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 8/26

 

The VB Toolbox

• This is used to draw the objects on the user interface. Objects

include:

• Pointer - used to select and edit objects.

• Picture box - display device which can contain bit-mapped pictures, text and line drawings.

• Label - used to display text that cannot be changed.

• Text box - a text input device which accepts keyboard input and

supports editing.• Frame - an object which allows other controls to be arranged in

logical groups.

Page 9: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 9/26

 

The VB Toolbox

• Command button - operates like a push button, which is pressed

 by clicking the mouse on it.

• Check box - displays an on or off value. Useful to display or enter 

data which can be one of two values.• Option button - used in a group of similar controls to select

 between a number of mutually exclusive options.

• Combo box - can be used to enter or select data. It is essentially a

single text box attached to a list. Can be used to input data or select existing data.

• List box - just like the list part of a combo box.

Page 10: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 10/26

 

The VB Toolbox

• Horizontal Scroll Bar - control used to set a position or a level of 

a quantity.

• Vertical Scroll bar - as above.

• Timer - operates by running a piece of program code at pre-setintervals.

• Drive list box - used to select and display from a range of disk 

drives.

• Directory list box - used to display files in a specified directory.• File list box - used to select a list of files by their attributes.

Page 11: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 11/26

 

The VB Toolbox

• Shape - used to add objects such as circles, rectangles

and rounded boxes to a form.

• Lines - lines, like graphics can be used to embellish a

form.

• Image - similar to picture box with less events. Used to

display bitmaps.

• Data control - used to access DBs.

• OLE - used to access other windows programs.

Page 12: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 12/26

 

Creating Applications in VB

• A VB interface consists of  forms and objects.

• A form is a window that appears on the screen.

• Most programs have at least one form, although it is

common to have many.

• Objects are items that appear on forms.

• Objects enable the program to interact with the user.

Page 13: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 13/26

 

Creating Applications in VB

• The whole purpose of VB code is to tell objects what to do

when the user does something i.e. Event-driven.

• Any time a user presses a key, moves the mouse, or clicks

the mouse button, such an action is called an event.

• Whenever an event occurs VB commands tell the computer 

that something has just happened.

Page 14: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 14/26

 

Objects

• Each control has a known set of characteristics:

•  properties that define the object’s appearance and

 behaviour.

• methods that the program can call to perform specific

actions on the object.

• events that can occur around the object, trigerring

automatic calls to specific event procedures in the code.

Page 15: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 15/26

 

Objects

OBJECTSOBJECTS

Properties

locations, colour,

values

Methods

move, clear,

 show

Events

 MouseMove,

 KeyPress

Page 16: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 16/26

 

Managing Projects

• The first form in a project is called the startup form.

• The program loads this form first and executes the code in the

Form_Load event.

• The statement that stops a program when it is running is the End statement.

• The following code would be attached to an exit button.

Sub cmdExit_Click()End

End Sub

Page 17: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 17/26

 

Defining Properties

• After a form is created and

some objects are drawn, the

next step is to define the

 properties.

• An object’s propertiesdetermine the object’s

• name, colour, size and

general appearance.

• Once an object is placed on aform VB assigns default

 properties - these should

always be customised.

Page 18: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 18/26

 

Properties

• To access a property you can write the object name, a dot

and then the name of the property:

• From1.Backcolor = 0

• Text1.Text = “Hello”

• Label1.Caption = Text1.Text

• To find out what properties an object has you can highlight

the object on the screen and press the F1 Key.

• Alternatively use the HELP.

Page 19: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 19/26

 

Properties

• Properties can be set at design time using the property

window.

• Properties can be set at Run Time using program code.

• The outside world changes properties automatically.

Page 20: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 20/26

 

• Planning a VB program is focused on events expected to

take place while the program is running.

• For each object in VB there is a list of pre-defined events to

which the object can respond.

• For example command buttons have several events:-

• Click 

• MouseDown

• MouseUp

• MouseMove

• These can all be viewed in the code window once the object

has been selected.

Events

Page 21: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 21/26

 

Events

• Most events correspond to actions that may be performed by

the person who is using the program/application.

• e.g. the user clicks a button, selects an option etc.

• all events are formally defined and recognised by VB.

• The application code contains blocks of code, known as event

 procedures.

• These procedures are designed to respond to specific events.

Private Sub cmdMessage_Click()

End Sub

Object name

 Event 

 Event 

 procedure

Page 22: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 22/26

 

Methods

• A method is a built-in procedure that performs an operation

on a specific control.

• For example, list boxes have methods called AddItem,

RemoveItem, and Clear, for maintaining list contents.

• For example:

If KeyAscii = 13 Then

List1.AddItem (Text1.Text)

  End If 

 Method 

Page 23: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 23/26

 

Properties, Events and Methods

• To summarise:

• Properties are items of information that describe a

 particular object.

• Methods are built-in procedures that take some action on

an object.

• Events are user actions for which customised procedures

may be written.

Page 24: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 24/26

 

Control Object Naming conventions

• Without assigning names to objects in large programs their 

meanings can often be confusing e.g. Command1,

Command2 etc.

• Microsoft have produced the following convention to

reduce confusion and promote standardisation.

Object Prefix Example

Form frm frmFileOpen

Check Box chk chkReadOnly

Combo Box cbo cboEnglish

Command Button cmd cmdExit

Data dat dataBiblio

Directory List Box dir dirSource

Drive List Box drv drvTarget

File List Box fil filSource

Frame fra fraLanguage

Grid grd grdPrices

Horizontal Scroll Bar hsb hsbVolume

Page 25: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 25/26

 

Writing code for applications

• The code window is where you will write the VB code for 

you application.

• To enter the window double click anywhere on the form.

• The code window includes the following elements:

• object box - displays the name of the selected object.

•  procedure box - lists the procedures (events) for an object.

Page 26: Session 1 - Introduction to Visual Basic 6.0

7/31/2019 Session 1 - Introduction to Visual Basic 6.0

http://slidepdf.com/reader/full/session-1-introduction-to-visual-basic-60 26/26

 

Writing code for applications

• Each object must have code associated with it and an event

(e.g. mouse click).

• Select the required event for the object from the procedure

 box.

• Each object has the following event procedure structure:-

Sub cmdRectangle_click()

??????

??????

End Sub

This is where

the code is written

This is created 

automatically

 Name of object   Event