Visual Basic.
NET
Introduction
Week 1 – Lesson 1
Objectives
• this lecture will cover:
– invoking Visual Basic
– main components of the IDE
– opening new and existing projects
– saving projects
– changing object properties
– naming objects
– creating simple interfaces using labels and buttons
Visual Basic Slide 2 of 27
Visual Basic.NET
• .NET Framework has 2 main components:
– common language runtime
– class library
• VB.NET:
– powerful programming language
– ideally suited to rapid development of graphical based
software
• VB6 is not compatible with VB.NET
Visual Basic Slide 3 of 27
Development Platform
• Visual Studio 2005/2008 Express Edition
– Available for free download from:
• http://www.microsoft.com/express/download/
• Visual Studio 2005/2008 Standard Edition
or Professional Edition
Visual Basic Slide 4 of 27
Application development
• Determine the interface of the application
• Select the best controls to be used in the
application
• Adjust the control properties e.g. Font size, text,
etc.
• Determine the event to be used for the controls
• Add code to objects
• Test and execute the program
Visual Basic Slide 5 of 27
Using Visual Basic.NET
• Fundamental operations:
– invoking VB.NET
– creating a new project
– saving a project
– opening an existing project
– closing Visual Basic
Visual Basic Slide 6 of 27
IDE screen
Menus &
Toolbar
Solution
Explorer
Toolbox
Properties
Error List Panel
Warnings Messages Form Window
Visual Basic Slide 7 of 27
IDE components
• TitleBar and MenuBar
• Toolbars
– short cuts to most common tasks
• Toolbox
– Contains the list of objects for use on a form
• Solution Explorer Window
– items contained in the current project
• Form Window
– screen the user will see
• Properties Window
– properties of controls on forms
• Error List
– details of errors and warnings
Visual Basic Slide 8 of 27
Designing the Interface
• Place appropriate controls on form
– click object then click form
– click an object and drag it on the form
– double click object
• Change the appearance of controls
– properties given default settings on creation
– properties displayed in the Properties Window
– settings can be modified at design time
Visual Basic Slide 9 of 27
Changing properties
• properties are altered by:
– supplying a new value, eg. Name
– selecting from alternatives, eg. Enabled
– choosing from a layout, eg. TextAlign
– using the colour pickers, eg. ForeColor
– using the menu, eg. Font
Visual Basic Slide 10 of 27
Properties Panel
• Properties determine:
– how objects are displayed
– how objects behave
• Each object has set of properties:
– appearance properties
– colour properties
• Custom, Web or System
– location and size properties
Visual Basic Slide 11 of 27
Standard Naming Conventions for Controls
Object Class Prefix Example
Form frm frmDataEntry
Button btn btnExit
TextBox txt txtPaymentAmount
Label lbl lblTotal
Radio Button rad radBold
CheckBox chk chkPrintSummary
Horizontal ScrollBar hsb hsbRate
Vertical ScrollBar vsb vsbTemperature
PictureBox pic picLandscape
ComboBox cbo cboBookList
ListBox lst lstIndegredients
Visual Basic Slide 12 of 27
Naming objects
• Objects naming convention
– Name property should be changed before writing the
program code
– Begin with a 3 character prefix name of the object type
– up to 200 alphanumeric character and the underscore
character may be used
– The name must not be a Visual Basic reserved word
– The name of the object should reflect the functionality
of the control
• E.g. For a button to clear the contents of the form, it should be
named btnClear
Visual Basic Slide 13 of 27
Label object
A label
• Contents can not be modified by the user
• Used for:
– descriptions of other controls
– results and instructions produced by program
• Main properties:
– Name: name of the label (lbl...)
– Text: text to be displayed
– AutoSize: whether label is automatically
resized to display all the Text
– TextAlign: alignment of the contents
– BorderStyle: style of border
Visual Basic Slide 14 of 27
Button object
• A button is used for execution of an action
• Main properties:
– Name: name of the object (btn...)
– Text: indicates button’s function
– Enabled: whether control can be used
• Can add an ampersand (&) in front of a character in the Text
property used as a hot key (shortcut key) i.e. press [ALT +
character] to trigger the button
Shortcut key enabled
Add an ampersand (&) symbol before the
button
character to be used as a shortcut key
Visual Basic Slide 15 of 27
TextBox object
• A textbox control is used for user to enter text
• Main properties:
– Main: Name of the textbox (txt…)
– Text:
• The text to be displayed and the text entered by the user
– TextAlign:
• Sets the alignment of the text
– BorderStyle:
• set the style of border to be used, either as Fixed3D
(default), FixedSingle or None
Visual Basic Slide 16 of 27
Hello World Project
Design the User Interface
Form Title
lblMessage
btnPush
btnExit
Visual Basic Slide 17 of 27
Properties Window-Label1
Rename Label1
(default name) to
lblMessage
Visual Basic Slide 18 of 27
Set other Properties
• Label
– Name lblMessage
– Text leave blank
• Button 1
– Name btnPush
– Text Push Me
• Button 2
– Name btnExit
– Text Exit
• Form
– Name frmHello
– Text Hello World by your name
Visual Basic Slide 19 of 27
Write 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 you care about, the
events you want to respond with code
• Code is written as event procedures
• VB will ignore events for which you do not
write code
Visual Basic Slide 20 of 27
Editor Window
• Declarations Section
• Class list
• Method list
Visual Basic Slide 21 of 27
Remark Statement
• Also known as Comment, used for
documentation
• Non-executable
• Automatically colored Green in Editor
• Begins with an apostrophe ( ' )
– On a separate line from executable code
– At the right end of a line of executable code
' Display the Hello World message
Visual Basic Slide 22 of 27
Assignment Statement
• Assigns a value to a property or
variable
• Operates from right to left
• Enclose text strings in quotation marks
(" ")
lblMessage.Text=" Hello World "
Visual Basic Slide 23 of 27
Ending a Program
• Execute the Exit Method of the Form
• Methods always have parentheses (this
will help you distinguish them from Properties
which never have parentheses)
• Current program may be referenced as
Application
Application.Exit()
Visual Basic Slide 24 of 27
Test and Debug
• Save Project - File Menu, Save All
• Run Project
– Debug Menu, Start
– Start Without Full Compile (F5)
– Start With Full Compile (CTRL F5)
• Correct any Errors and Rerun
– Compile errors
– Run-Time Errors
"Help is always available from the Help
– Logic errors Menu or by pressing F1."
– Syntax errors
Visual Basic Slide 25 of 27
Print the Code
• File Menu, Print
• Prints complete code listing
• Uses arrow symbol to denote line
continuation
Visual Basic Slide 26 of 27
Summary
• this lecture has covered:
– how the VB.NET development environment is used to
create, save and modify projects
– the role of properties in creating the user interface
– naming objects according to a widely used convention
– developing simple user interfaces using labels and
buttons
Visual Basic Slide 27 of 27