Lesson 3.1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Getting started

Introducing Visual Basic


In choosing to start programming with Visual Basic you have
made an excellent choice – the Visual Basic programming
language offers the easiest way to write programs for Windows.
This means you can easily create your own programs to give
maximum control over your computer and automate your work to
be more productive. Also, programming with Visual Basic is fun!
Like other programming languages, Visual Basic comprises
a number of significant “keywords” and a set of syntax rules.
Beginners often find its syntax simpler than other programming
languages, making Visual Basic a popular first choice to learn.
Although writing programs can be complex, Visual Basic makes
it easy to get started. You can choose how far to go. Another
advantage of Visual Basic is that it works with Microsoft Office
applications and with the Windows Script Host within the
Windows operating system – so the possibilities are immense...

• Visual Basic (VB) – quite simply the best programming


language for the novice or hobbyist to begin creating their own
8

standalone Windows applications, fast.

• Visual Basic for Applications (VBA) – an implementation of


Visual Basic that is built into Microsoft Office applications. It
runs within a host rather than as a standalone application.
• Visual Basic Script (VBScript) – a derivative of Visual Basic
that can be used for Windows scripting.
…cont’d

The evolution of Visual Basic

• Visual Basic 1.0 released in May 1991 at the Comdex trade


show in Atlanta, Georgia, USA.
• Visual Basic 2.0 released in November 1992 – introducing an
easier and faster programming environment.
• Visual Basic 3.0 released in the summer of 1993 – introducing
the Microsoft Jet Database Engine for database programs. Visual Basic derives
• Visual Basic 4.0 released in August 1995 – introducing support
for controls based on the Component Object Model (COM).
from an earlier simple
language called BASIC,
an acronym –
• Visual Basic 5.0 released in February 1997 – introducing the
ability to create custom user controls.
Beginners
All-purpose

• Visual Basic 6.0 released in the summer of 1998 – introducing


the ability to create web-based programs. This hugely popular
Symbolic
Instruction
Code.
edition is the final version based on COM and is often referred The “Visual” part was
added later as many
to today as “Classic Visual Basic”.
tasks can now be
• Visual Basic 7.0 (also known as Visual Basic .NET) released

9
accomplished visually,
in 2002 – introducing a very different object-oriented language without actually writing
any code.
based upon the Microsoft .NET framework. This controversial
edition broke backward-compatibility with previous versions,
causing a rift in the developer community.
• Visual Basic 8.0 (a.k.a.Visual Basic 2005).
• Visual Basic 9.0 (a.k.a. Visual Basic 2008).
• Visual Basic 10.0 (a.k.a. Visual Basic 2010).
• Visual Basic 11.0 (a.k.a. Visual Basic 2012).
• Visual Basic 12.0 (a.k.a. Visual Basic 2013).
(Version numbering of Visual Basic skipped 13 to keep in line
with the version numbering of Visual Studio itself.)
• Visual Basic 14.0 (a.k.a. Visual Basic 2015).
• Visual Basic 15.0 (a.k.a. Visual Basic 2017).
• Visual Basic 16.0 (a.k.a. Visual Basic 2019).

All examples in this book are created for Visual Basic 16.0
although many of the core language features are common to
previous versions of the Visual Basic programming language.
Getting started

Installing Visual Studio


In order to create Windows applications with the Visual Basic
programming language you will first need to install a Visual
Studio Integrated Development Environment (IDE).
Microsoft Visual Studio is the professional development tool that
provides a fully Integrated Development Environment for Visual
Basic, Visual C++, and Visual C#. Within its IDE, code can be
written in C++, C#, or the Visual Basic programming language to
create Windows applications.
Visual Studio Community edition is a streamlined version
of Visual Studio specially created for those people learning
Visual Studio is used
programming. It has a simplified user interface and omits
to develop computer
advanced features of the professional edition to avoid confusion.
programs, web apps,
mobile apps, and more. Within its IDE, code can be written in the Visual Basic
programming language to create Windows applications.
Both Visual Studio and Visual Studio Community provide a
Visual Basic IDE for Visual Basic programming. Unlike the fully-
featured Visual Studio product, the Visual Studio Community
edition is completely free and can be installed on any system
10

meeting the following minimum requirements:

Component Requirement

Windows 10 (version 1703 or higher)


Windows Server 2019
Operating system Windows 8.1 (with update 2919355)
Windows 7 Service Pack 1
Windows Server 2012 R2
CPU (processor) 1.8 GHz or faster
RAM (memory) 2 GB (8 GB recommended)
HDD (hard drive) Up to 210 GB available space
Minimum resolution of 1280 x 720
Video Card
Optimum resolution of 1366 x 768

