0% found this document useful (0 votes)
6 views

01. Introduction to Visual Basic

The document provides an introduction to Visual Basic (VB), covering the basics of computers, programming, and the process of visual program design. It explains the object-oriented nature of VB, the use of Visual Studio 2022 as an Integrated Development Environment (IDE), and the steps involved in writing a VB application, including defining the user interface, setting properties, and writing code. Additionally, it discusses types of errors, naming conventions, and concludes with a summary of key concepts.

Uploaded by

3C 01 蔡依涵
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

01. Introduction to Visual Basic

The document provides an introduction to Visual Basic (VB), covering the basics of computers, programming, and the process of visual program design. It explains the object-oriented nature of VB, the use of Visual Studio 2022 as an Integrated Development Environment (IDE), and the steps involved in writing a VB application, including defining the user interface, setting properties, and writing code. Additionally, it discusses types of errors, naming conventions, and concludes with a summary of key concepts.

Uploaded by

3C 01 蔡依涵
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

01.

Introduction to Visual
Basic
Outline
 What is a computer? 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 / binary (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

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 an 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 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.

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

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)

Sign in with a Microsoft account


(free to register, or we can use our CUHK
O365 account)

-OR- Click “Skip this for now.” for a 30-days


trial

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”.

1. Choose “Visual Basic”

2. Click “Start Visual Studio”


16
Visual Studio 2022 Click “Create a new project”

17
Visual Studio 2022
1. Select filters (Optional)

2. Select “Windows Forms


App (.NET Framework)”
with the “Visual Basic” label

3. Click “Next”
18
Visual Studio 2022

1. Name your project


(e.g., VBDemo)

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

 You can scroll down to view


more controls

 You can also rearrange them


or sort them alphabetically

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)

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

 If there is no error during the compilation process, the


application will be executed automatically

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.

 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.

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:

 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

36
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

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.

 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

39
Summary
 Assignment Statement

 Closing the form

 Comment / Remark

 Different types of errors


◦ Syntax error
◦ Run-time error
◦ Logical error

 Naming rules and conventions

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

41

You might also like