CP1007 Visual Basic Programming 1

advertisement
CP2030 VBFC
Visual Basic For C++ Programmers
Lecture 1
Back To Index
Module Introduction
Structure Of Module
 What Is Visual Basic
 The Visual Basic Environment
 Introduction To Controls, Events, Properties and Methods
 A Simple Visual Basic Program

CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 1
Introduction To The Module
Staff Names

D.I.Beardsmore
MU401
1461

P.I.Wilson
MU217
1806 CM1949
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
CM1940
Lec1 P 2
Structure Of The Module

Module consists of formal Lectures, Tutorials and Workshops

Lectures - 2 hrs approx.

Tutorials - 1 hour
In most cases questions will be set - time will be made available
for students to raise problems, and review lectures, etc

Workshops - 1 hours
Help with WS questions. Assessment sign offs.
Notes will be provided
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 3
Module Information

Refer to the timetable for Workshops and Tutorials.

WorkShop and Tutorial groups.
IT IS IMPORTANT TO KEEP TO YOUR ALLOCATED
GROUPS
Book list :
 Visual Basic Quick Tutor - R Smythe
 An Intro To Programming Using Visual Basic 4 - D. Schneider
 Mastering VB - S.Saxon, D Saxon

CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 4
Module Assessment

Assessment 70%
Four Assessment questions.
(Assessment 1 consists of a number of smaller questions)

Examination 30%
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 5
Aims of The Module

Provides an introduction to Windows programming - event
driven programming.

Design AND Programming in a Graphical User Interface
Environment

Introduces many of the most widely used controls - enables
students to fully develop a stand alone application.

Develops professional programming skills

Prepares students for industrial year
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 6
The Visual Basic module set.
CP1007
VB1
CP2028
VB2
CP2030
VB For C++
CP3013
App Dev in
VB
CP1000
S.P. in C++
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 7
What is Visual Basic?
Event-driven programming language
 Powerful, Windows-based, screen designer.....
 ....linked to underlying functionality in
subroutines/functions
 Increased functionality via third party VBX(Visual Basic
Extension - 16 bit) and OCX (Object Linking and
Embedding - 32 bit)
 Rapid application development

CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 8
Background to Visual Basic
Beginners
All-purpose
Symbolic
Instruction
Code
 A simple programming language
 Developed in 1963 as a resident language
 Simple Language made it very popular
 Most machines have own version of BASIC
 DOS based (not windows)
 Many version developed GWBASIC, CPMBASIC,… MSQuickBASIC(1982)

CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 9
Windows Programming
Visual Basic developed in-house at Microsoft
 Windows based
 Applications developed within an environment
 Powerful windows design for Graphical User Interfaces (GUIs).
 Simple but powerful coding language
 Quick to produce applications


Currently the most popular language for Windows applications
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 10
Differences between VB & C++

Students to add notes
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 11
Similarities between VB & C++
Students to add notes
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 12
Visual Basic Environment

Components of the VB design environment?
Menu
Bar
Project
Window
Control
Toolbox
Form
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Properties
Window
Lec1 P 13
VB Environment: Menu Bar

The Menu Bar consists of 3 elements the
– Title Bar, which holds the name of application, default if Microsoft Visual
Basic [design]
– Menu Bar, this is the link to Visual Basic’s menu facilities, each menu
option drops down into sub-menus
– Toolbar, contains icons which give access to the more commonly used
commands (which are also available through the menu bar)
Title bar
Menu Bar
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Toolbar
Lec1 P 14
VB Environment: Project Window
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 15
VB Environment: Properties Window
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 16
VB Environment: Control Toolbox
Pointer
Label
Group Frame
Check Box
Combo Box
Horizontal Scroll Bar
Timer
Directory List Box
Picture
Text Box
Command Button
Option/Radio Button
List Box
Vertical Scroll Bar
Drive List Box
File List Box
Shape
Line
Image
Data Control
Common Dialog
OLE
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Grid
Lec1 P 17
VB Environment: Context Sensitive Help
Context sensitive help is very useful
 Select the item control that you want help on
 Press the F1 key

F1
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 18
Visual Basic Program Overview
Design Tools
Screen Editor
Toolbox
Code Editor
Control/Event
Run/Debug
Other tools:
Paintbrush
etc..
Property Box
Colour Palette
Menu Design
Compiler/
Application
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Third Party
Controls
VBX/OCX
Lec1 P 19
Introduction To Controls, Events, Properties
& Methods
 Controls - Processing