The Visual Studio Community edition is used throughout this


book to demonstrate programming with the Visual Basic language,
but the examples can also be recreated in Visual Studio. Follow the
steps opposite to install Visual Studio Community edition.
…cont’d

l1 Open your web browser and navigate to the Visual Studio


Community download page – at the time of writing this
can be found at visualstudio.microsoft.com/vs/community

Installation of Visual
Studio is handled by an
installer application. You
can re-run the installer

l2 Click a button to select


your operating system
– Windows or macOS
l 3 Now, click on the
Download button
to grab the installer
at a later date to add or
remove features.

l4 Open your
Downloads
folder, then

11
click on the
installer file
icon to fetch
some setup
files

l5 Choose the two VB options as the type of installation

Choosing a different
destination folder may
require other paths to
be adjusted later – it’s
simpler to just accept the
suggested default.

l6 Click the Install button to begin the download and


installation process
Getting started

Exploring the IDE


l 1 Go to your Apps menu, then select the Visual Studio
2019 menu item added there by the installer:

The first time Visual


Studio starts it takes
a few minutes as it
performs configuration
routines.
l 2 Sign in with your Microsoft account, or register an
account then sign in, to continue

l 3 See a default Start Page appear where recent projects will


be listed alongside several “Get started” options
12

In the future your recent


projects will be listed
here so you can easily
reopen them.

l 4 For now, just click the link to Continue without code to


launch the Visual Studio application

The Visual Studio Integrated Development Environment (IDE)


appears, from which you have instant access to everything needed
to produce complete Windows applications – from here you can
create exciting visual interfaces, enter code, compile and execute
applications, debug errors, and much more.
...cont’d

Menu Bar

Toolbar

Solution Explorer

Status Bar Toolbox Notifications

Visual Studio IDE components


The Visual Studio IDE initially provides these standard features:

• Menu

13
Bar – where you can select actions to perform on all
your project files and to access Help. When a project is open,
extra menus of Project, and Build, are shown in addition to
the default menu selection of File, Edit, View, Debug, Test,
To change the color,
Analyze, Tools, Extensions, Window, and Help.
choose the Tools,
• Toolbar – where you can perform the most popular menu
actions with just a single click on its associated shortcut icon.
Options menu then
select Environment,


General, Color Theme.
Toolbox – where you can select visual elements to add to a
project. Click the Toolbox side bar button to see its contents.
When a project is open, “controls” such as Button, Label,
CheckBox, RadioButton, and TextBox are shown here.

• Solution Explorer – where you can see at a glance all the files
and resource components contained within an open project.

• Status Bar – where you can read the state of the current
activity being undertaken. When building an application a
“Build started” message is displayed here, changing to a
“Build succeeded” or “Build failed” message upon completion.
Getting started

Starting a new project


l1 On the Menu Bar, click File, New, Project to open the
“Create a new project” dialog

l
GettingStarted
2 Next, you must choose “Language”, “Platform”, and
“Project type” – select Visual Basic, Windows, Desktop
14

l3 Now, select the Windows Forms App option

l4 Click the Next button to open the “Configure your new


project” dialog

Leave the Create


directory for solution
option checked to create
a folder named as the
project name, and
located by default in a
C:\Users\username\
source\repos directory.
...cont’d

l 5 Enter a name of your choice in the Project name field –


in this case, the project name will be “GettingStarted”

l 6 Click on the Create button to create the new project

Visual Studio now creates your new project and loads it into the
IDE. A tabbed Designer window appears displaying a default
empty Form. You can select the View, Solution Explorer menu
to open a Solution Explorer window that reveals all files in your
project, and you can select the View, Properties Window menu
to open a Properties window listing all properties of your Form.

Designer

15
Solution Explorer

Properties window

The Designer is where you create visual interfaces for your


applications, and the Properties window contains details of the
item that is currently selected in the Designer window.
The Visual Studio IDE has now gathered all the resources needed
to build a default Windows application – click the Start button
on the Toolbar to launch this application. The application simply
creates a basic window – you can move it, minimize it, maximize
it, resize it, and quit the Alternatively, you can
application by closing it. It run applications using
may not do much, but you the Debug, Start
have already created a real Debugging menu
Windows program! options.
Getting started

Adding a visual control


The Toolbox in the Visual Studio IDE contains a wide range of
visual controls that are the building blocks of your applications.
Using the project created on page 15, follow these steps to start
using the Toolbox now:

The Toolbox will


automatically hide when
you click on another part
l 1 Click View, Designer
to see the Designer
window
of the IDE, but it can be

