1 Introduction To Visual Basic
1 Introduction To Visual Basic
Coverage:
This chapter covers object-oriented terminology and concepts, the basic files comprising a VB project,
elements of the Visual Studio integrated development environment, VB Help, and the first hands-on project
introducing VB objects, properties, and methods.
INTRODUCTION
Objective
Course objective – learn to design and develop Windows-based business applications using Visual Basic.NET
programs that meet commercial programming standards.
Program design and coding is satisfactory.
Work is equivalent to that expected from someone already working in the information technology field as
a professional programmer.
Grade you according to commercial standards.
.NET Framework Class Library. This is a library of predefined class objects. It enables you to quickly
build a computer application through the use of predefined objects such as forms, text boxes, labels,
buttons, drop-down list controls, and others (mandatory).
Common Language Runtime (CLR). This component manages the execution of a programming
project written in any of the languages that are included within Visual Studio including Visual Basic as a
language (mandatory). This component is installed as part of the .NET Framework.
MSDN (Help). This is the help component and provides access to a help reference library. This is
covered in detail at the end of this set of notes. It is an optional, but highly recommended component.
VB is also termed an event-driven programming language because you will write program code that
responds to events that are controlled by the system user. Example events include:
Clicking a button or menu.
Opening or Closing a form.
Moving the mouse over the top of an object such as a text box.
Moving from one text box to another.
In order to work with VB, you need to understand "object" terminology as defined in Table 1.
Table 1
Terminology Definition
Object A thing – like a noun in English. Examples include forms and controls you place on forms such
as buttons, text boxes, and icons.
Property Objects have properties – like adjectives in English. Properties describe object
behaviors. Examples of properties include Text, Name, BackColor, Font, and Size.
Refer to a property by the notation ObjectName.PropertyName (use the .dot notation) –
example: TotalDueTextBox.Text or AccountLabel.ForeColor.
Method Like a verb in English – these are the actions that objects exhibit. Examples include methods to
Show and Hide forms and methods to Print and Close forms.
Refer to a method with the notation ObjectName.MethodName – example Me.Close will close
the current form.
Event Events are actions usually triggered by the system user such as clicking a button; however,
events can also be triggered by the actions of objects. For example, closing a form can trigger an
event.
Class This is a really abstract term – it is a sort of template for an object. For example, all forms
belong to the Form class of object. All buttons belong to the Button class of object. Classes
include definitions for object properties, methods, and associated events. Each class is assigned
an identifying namespace within the .NET Framework Class Library.
Each new object you create is defined based on its class – the new object is called a class
instance.
This is the Start Page for Visual Studio 2008 (Ultimate Edition).
Click the New Project link shown in the figure above to create a new project. This opens the New
Project dialog box shown below.
Your first project will be a Visual Basic Project using a Windows Forms Application template as
shown in the figures above.
The default name is WindowsApplication1 -- not a very useful name. Change the project name
to: Ch01VBUniversity.
Click the OK button – it takes several seconds to create the project files from the template.
You can also close/open these windows with the View menu.
Change the Title Bar value of a form by typing a new value into the Text property for the form.
o Select the Form.
o Click the Alphabetic icon shown in the figure below to list the properties alphabetically.
o Type the new value VB University – Student Information in the Text property of the Properties
window.
Size the form as needed by clicking the white squares around the form and dragging/dropping.
Set the form's StartPosition property to CenterScreen. This will cause the project when it runs to
display in the center of the computer monitor screen.
Set the form's Font property – usually you'll leave this at about an 8.25 point font, but in class I use
a 10 point bold font so everyone in the back of the room can see the overhead display.
Visual Basic automatically assigns a value to the Name property of each control, for
example, Label1, Label2,Label3, or TextBox1, TextBox2, TextBox3,
or Button1, Button2, Button3. However, it is difficult to remember the difference between Label1 and Label2
so:
if you are going to later refer to the controls, it is best to rename them to a more meaningful name,
if you are not going to refer to the controls later, then just use the assigned default name such as Label1.
When you name an object such as a Label or TextBox or Button, you must follow these rules:
An object name can begin with an alphabetic letter or the special “underscore” character.
An object name can include letters, digits, and underscores.
An object name CANNOT include a space or a punctuation mark.
An object name CANNOT be a VB reserved word such as Button, Close, or TextBox.
An object name can contain a VB reserved word – object names such
as PrintButton, CloseButton,NameTextBox, and MajorTextBox are legal names.
Several naming conventions exist within industry – the common ones are the Hungarian naming
convention and the Pascal naming Convention.
Naming conventions are simply guidelines to help other programmers read your code more easily. We will use
thePascal naming convention. The rules are:
Begin an object name with an uppercase alphabetic character.
Capitalize each word that is part of an object name.
Select object names that are meaningful.
Append the full name of the control class to the end of the name.
Avoid abbreviations unless they are standard abbreviations such as SSN (social security number).
Examples of valid names: MajorTextBox, ResetButton, MessageLabel, TotalDueTextBox,
andCloseButton.
This table gives examples of both the Camel Casing and Hungarian naming conventions.
2. Place a Label control on the form by either double-clicking the Label tool or by drag/dropping a label to
the form.
o Notice that when you select the Toolbox's label control, the mouse pointer on the form becomes a
crosshair showing you where the upper left corner of the label will be displayed.
o Drag the new label to where you want it in the upper left corner of the form.
o Change the label's Text property to Student Name:
o The label also has a Name property – the first label is named Label1. This is a satisfactory name
and you will not refer to the label when you later write programming code so leave it
named Label1.
3. Add a second label control on the form. Set the Text property to Academic Major: – this label is
namedLabel2.
If you accidentally place a control on a form and discover that you don't need the control, simply delete it.
Run the project. Confirm that the project starts up centered on the console, and the alignment of controls is
professional in appearance.
Locking Controls
After you finish sizing control on a form, you can lock the controls to prevent accidental movement of the
controls.
Right-click any control and select the Lock Controls option in the context menu that displays as shown in
the figure below.
Right-click again and select Lock Controls to unlock the controls.
You'll notice a small "padlock" icon on the upper left corner of the locked controls.
Program Coding
Programming or writing program code is the means used to cause something to happen when an event, such
as a button click occurs. There are six events to be coded for this project – the first three events are for the
three display buttons.
Click event for the button named Display1Button, Display2Button, and Display3Button.
Click event for the button named ResetButton.
Click event for the button named PrintButton.
Click event for the button named ExitButton.
You need to master the following coding statements and concepts in this section:
The form named StudentInfo is created as an instance of the general Form object defined in the .NET
Framework class library.
It is declared by the Public Class StudentInfo statement.
The End Class statement marks the end of the form's class definition.
'Project: Ch01VBUniversity
'D. Bock
'Today's date
'Illustrates displaying information to read-only
' textbox controls by clicking buttons.
Accessing Intellisense
VB's Intellisense feature makes it easier for you to type programming statements.
To access Intellisense simply begin to type the name of an object such as the NameTextBox control shown in
this figure – VB will pop up a window that displays possible selections – this makes it easier for you to type
code and leads to fewer typing errors.
With the Intellisense focus on the NameTextBox, just type a dot (period) and the Intellisense will next
display all of the properties of the NameTextBox – type the letters "Te" (see the figure below) and
Intellisense will search and find the Text property and set the focus to that property for this text box
control.
Now type an equal sign (=) followed by your name inside double-quotes, e.g., "Douglas Bock".
The statement you've typed should look like this inside the sub procedure – VB will provide the necessary
spacing automatically.
This is called an Assignment Statement and is discussed in detail in the next section.
End Sub
Assign a student name to the Text property of the TextBox control named NameTextBox and a student’s major
to the TextBox control named MajorTextBox. The assignment statements to do this are:
Notice that the value is enclosed within double-quote marks – this indicates the value is a string of characters
and only string data is stored to the Text property of a TextBox control. The value is displayed in brown text.
Return to the design view and double-click the Reset Form button control – this will generate a Click event
sub procedure for this button. Type a remark and two statements to clear the contents of the two TextBox
controls as shown here.
Return to the design view and double-click the Exit button control – this will generate a Click event sub
procedure for this button. Type a remark and a statement to close the form as shown here.
Return to the form’s Design View (the Toolbox does not display in code view).
As shown in this figure, locate the PrintForm component in the Visual Basic PowerPacks section of
the Toolbox.
Double-click the PrintForm component – you will see it display in the component tray of the IDE below
the form you are designing. It will be named PrintForm1.
The printing PrintAction options when printing a form are to print to: (1) Print to Printer, (2) Print to File, and
(3) Print to Preview – this third option saves paper so we will use it.
Double-click the Print Form button control and then enter remarks and two coding statements as shown here.
If you have not done so already, add Remark statements before the Public Class statement. These remarks
identify the project, you as the programmer, and the date the program was written and must ALWAYS be
included in the project.
Place the mouse cursor in front of the Public Class statement.
Click the Enter key several times to add blank lines above the Public Class statement.
Type additional remarks similar to those shown here.
'Ch01VBUniversity
'D. Bock
'Today's Date
'Illustrates displaying information to read-only TextBox
'controls by clicking Button controls
Managing Projects
Copying Your Project to a Flash Drive
You need to be able to save your project and copy your work to a backup device, such as a flash drive.
VB will save your project files every time you build or execute a project after your initial save.
VB projects consist of many different files and folders within folders.
Save files as you work by clicking the Save All button on the button toolbar. DO NOT USE the File-
Save As menu at any time to try to save the project – if you do, you will likely only save an individual
file, not the entire project, and you will not have a complete project saved.
You may wish to examine the project folder. This figure above shows the folder open. Notice that the
original project folder named Ch01VBUniversity has another folder inside of it with an identical
name. It also has the Ch01VBUniversity.sln solution file.
If you click the "inner" Ch01VBUniversity folder, you will find folders named bin, My Project,
and obj. You will also find at five files with names
like Ch01VBUniversity.vbproj, Ch01VBUniversity.vbproject.user,StudentInfo.designer.vb, Studen
tInfo.resx, and StudentInfo.vb as shown in the figure below.
You should confirm that the backup copy of the project folder on your flash drive contains folders and
files that are identical to the folders and files in your original project folder.
Opening an Existing Project
Use the Windows Start button, then find Microsoft Visual Studio.Net!
Locate the Ch01VBUniversity project listed under existing projects.
Click on Ch01VBUniversity.sln to launch the project – this will enable you to revise or continue work on
an existing project.
Program Errors
From time to time you will make errors when entering program code. VB expects you to follow specific rules
for syntax, punctuation, and spelling.
VB has a smart editor that attempts to find and fix most errors.
If VB cannot understand some of your code, it will display a blue squiggly line under the part of the line;
that cannot be interpreted.
In the figure below the blue squiggly line indicates that VB does not understand what
the textName ortextMajor objects are.
Most likely, you named these two objects NameTextBox and MajorTextBox when designing the form,
but then used the wrong name when typing code in the sub procedure.
There are two other types of errors that we will study in later chapters:
Run-Time error – an error or exception that occurs while running a program.
Logic error – a program error that causes the display of erroneous output.
A Clean Compile
After you correct errors in your code, access the Build menu and select the Rebuild option to recompile the
program into a new MSIL version.
Compiling means to convert the program from human-readable form to machine-readable form – all referenced
classes and components are combined by VB into an executable (.exe) file.
Design Time – when you are designing a project using the IDE, you are in what is termed Design
Time.
Run Time – when you execute or run a project, you are in Run Time.
Break Time – occurs whenever VB encounters some type of processing error that prevents program
execution from continuing. We will cover error-handling throughout your studies of VB.
VB Help
MSDN
The Microsoft Developer Network (MSDN) library contains books and technical articles to help you answer
questions that will arise as you learn the VB language. This must be installed on your computer in order for it
to be available from the Help menu within VB.
MSDN can be access from a hard drive, network drive, or the Web (requires an Internet connection).
Hard drive installation is fastest and most convenient.
Web access is from http://msdn.microsoft.com.
You may opt to view Help on the Internet or use local Help installed on the PC.
You can also obtain context-sensitive help on an object by placing the insertion point in a word in the coding
editor or by selecting a VB object and pressing the F1 function key.
A good way to start learning about Help is to access the Help menu Index option, then inside that window,
access the Help menu and look for Help on Help as a submenu – this explains how to look up topics in Help.
You're now ready to complete your first computer programming laboratory assignment. You may wish to
review these notes prior to starting your practice computer lab assignment.