Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of...

Post on 21-Dec-2015

224 views 1 download

Transcript of Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of...

Introduction to Visual Basic

What it is, and a simple program

Mike Thelwall

University of Wolverhampton, UK

Origins through BASIC

BeginnersAll-purpose

SymbolicInstruction

CodeA simple programming languageDeveloped mid 1960’sSimplicity of BASIC made it a natural choice as a

programming languageMost machines have own version of BASIC

Windows Programming

Visual Basic developed for Windows by Microsoft from BASIC– Interface designer– Simple coding language– Quick to produce programs

Versions of it are now also in Excel, Access, Word, PowerPoint and many other software

What is it good for?

Lots of things!But particularly… automating repetitive tasks

that are governed by strict rulesIt is a relatively easy to learn language

– Good for those who are not ‘real’ programmers

Designing a Program – Draw it first

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 Basics 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

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

VB Environment: Properties Window

Events and Visual Basic

Start by drawing the screenThen add program code for Button Clicks

Trigger Event

Code Executed

Visual Basic Environment

Programming Application Example

You have a large text file containing 100,000 lines, each one of which is a reference from a journal article, with details of the author

You want to count how many of these references are to SUNY Albany faculty

Visual Basic can make this task possible

Designing the Program

Open the text fileRead in the first line of textCheck if the line contains “SUNY Albany”

– Keep a running total of how many matches there are

Read in the next line and repeatDisplay the total count when finished

What it looks like

The code

The data

nobody, no journal

someone not from Albany, journal of nothing

someone else not from Albany, journal of nothing

john smith, SUNY Albany, Topological properties of Spheres

Rong Tang, SUNY Albany, Some very hard information science

nobody, no journal

The result

Visual Basic in Excel

Visual basic hides behind Excel MacrosIf you know any version of VB then it is not

difficult to learn the other versionsVB looks horrible in Excel programs, but can

be very useful

A simple task for VB in Excel

Find the distance, in metres, between all pairs of a set of 34 US Chemistry departments

This is 1,089 distances – how can it be done?

Solution 1

Get the coordinates of each department by entering its Zip code into Multimap.com

Enter the coordinates into an Excel spreadsheetWrite a VB program to use this data to

calculate the distances between all pairs of departments

The code

How to write a Visual Basic program: Plan A

Think about EXACTLY what you want to do– Can it be fully automated, or will it require human

judgement?Write down EXACTLY what you want to do

– Break it into a series of stepsTake VB101 and learn VBWrite the programTest the program to make sure that it works

How to write a Visual Basic program: Plan B [Quick Version]

Think about EXACTLY what you want to do– Can it be fully automated, or will it require human

judgement?Write down EXACTLY what you want to do

– Break it into a series of stepsFind a VB programmerAsk them to write the program for youTest the program to make sure that it worksBuy them lunch