01. Introduction to Visual Basic
01. Introduction to Visual Basic
Introduction to Visual
Basic
Outline
What is a computer? What is programming?
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.
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.
Source code
Executable / binary (in high-level
(in machine language) programming language)
6
Writing Windows Applications
with Visual Basic
Windows Graphical User Interface (GUI)
◦ Defines how elements look and function.
Text boxes
Check box
Radio buttons
Buttons
Picture box
Label
7
Writing Windows Applications
with Visual Basic
8
Object Model in Visual
Basic
Visual Basic is an Object-Oriented Programming
(OOP) language.
◦ Many high-level programming language are OOP languages.
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.
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.
11
Object Model in Visual
Basic
Suppose we want to have two button objects in our
program
◦ 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, a message will be
shown.
◦ When the user clicks the “Exit” button, the program will be
terminated.
12
Visual Studio 2022
(VS2022)
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
13
Installation of VS2022
Make sure to check “.NET desktop development”
during the installation
14
VS2022 (For First Time Use)
15
VS2022 (For First Time Use)
Note: If you forgot to set this, later you can change the development setting in
“Tools” “Import and Export Settings…” “Reset all settings”.
17
Visual Studio 2022
1. Select filters (Optional)
3. Click “Next”
18
Visual Studio 2022
2. Click “Create”
19
Visual Studio 2022
Solution Explorer
Toolbox
Document Window
Properties Window 20
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
21
Toolbox
Holds tools that are used to
place controls on a form
22
Rename the Source File
By default, the source file is named
as Form1.vb, and the startup form 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 HelloForm.vb
◦ Sometimes the startup form will also be
changed automatically
If not, we can change it manually by right-
clicking the project name, and choose
“Properties”
◦ The name of the object should always
adhere to naming rules (see later slides)
23
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
24
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)
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.
27
3. Writing the Code
Visual Basic will ignore events for which you
do not write code.
28
Assignment Statement
Assigns a value to the property of an object
29
Ending a Program
Me.Close( )
30
Comments / Remark
Statement
Used for documentation
◦ Every procedure should begin with a comment providing explanation (to human).
31
The Final Code
32
Compile and Execute
Several ways to compile and execute your application:
1. Click the “Start Without Debugging” button on the
toolbar (or the “Start” button)
2. Press Ctrl + F5 (or F5)
3. Open “Debug” Menu, select “Start Without
Debugging” (or “Start Debugging”)
◦ Note: “Start Without Debugging” will be faster to load as it
will not load the debugger
33
Save the Project
When you compile the application, the whole
project will be saved automatically
But if you want to save the project manually:
◦ Open “File” Menu, select “Save All” (or simply click the
“Save All” icon on the top)
◦ Do NOT use “Save XXX.vb As …” when you are
working on the assignment
Otherwise, this file may then be saved in another
location (not in the project directory)
When you submit your assignment later, probably this
file will not be included in the submission
34
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.
Logical Errors
◦ Project runs, but produces incorrect results.
35
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:
36
Other Project Files
VB source codes (E.g. HelloForm.vb)
◦ Holds the code procedures
37
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
38
Summary
A program is a list of instructions to process data.
39
Summary
Assignment Statement
Comment / Remark
40
References and Readings
Programming in Visual Basic 2010.
◦ Bradley and Millspaugh, McGraw-Hill
◦ Chapter 1. Introduction to Visual Basic.
41