Visual Basic Dot Net
(VB.NET)
Hudson Nandere Lubinga
Email: nanderehudson@gmail.com, TEL: 0753625255
VB.NET IDE
04/22/2022 Hudson Nandere Lubinga 2
Integrated Development Environment
(IDE)
It allows the automation of many of the common programming
tasks in one environment
Writing the code
Checking for Syntax (Language) errors
Compiling and Interpreting(Transferring to computer language)
Debugging (Fixing Run-time or Logic Errors)
Running the Application
• Visual Basic.NET is one of several languages supported by Visual Studio (VS) IDE
• Other languages supported are called C++, C# and Java
04/22/2022 Hudson Nandere Lubinga 3
Using Visual Studio.NET
04/22/2022 Hudson Nandere Lubinga 4
Creating an Application
• Select the “Create Project” option from the “Recent Projects”
box on the Start Page
04/22/2022 Hudson Nandere Lubinga 5
Default Settings
04/22/2022 Hudson Nandere Lubinga 6
Visual Basic Forms
• This is a Visual Basic
GUI object called a form
• Forms are the windows
and dialog boxes that
display when a program
runs.
• A form is an object that
contains other objects
such as buttons, text
boxes, and labels
Slide 1- 7
Visual Basic Controls
• Form elements are
objects called controls
• This form has:
• Two TextBox controls
• Four Label controls
• Two Button controls
• The value displayed by
a control is held in the text property of the control
• Left button text property is Calculate Gross Pay
• Buttons have methods attached to events
Slide 1- 8
Design
T Window
Solution
o Explorer
o
l
b Properties
o Window
x
04/22/2022 Hudson Nandere Lubinga 9
Creating the Application
• Step 1: Add a Control to the Form – Button
• Look in the Toolbox for the Button Control
• Select the Button with the Mouse
• Draw a Rectangle Region in the Design Window by holding the mouse button
down
• Release the mouse button to see your button
• (Can also be added by double clicking on the button in the Toolbox)
04/22/2022 Hudson Nandere Lubinga 10
04/22/2022 Hudson Nandere Lubinga 11
Creating the Application
• Add a Second Button to the Form
• Put it in the lower right corner
• The project now contains
• a form with 2 button
• controls
04/22/2022 Hudson Nandere Lubinga 12
Control Properties
• Properties
• All controls have properties
• Each property has a value (or values)
• Determine the Look and Feel (and sometimes behavior) of a Control
• Set initially through the Properties Window
• Properties Set for this Application
• Name
• Text
04/22/2022 Hudson Nandere Lubinga 13
Name Property
• The name property establishes a means for the program to refer to
that control
• Controls are assigned relatively meaningless names when created
• Change these names to something more meaningful
• Control names must start with a letter
• Remaining characters may be letters, digits, or underscore
04/22/2022 Hudson Nandere Lubinga 14
Examples of Names
The label controls use the default names (Label1, etc.)
Text boxes, buttons, and the Gross Pay label play an
active role in the program and have been changed
Label1 txtHoursWorked
Label2 txtPayRate
Label3 lblGrossPay
btnCalcGrossPay btnClose
04/22/2022 Hudson Nandere Lubinga 15
Control Naming Conventions
• Should be meaningful
• 1st 3 lowercase letters indicate the type of control
• txt… for Text Boxes
• lbl… for Labels
• btn… for Buttons
• After that, capitalize the first letter of each word
• txtHoursWorked is clearer than txthoursworked
• Change the name property
• Set the name of button1 to btnWelcome
• Set the name of button2 to btnExit
04/22/2022 Hudson Nandere Lubinga 16
Setting Control Properties
• Click on the Control in the Design Window
• Select the appropriate property in the Properties
Window
04/22/2022 Hudson Nandere Lubinga 17
Text Property
• Determines the visible text on the control
• Change the text property
• btnWelcome set to “Say Welcome”
• btnExit set to “Exit”
• Do not need to include the “ “ in your text field
• Notice how the buttons now display the new text
04/22/2022 Hudson Nandere Lubinga 18
Event Driven Programming
• The GUI environment is event-driven
• An event is an action that takes place within a program
• Clicking a button (a Click event)
• Keying in a TextBox (a TextChanged event)
• Visual Basic controls are capable of detecting many, many events
• A program can respond to an event if the programmer writes an event
procedure
04/22/2022 Hudson Nandere Lubinga 19
Event Procedures
• An Event Procedure is a block of code that executes
only when particular event occurs
• Writing an Event Procedure
• Create the event procedure sub
• Double click on control from Design Window – for default event
for that control
OR
• Open the Code Editor (F7 or View Menu/Code option)
• Select Control & Select Event from drop down windows in Code
Editor
• Add the event code to the event procedure stub
04/22/2022 Hudson Nandere Lubinga 20
Open the Code Editor
04/22/2022 Hudson Nandere Lubinga 21
04/22/2022 Hudson Nandere Lubinga 22
Select the Control for the Event Procedure
• Select the btnWelcome control from the Form Controls List Box
04/22/2022 Hudson Nandere Lubinga 23
Select the Event for the Event Procedure
• Select the Click event from the list of many available events
• Buttons have 57 possible events they can respond to
04/22/2022 Hudson Nandere Lubinga 24
Event Procedure Sub
• Beginning of Procedure is created for you
• If you create sub by double clicking on control it will create
a sub for the most commonly used event for that control
04/22/2022 Hudson Nandere Lubinga 25
Add the Event Code
• Write the code that you want executed when the user clicks on the
btnWelcome button
• Type: MsgBox (“Welcome to Visual Basic”)
• Must be contained within the Event Procedure Stub
04/22/2022 Hudson Nandere Lubinga 26
Writing Visual Basic Code
• Not Case Sensitive
• Visual Basic will “correct” case issues for you
• Keywords are in Blue
• Special reserved words
• Comments in Green
• Problems with Syntax (Language) will be underlined in
blue
04/22/2022 Hudson Nandere Lubinga 27
Coding Conventions
• Rules
• Use spaces to separate the words and operators
• Indentation and capitalization have no effect
• Recommendations
• Use indentation and extra spaces for alignment
• Use blank lines before and after groups of related
statements
• Code all variable declarations at the start of the procedure
• Group related declarations
04/22/2022 Hudson Nandere Lubinga 28
Comments
• Usage
• Type an apostrophe ( ' ) followed by the comment
• The compiler ignores everything on the line after ‘
• Used for documentation/readability and to disable chosen
statements during testing
• Recommendations
• Follow apostrophe with a star for readability ( ‘* )
• Use at beginning of program to indicate author, purpose, date,
etc.
• Use for groups of related statements and portions of code that
are difficult to understand
04/22/2022 Hudson Nandere Lubinga 29
Create Event Procedure for Exit Button
• Create an Event Procedure for when the btnExit button is clicked
• Have it display “Goodbye” in a MsgBox
• Then “End” – this will terminate the program
04/22/2022 Hudson Nandere Lubinga 30
Switching to Design Window
• You can switch between the Design Window and the
Code Window (once opened) by clicking on the tabs
at the top of the
• Design and Code Windows
• Form1.vb(Design) is the
• design window
• Form1.vb is the Code Window
04/22/2022 Hudson Nandere Lubinga 31
Running the Application
Click the Run Icon on the
Standard Toolbar
Or Press F5
This will begin the program
Display the Form/Window
Nothing will happen
Waiting on an Event
04/22/2022 Hudson Nandere Lubinga 32
Test the Events
• Click on the “Say Welcome” button
• The message box should display
• Click on the “Exit” button
• The message box should display
• The application should terminate
04/22/2022 Hudson Nandere Lubinga 33
Save the Project
• Make sure to save your work
• SAVE ALL (not Save Form)
• Visual Basic applications are
• made of several files -
• Often even several forms
04/22/2022 Hudson Nandere Lubinga 34