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

28
Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    224
  • download

    1

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

Page 1: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

Introduction to Visual Basic

What it is, and a simple program

Mike Thelwall

University of Wolverhampton, UK

Page 2: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.
Page 3: 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

Page 4: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

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

Page 5: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.
Page 6: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.
Page 7: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

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

Page 8: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

Designing a Program – Draw it first

Page 9: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

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

Page 10: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

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 11: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

VB Environment: Properties Window

Page 12: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

Events and Visual Basic

Start by drawing the screenThen add program code for Button Clicks

Trigger Event

Code Executed

Page 13: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

Visual Basic Environment

Page 14: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

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

Page 15: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

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

Page 16: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

What it looks like

Page 17: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

The code

Page 18: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

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

Page 19: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

The result

Page 20: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

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

Page 21: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

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?

Page 22: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

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

Page 23: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.
Page 24: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

The code

Page 25: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.
Page 26: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.
Page 27: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

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

Page 28: Introduction to Visual Basic What it is, and a simple program Mike Thelwall University of Wolverhampton, UK.

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