Getting Started With Visual Basic 2010
Getting Started With Visual Basic 2010
11/06/2020 1
Introduction
• Visual Studio 2010 is an integrated environment
for building, testing, debugging, and deploying
a variety of applications: Windows applications,
web applications, classes and custom controls,
and even console applications.
• In this chapter, you’ll learn how to do the
following:
– Navigate the integrated development environment
of Visual Studio
– Understand the basics of a Windows application
11/06/2020 2
Exploring the Integrated Development Environment
11/06/2020 4
The Start Page
• When you run Visual Studio 2010 for the first time,
you will be prompted to select the type of projects
you plan to build so that the environment can be
optimized for that specific type of development.
• I’m assuming that you have initially selected the
Visual Basic Development settings, which will
optimize your copy of Visual Studio for building
Windows and web applications with Visual Basic
2010.
11/06/2020 5
• After the initial configuration, you will see a
window similar to the one shown in Figure
11/06/2020 6
• The Recent Projects tab will be empty, of course,
unless you have already created some test
projects. Visual Studio 2010 will detect the
settings of a previous installation, so if you’re
upgrading from an earlier version of Visual
Studio. the initial screen will not be identical to
the one shown in Figure.
• Welcome Click the Welcome tab to see a series
of links that provide developer assistance for
using Visual Studio. These links include what’s
New In Visual Studio 2010, Creating Applications
With Visual Studio, and Extending Visual Studio,
among others.
11/06/2020 7
• Windows Here you’ll find a list of topics
related to Windows application development.
Windows applications, frequently referred to
as desktop applications, are the applications
you install on a local computer and execute
locally.
• Web Here you’ll find a list of topics related to
web application development. Web
applications are executed on a remote
computer, the web server, and you interact
with them through a browser.
11/06/2020 8
• Cloud, Office, SharePoint In addition to Windows and
web applications, Visual Studio can be used to develop
applications for Office and SharePoint as well as
applications that use a new Microsoft platform for
building distributed applications, the Azure platform.
• Data Here you’ll find a list of topics related to data-
driven programming. All applications that interact with
a database are data driven; they can be Windows or
web applications. The principles of interacting with a
database (retrieve, display, and update database data)
are the same regardless of whether you use them to
build Windows or web applications.
11/06/2020 9
• Recent Projects Here you see a list of the
projects you opened most recently with Visual
Studio and you can select the one you want to
open again.
• chances are you will continue working on the
same project as the last time.
• Each project name is a hyperlink, and you can
open a project by clicking its name.
• Above the list of recent projects there are two
hyperlinks —one for creating a new project
and another one for opening a new solution.
11/06/2020 10
• Most developers will skip the Start Page.
• To do so, open the Tools menu and choose the
Import And Export Settings command to start a
configuration wizard.
• In the first dialog box of the wizard, select the Reset
All Settings check box and then click the Next button.
• The next screen of the wizard prompts you for a
location in which to save the new settings so that
Visual Studio can read them every time it starts.
• Leave the default location and click Next again to
see the last screen of the wizard, in which you’re
prompted to select a default collection of settings.
11/06/2020 11
Starting a New Project
11/06/2020 12
• Open the File menu and choose New Project, or click the
New Project link on the Start Page.
• In the New Project dialog box that pops up, you’ll see a
list of project types you can create with Visual Studio.
• The most important ones are Windows Forms
Applications, which are typical Windows applications
with one or more forms (windows);
• Console Applications, which are simple applications that
interact with the user through a text window (The
console);
• Windows Forms Control Libraries, which are collections
of custom controls;
• and Class Libraries, which are collections of classes.
11/06/2020 13
11/06/2020 14
• For this project, select the Windows Forms
Application template; Visual Studio suggests
the name WindowsApplication1 as the project
name. Change it to MyTestApplication, select
the Create Directory for Solution check box,
and then click the OK button to create the
new project.
11/06/2020 15
11/06/2020 16
• The new project contains a form already: the
Form1 component in the Solution Explorer.
The main window of the IDE is the Form
Designer, and the gray surface on it is the
window of your new application in design
mode.
• Using the Form Designer, you’ll be able to
design the visible interface of the application
(place various components of the Windows
interface on the form and set their properties)
and then program the application.
11/06/2020 17
• You can always show any of the toolbars at any
time. Open the View menu and choose Toolbars.
You’ll see a submenu with 28 commands that are
toggles. Each command corresponds to a toolbar,
and you can turn the corresponding toolbar on or
off by clicking one of the commands in the
Toolbars submenu.
• The last item in the Toolbars submenu is the
Customize command; Customize leads to a dialog
box in which you can specify which of the toolbars
and which of the commands you want to see.
11/06/2020 18
• Use this menu to customize the environment
for the way you work with Visual Studio. You
can hide just about any component of the IDE,
except for the main menu — after all; you
have to be able to undo the changes!
11/06/2020 19
Using the Windows Form Designer
• To design the form, you must place on it all
the controls you want to display to the user at
runtime. The controls are the components of
the Windows interface (buttons, text boxes,
radio buttons, lists, and so on).
• Open the Toolbox by moving the pointer over
the Toolbox tab at the far left.
11/06/2020 20
The controls are organized into groups according to function on the interface.
11/06/2020 21
• To place a control on the form, you can
double-click the icon for the control.
• A new instance with a default size will be
placed on the form. Then you can position and
resize it with the mouse. Or you can select the
control from the Toolbox with the mouse and
then click and drag the mouse over the form
and draw the outline of the control.
• The control properties will be displayed in the
Properties window.
11/06/2020 22
• This window, at the far right edge of the IDE
and below the Solution Explorer displays the
properties of the selected control on the form.
If the Properties window is not visible, open
the View menu and choose Properties
Window, or press F4.
• If no control is selected, the properties of the
selected item in the Solution Explorer are
displayed. In the Properties window, also
known as the Properties Browser.
11/06/2020 23
• The properties are organized in categories
according to their role.
• The properties that determine the appearance
of the control are listed alphabetically under
the header Appearance.
• the properties that determine the control’s
behavior are listed alphabetically under the
header Behavior, and so on.
• You can click the AZ button on the window’s
title bar to display all properties in alphabetical
order.
11/06/2020 24
11/06/2020 25
• Some controls, such as the Label, Button, and
CheckBox controls, support the AutoSize
property; AutoSize determines whether the
control is resized automatically to
accommodate the caption.
• TextBox control, as well as many others,
doesn’t support the AutoSize property.
• The TextBox control accepts a single line of
text, and you must set its MultiLine property
to True before you can resize the TextBox
control vertically.
11/06/2020 26
• The Multiline property determines whether
the TextBox control can accept one (if
Multiline = False) or more (if Multiline = True)
lines of text.
• To examine the control’s behavior at runtime,
press F5 or debug Menu and then choose
Start Debug. The application will be compiled,
and a few moments later.
11/06/2020 27
• When you’re finished, open the Debug menu
and choose Stop Debugging. This will
terminate your application’s execution, and
you’ll be returned to the IDE.
• The Stop Debugging command is also available
as a button with a blue square icon on the
toolbar. Finally, you can stop the running
application by clicking the Close button in the
application’s window.
11/06/2020 28
Understanding the IDE Components
• The IDE of Visual Studio 2010 contains
numerous components and it will take you a
while to explore them.
• In the following sections, I will go through the
basic items of the IDE
11/06/2020 29
The IDE Menus
11/06/2020 30
File Menu
• The File menu contains commands for
opening and saving projects or project items
as well as commands for adding new or
existing items to the current project.
• For the time being, use the New _ Project
command to create a new project; Open _
Project/Solution to open an existing project or
solution, Save All to save all components of
the current project, and the Recent Projects
submenu to open one of the recent projects.
11/06/2020 31
Edit Menu
• The Edit menu contains the usual editing
commands. Among these commands are the
Advanced command and the IntelliSense
command.
• Note that these two items are visible only
when you’re editing your code and are
invisible while you’re designing a form.
11/06/2020 32
Edit _ Advanced Submenu
• The following options are the more-interesting ones
available through the Edit _ Advanced submenu:
– View White Space: Space characters (necessary to indent lines
of code and make it easy to read) are replaced by periods.
– Word Wrap When a code line’s length exceeds the length of
the code window, the line is automatically wrapped.
– Comment Selection/Uncomment Selection Comments are
lines you insert between your codes statements to document
your application. Every line that begins with a single quote is a
comment; it is part of the code, but the compiler ignores it.
– The Comment Selection/Uncomment.Selection command
allows you to comment (or uncomment) large segments of
code in a single move.
11/06/2020 33
Edit _ IntelliSense Submenu
• IntelliSense is a feature of the editor (and other
Microsoft applications) that displays as much
information as possible, whenever possible.
• When you type the name of a control and the
following period, IntelliSense displays a list of the
control’s properties and methods so that you can
select the desired one.
• When you type the name of a function and an
opening parenthesis, IntelliSense will display the
syntax of the function — its arguments.
11/06/2020 34
• List Members:
– When this option is on, the editor lists all the members
(properties, methods, events, and argument list) in a drop-
down list.
– This list appears when you enter the name of an object or
control followed by a period. Then, you can select the desired
member from the list using either the mouse or the keyboard.
• Parameter Info:
– While editing code, you can move the pointer over a variable,
method, or property and see its declaration in a pop-up box.
– You can also jump to the variable’s definition or the body of a
procedure by choosing Go To Definition from the context
menu that appears if you right-click the variable or method
name in the code window.
11/06/2020 35
• Quick Info:
– is another IntelliSense feature that displays
information about commands and functions. When
you type an opening parenthesis following the name
of a function, for example, the function’s arguments
will be displayed in a ToolTip box. The first argument
appears in bold font; after a value for this argument is
entered, the next one is shown in bold.
• Complete Word
– The Complete Word feature enables you to complete
the current word by pressing Ctrl+ spacebar.
11/06/2020 36
• Insert Snippet
– This command opens the Insert Snippet window
at the current location in the code editor window.
Code snippets, which are an interesting feature of
Visual Studio 2010
11/06/2020 37
Edit _ Outlining Submenu
• To simplify the management of the code
window, the Outlining submenu contains
commands that collapse and expand the
various procedures.
• You can expand a procedure’s listing at any
time by clicking the plus sign. When you do so,
a minus sign appears in front of the
procedure’s name, and you can click it to
collapse the body of the procedure again.
11/06/2020 38
• Hide Selection
– This option lets you hide the selected code segment.
You can select part of a routine or multiple routines,
which are hidden as a whole with this command.
– To display the hidden code, click the plus icon on the
left margin, or use the Stop Hiding Selection
command.
• Toggle Outlining Expansion
– This option lets you change the outline mode of the
current procedure.
– If the procedure’s definition is collapsed, the code is
expanded, and vice versa.
11/06/2020 39
• Toggle All Outlining
– This option is similar to the Toggle Outlining
Expansion option, but it toggles the outline mode
of the current document.
– A form is reduced to a single statement. A file with
multiple classes is reduced to one line per class.
• Stop Outlining
– This option turns off outlining and adds a new
command to the Outlining submenu, Start
Automatic Outlining, which you can select to turn
on automatic outlining again.
11/06/2020 40
• Stop Hiding Current This option stops hiding
the currently hidden selection.
• Collapse To Definitions This option reduces
the listing to a list of procedure headers.
11/06/2020 41
View Menu
• This menu contains commands that allow you
to display any toolbar or window of the IDE.
• The Other Windows command leads to a
submenu with the names of some standard
windows, including the Output and Command
windows. The Output window is the console
of the application.
• The Command window allows you to enter
and execute statements. When you debug an
application, you can stop it and enter VB
statements in the Command window.
11/06/2020 42
Project Menu
• This menu contains commands for adding
items to the current solution.
• The last option in this menu is the Project
Properties command, which opens the
project’s properties pages.
• The Add Reference and Add Web Reference
commands allow you to add references to
.NET components and web components
respectively.
11/06/2020 43
Build Menu
11/06/2020 46
• Help Menu
– This menu contains the various help options.
– The Dynamic Help command opens the Dynamic
Help window, which is populated with topics that
apply to the current operation.
– The Index command opens the Index window, in
which you can enter and get help on a specific
topic.
• The Toolbox Window
– The Toolbox window contains all the controls you
can use to build your application interface.
11/06/2020 47
• The Solution Explorer Window
– The Solution Explorer window contains a list of the
items in the current solution.
– A solution can contain multiple projects, and each
project can contain multiple items.
– The Solution Explorer displays a hierarchical list of all
the components, organized by project.
• The Properties Window
– This window (also known as the Properties Browser)
displays all the properties of the selected component
and their settings. Every time you place a control on a
form, you switch to this window to adjust the
appearance of the control.
11/06/2020 48
• The Output Window
– The Output window is where many of the tools,
including the compiler, send their output.
– Every time you start an application, a series of
messages is displayed in the Output window.
– These messages are generated by the compiler,
and you need not understand them at this point. If
the Output window is not visible, choose View _
Other Windows _ Output from the menu.
11/06/2020 49
• The Command and Immediate Windows
– While testing a program, you can interrupt its
execution by inserting a breakpoint.
– When the breakpoint is reached, the program’s
execution is suspended and you can execute a
statement in the immediate window.
– Any statement that can appear in your VB code
can also be executed in the immediate window. To
evaluate an expression, enter a question mark
followed by the expression you want to evaluate.
– ? Math.Log(35)
11/06/2020 50
• The Error List Window
– This window is populated by the compiler with
error messages if the code can’t be successfully
compiled. You can double-click an error message
in this window and the IDE will take you to the line
with the statement in error — which you should
fix.
11/06/2020 51
Building a Console Application
• Apart from Windows applications, you can use
Visual Studio 2010 to build applications that
run in a command prompt window. The
command prompt window isn’t really a DOS
window.
• Start a new project. In the New Project dialog
box, select the template Console Application.
You can also change its default name from
ConsoleApplication1.
11/06/2020 52
Using Code Snippets
• Visual Basic 2010 comes with a lot of predefined
code snippets for selected actions, and you can
insert these snippets into your code as needed.
• Let’s say you want to insert the statements for
writing some text to a file, but you have no idea
how to access files.
• Then open the Edit menu and choose
IntelliSense _ Insert Snippet (or right-click
somewhere in the code window and choose
Insert Snippet from the context menu).
11/06/2020 53
END
11/06/2020 54