You are on page 1of 40

01.

Introduction to Visual Basic


Outline
 What is programming?

 Describe the process of visual program design and


development.

 Your first VB project

 Identify syntax errors, run-time errors, and logical


errors.

 Naming convention

2
What is a Computer?
 A computer is a machine that processes data
according to a list of instructions.
◦ Data
 Documents, video files, audio files, image files, user
inputs, etc.
◦ Instructions
 Programs, software, apps

 Examples
◦ Desktop computer, notebook, smartphone, server,
game console

3
What is Programming?
 Programming means specifying instructions to
be carried out by a computer.

 A program is written in a certain programming


language.

4
What is Programming?
 A programming language is a language designed for human to
express computer instructions.
◦ High-level programming languages  More English-like, easier to use
 e.g.: Visual Basic, Java, C++, C#, Objective C, Python, etc.

 However, computers cannot understand high-level programming


language directly, they can only understand machine language.
◦ Machine language (e.g. 1000111000 …) is too difficult to be understood by
human.

 Hence, we need a compiler to translate high-level programming


language to machine language.

5
10111111 A = 1 + 2;
10101011

Compiler print A;
11010110
10101111
11110101

Source code
Executable (in high-level
(in machine language) programming language)

 A compiler is a program that translates instructions written in


a programming language into another language, usually from
a high-level programming language to a machine language.
 The resulting program can typically be executed by a
computer directly.

6
Writing Windows Applications with
Visual Basic
 Windows Graphical User Interface (GUI)
◦ Defines how elements look and function.

Text boxes

Check box

Radio buttons Message box

Buttons
Picture box
Label

7
Writing Windows Applications with
Visual Basic

 Windows are called forms.


 Elements are called controls.

8
Object Model in Visual Basic
 Visual Basic is an Object-Oriented Programming (OOP)
language.
◦ Many high-level programming language are OOP languages.

 An object-oriented program consists of many objects.


◦ Each object is an independent unit.

 Objects = Nouns
◦ Buttons can be clicked by users.
◦ Textboxes can accept inputs from users.
◦ Message boxes can show messages to users.
◦ Forms are windows showing the layout of the GUI program.

9
Object Model in Visual Basic
 Objects have properties, methods, and events.

 Properties = Adjectives
◦ Size of the Button.
◦ Color of the Message Box.

 Methods ≈ Verbs
◦ Close the Form.
◦ Show the Message Box.

 Events occur when the user takes action.


◦ The user clicks a button.
◦ The user moves a form.

10
Object Model in Visual Basic
 To create objects, we use classes
◦ There are some built-in classes defined already.
◦ If necessary, we can define our own classes.

 Objects are created by classes, which are


blueprints (templates) used to create new
objects/instances.
◦ Classes contain the definitions of all properties,
methods, and events of the created objects.

11
Object Model in Visual Basic
 Suppose we want to have two new button objects
◦ The Button class is pre-defined in Visual Basic.
◦ Each button created is based on the Button class.
◦ Each button has its own set of properties, methods, and events.
◦ One button is labeled “Push Me”, another is labeled “Exit”.
◦ When the user clicks the “Push Me” button, it will display a message.
◦ When the user clicks the “Exit” button, it will terminate the program.

 More details about OOP will be covered in later lectures.

12
Writing Visual Basic GUI Applications
 There are three steps when writing a Visual
Basic GUI application
1. Defining the user interface
 Create Forms and Controls
2. Setting the properties
 Give each object a name and define attributes, such
as the text label and the size of a button
3. Writing the source code
 Use programming statements to carry out the
actions needed by the program

13
Visual Studio 2019 (VS2019)
 Integrated Development Environment (IDE)
◦ Form designer
 Allows programmers to visually create a form
◦ Code editor
 Allows programmers to enter and modify VB source code
◦ Compiler
 Translates the VB statements into (intermediate) machine code
◦ Debugger
 Help to locate and correct errors in the source code
◦ Object browser
 View the available classes, objects, properties, methods, and events

14
Installation of VS2019
 Make sure to check “.NET desktop development” during
the installation

15
VS2019 (For First Time Use)

Need a Microsoft account


(free to register, or we can
use O365 account) to sign
in after the 30-days trial

Click “Not now, maybe later.”


for a 30-days trial

16
VS2019 (For First Time Use)
Note: You can change the development
setting in “Tools”  “Import and Export
Settings…”  “Reset all settings” later.

1. Choose “Visual Basic”

2. Click “Start Visual Studio”

17
Visual Studio 2019

Click “Create a new project”

18
Visual Studio 2019 1. Select “Visual Basic” as filter

2. Click “Windows Forms App


(.NET Framework)” for Visual Basic

3. Click “Next”
19
Visual Studio 2019

1. Name your project


(e.g., VBDemo)

2. Click “Create”

20
Visual Studio 2019
Toolbox
Properties Window

Solution Explorer

Document Window

21
IDE Main Window
 Document Window
◦ The largest window in the center of the screen
◦ Items that display in the Document window include:
 Form Designer
 Code Editor

 Solution Explorer
◦ Holds the filenames for the files included in your project and a list of
the classes it references

 Properties Window
◦ Used to set the properties for the objects in a project

22
Toolbox
 Holds tools that are used to
place controls on a form

 You can scroll down to view


more controls

 You can also rearrange them


or sort them alphabetically

23
Rename the Source File
 By default, the source file is named
