Вы находитесь на странице: 1из 28

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 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 1

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

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 2

The Visual Basic module set.


CP1007 VB1

CP2028 VB2
CP2030 VB For C++

CP3013 App Dev in VB

CP1000 S.P. in C++

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 3

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.

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 4

Introduction to the Module [2]

Timetable details (see module guide)

2 x 1-hour lectures per week Tutorial Workshop

Weekly contents
Assessments

Coursework Exam

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 5

Review of V.B. Environment

Identify the components of the VB design environment?

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 6

Review of V.B. Environment

Identify the components of the VB design environment?

Menu Bar

?
Code
CP2028 Visual Basic Programming 2 The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 7

Review of V.B. Environment

Identify the components of the VB design environment?

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 8

Review of V.B. Environment

Identify the components of the VB design environment?

Menu Bar Control Toolbox

Project Window

Form
CP2028 Visual Basic Programming 2 The VB Team Copyright University of Wolverhampton

Properties Window
Week 1 Lecture 1 Slide 9

Visual Basic Program Structure.


Project File

.VBP
.FRM .FRX .BAS .VBX files

Form Files

Modules

Custom Controls

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 10

Control Bar - How many controls can you identify?

Standard Edition

Professional Edition

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 11

Control bar - Properties


Run Time Vs Design Time (Startup Defaults)
Name Setting control positions when place on form Vs via the properties window What if we want to resize our forms do we resize controls? How? Are there tools to do it?
CP2028 Visual Basic Programming 2 The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 12

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

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 13

Visual Basic Event Processing

Trigger Event

Code Executed

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 14

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)

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 15

Visual Basic Events


The events that can happen to a control are predetermined Each type of control has a relevant set of events

The events that can happen to a Command Button

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 16

Programming Practices

We need to consider
Control naming conventions Variable naming conventions

Code documentation

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 17

Scope of Variables

Shows declarations at form level, known as: General Declarations Shows variable declarations within an event handler

Form1 General Declarations Dim sName1 As String Dim iNum1 As Integer Sub Command1_Click () Dim sName2 As String Dim iNum2 As Integer Available variables: sName1, sName2, iNum1, iNum2 Sub Command2_Click () Dim sName3 As String Dim iNum3 As Integer Available variables: sName1, sName3, iNum1, iNum3

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 18

Scope

The general rule is to declare variables at the lowest possible level.

Ie
Control level Form level Module level

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 19

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.

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 20

Question calculator

Try the calculator with 5 divide 0 !!! Why is the application not working ? How can we correct the problem ?

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 21

Solution

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 22

Code

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 23

code

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 24

Static Variables

A static variable will hold its value when it goes out of scope: Sub Command1_Click()
'declare variables

'increment variables 'display variables

Dim iDimCount As Integer Static iStaticCount As Integer


iDimCount = iDimCount + 1 iStaticCount = iStaticCount + 1

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

CP2028 Visual Basic Programming 2 The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 25

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 forms general declaration level Except the scope is local to the procedure
The VB Team Copyright University of Wolverhampton

CP2028 Visual Basic Programming 2

Week 1 Lecture 1 Slide 26

CP2028 Visual 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

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Week 1 Lecture 1 Slide 27

End of Lecture

Click House to Return to Main Menu


Week 1 Lecture 1 Slide 28

CP2028 Visual Basic Programming 2

The VB Team Copyright University of Wolverhampton

Вам также может понравиться