Graphical objects which can be placed onto a form (window)
– Controls have pre-defined ‘events’ eg. mouse click
– Code can be attached to an event
– When an event occurs
 if code has been attached it is executed,
 otherwise default processing takes place

Controls - Properties
– all controls have appropriate properties
 such as Height, Name, Colour
– properties can be changed either at design-time or at run-time
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 20
Visual Basic Events
eg. Mouse Click, Key Press, Got Focus, Timer, etc.
 Control events are pre-determined by the programmer
 Each type of control has a relevant set of events


Eg. Events that
can happen to a
command button
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 21
Event Driven Programs

In normal programming your application is always in control

In GUI(Graphical User Interface) programming your
applications provide a number of service routines which are
triggered as required by the operating system

Events happen to a control/object
– mostly user generated events
– controls themselves can also cause events

Events are pre-set/pre-programmed
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 22
Control - Form(s)
At design stage controls are placed on the form
 During execution the form control acts like any other windowmaximize, minimize, etc
 An application can be make up from a number of forms
 Like all other controls the form has properties - caption name,
size, colour, etc

CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 23
Events and Visual Basic
Visual Basic is event-driven
 You start with your user interface by drawing graphical
Controls on Forms
 You then add program code for each of these controls which will
be run (or triggered) when a particular event is generated
 Example event: Command Button Click

Trigger Event
Code Executed
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 24
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)
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 25
Controls Properties

Properties describe the characteristics of a control, these can be
– physical characteristics such as height, width and colour
– or its current state such as enabled, or links definitions to other
applications

Note the name and caption properties of a control have the same
default values but are actually different.
– The name is what code uses to reference the control, whereas
– the caption is purely what is written on the control so the user can
identify it
Good idea to rename controls to meaningful names

Properties can be changed either at design time or at run time
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 26
Changing Properties at Design Time
By selecting the properties window
properties can be set to values at design
time
 A drop down list of the controls currently
on the form, allows the selection of a
control so its properties can be edited
 All the properties of that control are
shown below, where they can be selected
for editing
 The currently selected property of the
selected control is shown in the edit
window where it can be modified

CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 27
Changing Properties at Run Time

To change a control’s property you assign the new property to it:
Control Name
Property

Command1.Enabled = False
Label1.ForeColor = QBColor(4)

To access a control on another form you should use the form name
to prefix the property name:
Form Name
Control Name
Property
Form1.Command1.Caption = “New Caption”
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 28
Controls Methods

A method is a routine which operates on a control, for example
control_name.SetFocus
would make control_name the currently selected control.
 There are many methods available, we will only use a few of
them

The Events, Properties and Methods for controls can be found
under the controls description in Help. i.e. select a control then
press F1 to get the help for a control.
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 29
Controls - Label and Text Boxes
 Labels are used for text output : Titles, Messages, and Prompts
- screen text that the user cannot change.
eg. Label1.Caption = “Heading For Program 1”
 Text
Boxes are used for both text input and output.
eg.
Text1.Text = “Output to Text box1”
sName = Text2.Text (Text Input covered in week2)
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 30
Control - Command
Writing Code Under an Event
When we want our program to do something we have to write
code to do it, code is written under event procedures
 We have to decide the control and its event procedure we want
the user to use to invoke the code
 Command buttons are often used to invoke code. Normally for
command buttons we use their click event


In actual fact you can write code for any event from any control.
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 31
Developing a Simple VB Program

Students to add notes
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 32
Visual Basic Program Structure

Project File
– ‘.MAK’

Form Files
– ‘.FRM’
– ‘.FRX’

Modules
– ‘.BAS’

Custom Controls
– ‘.VBX’ files

Save As...
– Possible problem...
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 33
Saving The Project

When you come to save the project you will be saving a number
of files:
– FORM1.FRM
– PROJECT1.MAK
the form itself
the project make file

The best way to organise your projects is by keeping each
project in a separate directory.
You can create new directories using file manager

If you are working on the harddisk/network drive then you
should make sure that you copy the files back onto your floppy
disk when you’ve finished!
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 34
Question

Create an application to either display a students name with
either First name followed by Second name OR Second name
followed by first name.
CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton
Lec1 P 35
Download