as Form1.vb, and the startup
object is named as Form1
◦ The properties window shows the name
and the properties of the object
◦ In Solution Explorer, we can rename the
file, say to HelloWorld.vb
◦ The name of startup object should also
be changed accordingly
◦ The name of the object should always
adhere to naming rules (see later slides)

2
4
1. Defining the UI
 Define the user interface
◦ Set up the form
◦ Place a label and two
button controls on the
form
◦ Lock the controls (No
more move)

 After the user interface is


defined, the next step is
to set the properties

25
2. Setting Properties
 HelloForm
◦ Name HelloForm
◦ Text Hello World
 Label 1
◦ Name MessageLabel
◦ Text <Leave blank>
 Button 1
◦ Name PushButton
◦ Text Push Me
 Button 2
◦ Name ExitButton
◦ Text Exit

26
3. Writing the Code
 While the project is running, the user can perform actions.

 Each action by the user causes an event to occur.

 Write code for the events we care about or the events we want to
respond to with code.

 Code is written as event procedures.


◦ We write Visual Basic code in procedures
◦ Currently each procedure will be a sub procedure and begin with words
Private Sub and end with End Sub
 More details will be given in later lectures.

27
3. Writing the Code
 Visual Basic will ignore events for which you
do not write code.

 Visual Basic will automatically name event


procedures as the object name, an
underscore(_), and the name of the event.
◦ E.g. PushButton_Click

28
Assignment Statement
 Assigns a value to the property of an object

MessageLabel.Text = " Hello World "

 The value appearing on the right side of the assignment


operator (=) is assigned to the property named on the left side
of the assignment operator.
◦ Enclose text strings in a pair of double quotation marks (" ")

 In general, to assign a value to the property of an object:


◦ ObjectName.Property = value

29
Ending a Program

Me.Close( )

 Current Form (the form itself) can be referenced as Me

 Methods always have parentheses.


◦ This will help us distinguish them from Properties, which never
have parentheses.

 In general, to execute a method of an object:


◦ ObjectName.Method()

30
Comments / Remark Statement
 Used for documentation
◦ Every procedure should begin with a comment providing explanation (to human).

 Non-executable, ignored by the compiler

 Automatically colored Green in Editor

 Begins with an apostrophe ( ' )


◦ On a separate line from executable code
 'Display the Hello World message

◦ At the right end of a line of executable code


 MessageLabel.Text = "Hello World" 'Assign message to Text property

31
The Final Code

' Project: Hello World


' Programmer: <Your name>
' Date: <Today's date>
' Description: This program will display a "Hello World" message

Public Class HelloForm


' Display a "Hello World" message when the user clicks PushButton
Private Sub PushButton_Click(sender As Object, e As EventArgs) Handles PushButton.Click
MessageLabel.Text = "Hello World"
End Sub

' Exit the program when the user clicks ExitButton


Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
Me.Close()
End Sub
End Class

32
Run your Application
 Run Project (choose any way)
◦ Click the Start button on the toolbar
◦ Press F5 or Ctrl+F5
◦ Open “Debug” Menu, select “Start Debugging” or “Start Without
Debugging”

 Save Project
◦ Open “File” Menu, select “Save All”
◦ For the assignments, please do NOT use “Save As …”

 The program will be compiled before the program is executed.

 Our goal is to have no errors during the compile process.

33
Type of Errors
 Syntax Errors (Compile-time Errors)
◦ Breaks Visual Basic’s rules for punctuation, format, or spelling
◦ The editor identifies a syntax error with a squiggly line and we can
point to an error to pop up the error message.
◦ We can check the Error List window to help locate the error lines.

 Run-Time Errors (Exceptions)


◦ Statements that fail to execute, such as impossible arithmetic
operations or calculation with non-numeric data
 E.g., compute “Hello World” + 1

 Logical Errors
◦ Project runs, but produces incorrect results.

34
Project Directory and Solution File
 Default location of the project files
◦ C:\Users\<user_name>\source\repos\<project_name>
◦ You can change the default location in:
Tools  Option  Projects and Solutions  Locations  Project location:

 One Solution File (E.g. HelloWorld.sln)


◦ A text file that holds information about the solution
and the projects it contains
◦ To open a project from Visual Studio, we should select
the solution file

35
Other Project Files
 VB source codes (E.g. HelloForm.vb)
◦ Holds the code procedures

 Form Files (E.g. HelloForm.Designer.vb)


◦ Holds the definition of a form and its controls

 Resource File for the Form (E.g. HelloForm.resx)


◦ A text file that defines all resources used by the form
including string of text and any graphics

36
Naming Rules and Conventions
 Naming Rules
◦ No spaces, punctuation marks, or reserved words
◦ Cannot compile the program if violated

 Naming Conventions
◦ Pascal casing
 MessageLabel
 ExitButton
 DataEntryForm
 PaymentAmountTextBox

◦ Camel casing
 messageLabel
 exitButton
 dataEntryForm
 paymentAmountTextBox
◦ Can still compile the program if violated
◦ But recommended to follow to improve the program readability

37
Summary
 A program is a list of instructions to process data.

 Visual Basic is an object oriented programming


language handling different events (users’ actions).

 Forms and Controls

 Writing a VB project requires:


◦ Defining the UI
◦ Setting the properties
◦ Writing the code

38
Summary
 Assignment Statement

 Closing the form

 Comment / Remark

 Different types of errors


◦ Syntax error
◦ Run-time error
◦ Logical error

 Naming rules and conventions

39
References and Readings
 Programming in Visual Basic 2010.
◦ Bradley and Millspaugh, McGraw-Hill
◦ Chapter 1. Introduction to Visual Basic.

40

You might also like