cp2030 vbfc visual basic for c++ programmers lecture 1

35
Lec1 P 1 Visual Basic For C++ Programmers Copyright © University of Wolverhampton 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

Upload: azure

Post on 05-Jan-2016

58 views

Category:

Documents


4 download

DESCRIPTION

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. Introduction To The Module Staff Names. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 1CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

CP2030 VBFCVisual Basic For C++ ProgrammersLecture 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

Page 2: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 2CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Introduction To The ModuleStaff Names

D.I.Beardsmore MU401 1461 CM1940

P.I.Wilson MU217 1806 CM1949

Page 3: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 3CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Structure Of The Module Module consists of formal Lectures, Tutorials and Workshops

Lectures - 2 hrs approx. Notes will be provided

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.

Page 4: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 4CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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

Page 5: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 5CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Module Assessment

Assessment 70%

Four Assessment questions.

(Assessment 1 consists of a number of smaller questions)

Examination 30%

Page 6: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 6CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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

Page 7: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 7CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

The Visual Basic module set.

CP1007VB1

CP2028VB2

CP3013App Dev in VB

CP2030VB For C++

CP1000S.P. in C++

Page 8: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 8CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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

Page 9: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 9CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Background to Visual Basic

BeginnersAll-purpose

SymbolicInstruction

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,… MS-

QuickBASIC(1982)

Page 10: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 10CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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

Page 11: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 11CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Differences between VB & C++

Students to add notes

Page 12: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 12CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Similarities between VB & C++

Students to add notes

Page 13: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 13CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Visual Basic Environment

Components of the VB design environment?

Form

MenuBar

Control Toolbox

Project Window

PropertiesWindow

Page 14: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 14CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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 Toolbar

Page 15: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 15CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

VB Environment: Project Window

Page 16: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 16CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

VB Environment: Properties Window

Page 17: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 17CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

VB Environment: Control Toolbox

Pointer

Label

Group Frame

Check Box

Combo Box

Horizontal Scroll Bar

Timer

Directory List Box

Shape

Image

Common Dialog

OLE

Picture

Text Box

Command Button

Option/Radio Button

List Box

Vertical Scroll Bar

Drive List Box

File List Box

Line

Data Control

Grid

Page 18: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 18CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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

Page 19: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 19CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Visual Basic Program Overview

Design Tools

Screen Editor Code Editor Run/Debug

Toolbox

Property Box

Colour Palette

Menu Design

Control/Event

Third PartyControlsVBX/OCX

Other tools:Paintbrushetc..

Compiler/Application

Page 20: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 20CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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

Page 21: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 21CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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 thatcan happen to a command button

Page 22: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 22CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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

Page 23: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 23CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Control - Form(s)

At design stage controls are placed on the form During execution the form control acts like any other window-

maximize, 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

Page 24: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 24CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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

Page 25: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 25CP2030 Visual Basic For C++ Programmers 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)

Page 26: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 26CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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

Page 27: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 27CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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

Page 28: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 28CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Changing Properties at Run Time

To change a control’s property you assign the new property to it:

Command1.Enabled = FalseLabel1.ForeColor = QBColor(4)

To access a control on another form you should use the form name to prefix the property name:

Form1.Command1.Caption = “New Caption”

Control Name Property

Form Name Control Name Property

Page 29: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 29CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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.

Page 30: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 30CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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)

Page 31: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 31CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Control - CommandWriting 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.

Page 32: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 32CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Developing a Simple VB Program

Students to add notes

Page 33: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 33CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Visual Basic Program Structure

Project File– ‘.MAK’

Form Files– ‘.FRM’

– ‘.FRX’

Modules– ‘.BAS’

Custom Controls– ‘.VBX’ files

Save As...– Possible problem...

Page 34: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 34CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Saving The Project

When you come to save the project you will be saving a number of files:

– FORM1.FRM the form itself

– PROJECT1.MAK 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!

Page 35: CP2030 VBFC Visual Basic For C++ Programmers Lecture 1

Lec1 P 35CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

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.