l
fixed in place so it will
never hide, using the 2 Next, click View,
pin button on the
Toolbox on the Menu
Toolbox bar.
Bar, or click the Toolbox
side bar button, to
display the Toolbox
categories

l 3 Now, click on the


16

expansion arrow
beside the Common
If no controls are visible Controls category
in the Toolbox, right- heading to expand the
click on the Toolbox then list of visual controls.
choose Show All to
Usefully, each control
reveal the categories.
name appears beside
an icon depicting that
control as a reminder.
You can click on the
Common Controls
category heading again
to collapse the list, then
expand other categories
to explore the range
of controls available to
Any pinned Window in
the IDE can be dragged
build your application
from its usual location to interfaces
any position you prefer.
Drag it back to the initial
location to re-dock it.
…cont’d

l 4 Click and drag the Button item from the Common


Controls categories in the Toolbox onto the Form in the
Designer window, or double-click the Button item, to add
a Button control to the Form

A Button is one of the


most useful interface
controls – your program
determines what
happens when the user
The Button control appears in the Designer window surrounded clicks it.
by “handles” that can be dragged to resize the button’s width and
height. Click the Start button to run the application and try
out your button.

17
The Button control behaves in a familiar Windows application
manner, with “states” that visually react to the cursor: This Button control
performs no function
when it’s clicked – until
you add some code.

Default State Hover State Down State


Getting started

Adding functional code


The Visual Studio IDE automatically generates code, in the
background, to incorporate the visual controls you add to your
program interface. Additional code can be added manually, using
the IDE’s integral Code editor window, to determine how your
program should respond to interface events – such as when the
user clicks a button.
Using the project created on page 17, follow these steps to start
using the Visual Studio Code editor now:

l
Switch easily between
the Code window and 1 Double-click on the
Designer window
Button control you have
by clicking on the
appropriate window tab.
added to the default
Form in the Designer
window. A new tabbed
text window opens in
the IDE – this is the
Code editor window

l
18

2 The cursor is automatically placed at precisely the right


point in the code at which to add an instruction to
determine what the program should do when this button
is clicked. Type this instruction into the Code editor
MsgBox(“Hello World!”)

This Solution Explorer


and Properties
windows are closed
here for clarity. You can
reopen them at any time
from the View menu.
…cont’d

l 3 Click the Start button to run the application and test the
code you have just written to handle the event that occurs
when the button is clicked

Use the View menu on


the Menu Bar to open
the Code editor, Form
Designer, or any other
window you require at
any time.

19
l 4 Push the OK button to close the dialog box, then click
the X button on the Form window, or click the Stop
Debugging button on the Menu Bar, to stop the program

Each time the button in this application is pressed, the program


reads the line of code you added manually to produce a dialog
box containing the specified message. The action of pressing the
button creates a Click event that refers to the associated
“event-handler” section of code you added to see how to respond.
In fact, most Windows software works by responding to events in
this way. For instance, when you press a key in a word processor
a character appears in the document – the KeyPress event calls
upon its event-handler code to update the text in response.
The process of providing intelligent responses to events in
your programs is the very cornerstone of creating Windows
applications with Visual Basic.
Getting started

Saving projects
Even the simplest Visual Basic project comprises multiple files
that must each be saved on your system to store the project.
Follow these steps to save the current project to disk:

l 1 Click the Save All


button on the Toolbar,
or click File, Save All
on the Menu Bar, or
You can click File, Close press Ctrl + Shift + S
Solution on the Menu
Bar to close an open
project – a dialog will
prompt you to save any
changes before closing.

l 2 Your project is now saved at its default save location

l 3 To discover or change the save location, click Tools on


the Menu Bar, then select the Options item – to open
20

the Options dialog

l 4 Expand Projects and Solutions in the left-hand pane,


then choose the Locations option to reveal Projects
location

Find the Debug folder


in your saved project
directory containing the
application’s executable
(.exe) file – you can
double-click this to run
your program like other
Windows applications.
Reopening projects
Use these steps to reopen a saved Visual Basic project:

l 1 Click File, Open, Project/Solution... on the Menu Bar


to launch the Open Project/Solution dialog

Only have one project

l
open at any given time
2 In the Open Project/Solution dialog, select the folder to avoid confusion –
containing the project you wish to reopen, and Open it unless several are needed
to be open together for
advanced programming.

21
l 3 Select the Visual Basic Solution file with the extension
.sln to reopen the project, or open the folder bearing the
project name then select the Visual Basic Project File
with the extension .vbproj to reopen the project
If you don’t see the
Designer window after
you have reopened a
project, click on the
MainWindow.xaml
icon in Solution
Explorer to make it
appear.

You might also like