CP2028 Visual Basic Programming 2

Post on 31-Dec-2015

31 views 0 download

description

CP2028 Visual Basic Programming 2. Week 1 Lecture 1 & 2 Introduction to the module The VB module set Contents and structure of this module Review of the Visual Basic environment Review of programming practices Variables & Scope. CP2028 Module Aims and Objectives. - PowerPoint PPT Presentation

Transcript of CP2028 Visual Basic Programming 2

Week 1 Lecture 1 Slide 1CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

CP2028Visual Basic Programming 2

Week 1 Lecture 1 & 2– Introduction to the module

The VB module set Contents and structure of this module

– Review of the Visual Basic environment– Review of programming practices

– Variables & Scope

Week 1 Lecture 1 Slide 2CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

CP2028 Module Aims and Objectives

Reinforce the skills and knowledge gained in VB1.

Further develop programming abilities. Topics covered include:

– storing and accessing information in files– using Databases within VB.– File handling– error handling

Week 1 Lecture 1 Slide 3CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

The Visual Basic module set.

CP1007VB1

CP2028VB2

CP3013App Dev in VB

CP2030VB For C++

CP1000S.P. in C++

Week 1 Lecture 1 Slide 4CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Introduction to the Module [1]The Module Guide

The module consists of 12 weeks of study, plus a revision week.

As in VB1 the aim is to increase your skills and knowledge in program design and development.

Visual Basic is the target language, but the skills gained are applicable to other languages.

Week 1 Lecture 1 Slide 5CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Introduction to the Module [2]

Timetable details (see module guide)– 2 x 1-hour lectures per week– Tutorial– Workshop

Weekly contents

Assessments– Coursework– Exam

Week 1 Lecture 1 Slide 6CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Review of V.B. Environment

Identify the components of the VB design environment?

?

?

?

?

?

Week 1 Lecture 1 Slide 7CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Review of V.B. Environment

Identify the components of the VB design environment?

Code

MenuBar

?

Week 1 Lecture 1 Slide 8CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Review of V.B. Environment

Identify the components of the VB design environment?

?

?

?

?

?

Week 1 Lecture 1 Slide 9CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Review of V.B. Environment

Identify the components of the VB design environment?

Form

MenuBar

Control Toolbox

Project Window

PropertiesWindow

Week 1 Lecture 1 Slide 10CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Visual Basic Program Structure.

Project File– ‘.VBP’

Form Files– ‘.FRM’– ‘.FRX’

Modules– ‘.BAS’

Custom Controls– ‘.VBX’ files

Week 1 Lecture 1 Slide 11CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Control Bar - How many controls can you identify?

StandardEdition

ProfessionalEdition

Week 1 Lecture 1 Slide 12CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Control bar - Properties

Name

Run Time Vs Design Time (Startup Defaults)

Setting control positions when placeon form Vs via the properties window

What if we want to resize our formsdo we resize controls? How? Are there tools to do it?

Week 1 Lecture 1 Slide 13CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Review of Event Driven Programming

Application program is composed of a number of subroutines, which are triggered by events within the environment.

– Typical events include Mouse-click Keyboard use Field value changes

Events happen to a control.– Mostly user generated events– Controls can also cause events

Week 1 Lecture 1 Slide 14CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Visual Basic Event Processing

Trigger Event

Code Executed

Week 1 Lecture 1 Slide 15CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Types of Events

Events can be classified as:

User generated– (e.g. command button click)

Computer generated– (e.g. specific time elapsed, from a timer control)

Program generated– (i.e. program explicitly generates an event from within the code)

Week 1 Lecture 1 Slide 16CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Visual Basic Events

The events that can happen to a control are pre-determined

Each type of control has a relevant set of events

The events that can happen to a Command Button

Week 1 Lecture 1 Slide 17CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Programming Practices

We need to consider

– Control naming conventions

– Variable naming conventions

– Code documentation

Week 1 Lecture 1 Slide 18CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Scope of Variables

Shows declarationsat form level, knownas: General Declarations

Shows variabledeclarations withinan event handler

Form1General Declarations

Sub Command1_Click ()

Sub Command2_Click ()

Dim sName1 As StringDim iNum1 As Integer

Dim sName2 As StringDim iNum2 As Integer

Dim sName3 As StringDim iNum3 As Integer

Available variables:sName1, sName2, iNum1, iNum2

Available variables:sName1, sName3, iNum1, iNum3

Week 1 Lecture 1 Slide 19CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Scope The general rule is to declare variables at

the lowest possible level.

Ie Control level

Form level Module level

Week 1 Lecture 1 Slide 20CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Question Calculator

Create a front end for a simple calculator, You should be able to accept two numbers and perform addition, subtraction, multiplication and division.

Week 1 Lecture 1 Slide 21CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Question calculator

Try the calculator with 5 divide 0 !!!

Why is the application not working ?

How can we correct the problem ?

Week 1 Lecture 1 Slide 22CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Solution

Week 1 Lecture 1 Slide 23CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Code

Week 1 Lecture 1 Slide 24CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

code

Week 1 Lecture 1 Slide 25CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Static Variables A static variable will hold its value when it goes

out of scope:Sub Command1_Click()

'declare variablesDim iDimCount As IntegerStatic iStaticCount As Integer

'increment variables iDimCount = iDimCount + 1 iStaticCount = iStaticCount + 1

'display variables Label3.Caption = Str$(iDimCount) Label4.Caption = Str$(iStaticCount)End Sub

A static variable can only be declared inside a procedure

Note the use of a comment Note use of Str$ function

Week 1 Lecture 1 Slide 26CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

Static Variables: Effects

The effect of using a static variable can be seen below:

This is has the same effect as if the variable had been declared at the form’s general declaration level

Except the scope is local to the procedure

Week 1 Lecture 1 Slide 27CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

CP2028Visual Basic programming 2

Week 1 - Summary– Structure of this module and its position in the

VB module set.– Review of VB environment.– Review of event-driven programming

Week 1 Lecture 1 Slide 28CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton

End of Lecture

Click House to Return to Main Menu