You are on page 1of 23

Copyright 2011 Pearson Addison-Wesley

Addison Wesley
is an imprint of
2011 Pearson Addison-Wesley. All rights reserved.
Chapter 2
Creating Applications with
Visual Basic
Copyright 2011 Pearson Addison-Wesley
Addison Wesley
is an imprint of
2011 Pearson Addison-Wesley. All rights reserved.

Section 2.1
Focus on problem Solving: Building the
Directions Application

In this section ou create our !irst Visual


Basic application:

a window that displas a map and road


directions to a hotel. In the

process ou learn how to place controls on


a !orm and manipulate

various properties.
Copyright 2011 Pearson Addison-Wesley
"e!ine #hat the Application is $o
"o

%urpose: "ispla a map to the &ighlander


&otel

Input: 'one

%rocess: "ispla a !orm

(utput: "ispla on the !orm a graphic image


showing a map
Chapter 2 ) Slide *
Copyright 2011 Pearson Addison-Wesley
Visuali+e the Application and "esign Its
,ser Inter!ace

Below is a s-etch o! the application.s !orm


Chapter 2 ) Slide /
Copyright 2011 Pearson Addison-Wesley
"etermine the Controls 'eeded
Control Type Control Name Description
Form Form1 A small !orm that will serve as
0"e!ault 'ame1 the window onto which the other
controls will be placed
2abel 2abel1 "isplas the message
0"e!ault 'ame1 3"irections to the &ighlander
&otel3
%ictureBo4 %ictureBo41 "isplas the graphic image
0"e!ault 'ame1 showing the map to the hotel
Chapter 2 ) Slide 5
Copyright 2011 Pearson Addison-Wesley
"e!ine 6elevant %ropert Values !or 7ach
Control

Form

'ame: Form1

$e4t: 3"irections3

2abel

'ame: 2abel1

$e4t: 3"irections to the &ighlander &otel3

$e4tAlign: 8iddleCenter

Font: 8icroso!t sans seri!9 bold9 1: point

%ictureBo4

'ame: %ictureBo41

%icture: &otel8ap.;pg

Si+e8ode: StretchImage
Chapter 2 ) Slide <
Copyright 2011 Pearson Addison-Wesley
Create the Forms and (ther Controls ,sing
Visual Basic

7stablish the Form and set its $e4t propert

Add a 2abel control

%osition and resi+e it on the !orm

Set $e4t9 $e4tAlign9 and Font properties

Add a %ictureBo4 control

%osition and resi+e it on the !orm

Set Image propert to displa &otel8ap.;pg

6un the application

Close and save the application


Chapter 2 ) Slide =
Copyright 2011 Pearson Addison-Wesley
"esign 8ode9 6un 8ode9 and Brea- 8ode

Visual Basic has three modes in which it


operates:

"esign 8ode

the mode in which ou create the application

also -nown as design time

6un 8ode

e4ecutes the application in the Visual Studio


environment

also -nown as run time

Brea- 8ode

momentaril suspends e4ecution o! a running


application

!or testing and debugging purposes


Chapter 2 ) Slide :
Copyright 2011 Pearson Addison-Wesley
%ro;ect (rgani+ation (n "is-

,ser creates a new pro;ect in Visual Studio

A solution and a !older are created at the same


time with the same name as the pro;ect

$he pro;ect belongs to the solution

8ultiple pro;ects can be included in a solution

$he !older stores !iles related to the pro;ect


including:

A solution !ile 0.sln1

A pro;ect !ile 0.vbpro;1


Chapter 2 ) Slide >
Copyright 2011 Pearson Addison-Wesley
(pening an 74isting %ro;ect

,se 6ecent %ro;ects list on Start %age

%rovided it hasn.t been moved or deleted

,se (pen %ro;ect button on Start %age

$hen browse using (pen %ro;ect dialog bo4

,se (pen %ro;ect option on File menu

$hen browse using (pen %ro;ect dialog bo4


Chapter 2 ) Slide
1?
Copyright 2011 Pearson Addison-Wesley
%roperties #indow

,sed to view and modi! the propert values


o! a given ob;ect

$wo views o! the properties are available:

Alphabetic 0across all properties1

Categori+ed 0groups properties b logical use1


Chapter 2 ) Slide
11
Copyright 2011 Pearson Addison-Wesley
Addison Wesley
is an imprint of
2011 Pearson Addison-Wesley. All rights reserved.

Section 2.2
Focus on problem Solving:
6esponding to 7vents

An application responds to events9 such as


mouse clic-s and

-eboard input9 b e4ecuting code -nown


as event handlers.

In this section9 ou write event handlers !or


the "irections

application.
Copyright 2011 Pearson Addison-Wesley
8odi! the Directions Application

$he &ighlander &otel manager would li-e ou to add


the !ollowing items to the application:

A 2abel containing the written directions

A Button to displa the directions

A Button to e4it the application


Chapter 2 ) Slide
1*
Copyright 2011 Pearson Addison-Wesley
Controls to be Added

Control Type Control Name Description
2abel lbl"irections "isplas written directions
to the hotel
Button btn"ispla"irections #hen clic-ed9 causes
lbl"ispla"irections te4t
to appear on the !orm
Button btn74it Stops the application
when clic-ed
Chapter 2 ) Slide
1/
Copyright 2011 Pearson Addison-Wesley
Control %roperties

2abel

'ame: lbl"irections

$e4t: @$raveling on I):>9AetcB

Visible: False

Button

'ame: btn"ispla"irections

$e4t: @"ispla "irectionsB

Button

'ame: btn74it

$e4t: @74itB
Chapter 2 ) Slide
15
Copyright 2011 Pearson Addison-Wesley
$he Code #indow

"ouble)clic-ing a control in design mode:

opens the code window

creates a code template !or the control.s event


handler where ou !ill in the code !or the event
Chapter 2 ) Slide
1<
Copyright 2011 Pearson Addison-Wesley
$he Clic- 7vent &andler !or
btn"ispla"irections

Chapter 2 ) Slide
1=
Copyright 2011 Pearson Addison-Wesley
Changing a Control.s Visible %ropert in
Code

Speci! the control name (lblDirections)

$hen a dot

$hen the %ropert'ame (Visible)

For e4ample:

lblDirections.Visible

re!ers to the Visible propert o! the lbl"irections


control

$he visible propert values ma onl be true or


!alse
Chapter 2 ) Slide
1:
Copyright 2011 Pearson Addison-Wesley
$he Assignment Statement

Speci! the item to receive the value

$hen the eCual smbol

$hen the value to be assigned

For e4ample:

lblDirections.Visible = True

Assigns the value $rue to the Visible propert o!


the lbl"irections control

Causes the te4t o! the lbl"irections control to


become visible to the user
Chapter 2 ) Slide
1>
Copyright 2011 Pearson Addison-Wesley
Switching Between the Code #indow and
the "esigner #indow

#indow $abs

Solution 74plorer Icons

8enu Bar

Deboard Shortcuts

F= opens the Code #indow

Shi!t E F= opens the "esigner #indow


Chapter 2 ) Slide
2?
Copyright 2011 Pearson Addison-Wesley
$he Clic- 7vent &andler !or btn74it

Chapter 2 ) Slide
21
Copyright 2011 Pearson Addison-Wesley
,se Visual Basic to ,pdate the Application

%lace the label and the buttons on the !orm

7nter the code !or the two procedures

$est the application


Chapter 2 ) Slide
22
Copyright 2011 Pearson Addison-Wesley
,sing IntelliSense

IntelliSense is a !eature that provides


automatic code completion as ou tpe
programming statements

%ress the $ab -e to use IntelliSense

For 74ample:
Chapter 2 ) Slide
2*

You might also like