37
Starting Out with Visual Basic .NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic .NET

Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Embed Size (px)

Citation preview

Page 1: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Chapter 2

Creating Applications With

Visual Basic .NET

Page 2: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

2.1Introduction

Page 3: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

A First Visual Basic .NET Application

• Develop your first application:• Display a map and written directions to

the Highlander Hotel

• Use a form with labels

• Use a PictureBox control

• Write an event procedure

Page 4: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

2.2Focus on Problem Solving:

Building the Hotel Directions Application

In This Section You Create Your First Visual Basic .NET Application: a Window That Displays a Map and Road Directions to a

Hotel

Page 5: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Clearly Define What the Program is To Do

• Purpose: Display a map to the Highlander Hotel

• Input: None

• Process: Display a form

• Output: Display a graphic image showing a map on the form

Page 6: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Visualize the Application Running on the Computer and Design its User Interface

Page 7: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Make a List of the Controls Needed

Control Type Description(Control Name)

Form A small form that will serve as (Default Name: Form1) the window onto which the other controls will be placed

Label Displays the message(Default Name: Label1) "Directions to the Highlander Hotel"

PictureBox Displays the graphic image(Default Name: PictureBox1) showing the map to the hotel

Page 8: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Define the Values ofEach Control's Relevant Properties

• Form• Name: Form1

• Text: "Directions"

• Label• Name: Label1

• Text: "Directions to the Highlander Hotel"

• TextAlign: MiddleCenter

• Font: Microsoft sans serif, bold, 18 point

• PictureBox• Name: PictureBox1

• Picture: HotelMap.jpg

• SizeMode: StretchImage

Page 9: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Use Visual Basic .NET toCreate the Forms and Other Controls

• Establish the Form

• Add the Labels

• Set the TextAlign Property, Font and Style

• Insert a PictureBox Control

• Try Running the Application

Page 10: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Project Organization on Disk

• Each Solution is stored as a Visual Basic .NET Project

• Within the Folder created with the project name are various files, including:• .sln contains data describing the solution• .vbproj contains data describing the project

Page 11: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Properties Window

• Used to view and modify the property values of a given object

• Two views of the properties:• Alphabetic (across all properties)• Categorized (groups properties by logical use)

Page 12: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

2.3Focus on Problem Solving:

Responding to Events

An Application Responds to Events, Such As Mouse Clicks and Keyboard Input, by Executing Code

Known As Event Procedures

In This Section, You Write Event Procedures for the Directions Application

Page 13: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Augment the Hotel Application

• Now the hotelowner wants toadd an optionto view writtendirections:

Page 14: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Controls to be Added

Control Type Description(Control Name)

Label Displays written directions to (lblDirections) the hotel

Button When clicked, causes the above(btnDisplayDirections) label to appear on the form

Button Stops the application(btnExit) when clicked

Page 15: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Direction's ApplicationControl Properties

• Label:• Name: lblDirections

• Text: "Traveling …"

• Button• Name: btnDisplayDirections

• Text: "Display Directions"

• Button:• Name: btnExit

• Text: "Exit"

Page 16: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Method btnDisplayDirections_Click, I

Private Sub btnDisplayDirections_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnDisplayDirections.Click' Make the directions visiblelblDirections.Visible = True

End Sub

Line Continuation Mark

Name of the event the procedure responds to

Name of the control that owns the event procedure

Marks the beginning of this event procedure

Page 17: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Method btnDisplayDirections_Click, II

Private Sub btnDisplayDirections_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnDisplayDirections.Click' Make the directions visiblelblDirections.Visible = True

End Sub

Makes the control lblDirections visible:Assigns the value True to the Visible Propertyof the lblDirections control.

Page 18: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Syntax for Referring to theValue of a Control's Property

• ControlName

• Dot

• PropertyName

• In this situation:• lblDirections.Visible

Page 19: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Syntax for an Assignment Statement

• Item receiving the value

• Equal symbol

• Value to be assigned

• In this situation:• lblDirections.Visible = True

Page 20: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Use Visual Basic .NET toUpdate the Application

• Place the label and the buttons on the form

• Enter the code for the two procedures

• Test the application

Page 21: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Additional Properties

• Color properties:• BackColor: Sets the background color• ForeColor: Sets the foreground (e.g., text) color

• Form style property value examples:• Sizable: (Default) Has normal buttons in upper

right and is resizable via the edges• Fixed3D: Has a 3D look; normal buttons; is not

resizable by its edges

Page 22: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

2.4Modifying the Text Property

With Code

Quite Often, You Will Need to Change a Control’s Text Property With Code

This Is Done With an Assignment Statement

Page 23: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

The Text Property Can BeModified via Code, I

• Suppose that a form was established with a label lblMessage that said:

1 Kilometer = ?

• And on a btnfeet button click, we wanted to change the value of the text property to

1 Kilometer = 3,281 feet

Page 24: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

The Text Property Can BeModified via Code, II

Private Sub btnFeet_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnFeet.Click

' Display the conversion to feet.lblMessage.Text = "1 Kilometer = 3,281 feet"

End Sub

Assigns the given string to the text propertyof lblMessageThis has the effect of changingthe previously displayed value to this one

Page 25: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

2.5The AutoSize, BorderStyle,and TextAlign Properties

The Label Control’s AutoSize Property Allows a Label to Change Size Automatically to Accommodate the

Amount of Text in Its Text Property

The BorderStyle Property Allows You to Set a Border Around a Label Control

Page 26: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

AutoSize Property for Labels

• AutoSize is a Boolean (True or False) Property of labels

• False (the default) means that the box size will not change, depending on the amount of text assigned to it

• True means that it will resize itself to fit variable amounts of text

Page 27: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

BorderStyle Property for Labels

• BorderStyle determines the look of the box• None (the default) means no border• FixedSingle means a border one pixel wide• Fixed3D gives it a recessed 3-dimensional look

Page 28: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

TextAlign Property for Labels

• The value of TextAlign establishes the text's justification:• TopLeft• TopCenter• TopRight• MiddleLeft• MiddleCenter

– MiddleRight– BottomLeft– BottomCenter– BottomRight

Page 29: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

2.6Clickable Images

Controls Other Than Buttons Have Click Event Procedures

Page 30: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

PictureBox Control

• As we saw earlier the Image Property can be set to an graphic of some sort

• The image is clickable

• This event can be handled by code to take whatever the appropriate action is

Page 31: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

PicutreBox Click Event code

• When PictureBox picUSA is clicked, lblMessage is set appropriately:

Private Sub picUSA_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles picUSA.Click' Display the country namelblMessage.Text = "United States of America"

End Sub

Page 32: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

2.7Using Visual Basic .NET Help

In This Section You Learn to Use the Visual Basic .NET Help System

Page 33: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Dynamic Help

• Dynamic Help provides help information that is relevant to the operation you are currently performing

• This window occupies the same location as the Properties window

• Simply select the tab at the bottom to select which you wish to view

Page 34: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Help Menu

• The usual categories of Help that you are probably accustomed to in Microsoft applications• Contents…• Index…• Search…

• Are available through this window also

Page 35: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

2.8Debugging Your Application

At Some Point, Most Applications Contain Bugs, or Errors That Prevent

the Application From Operating Properly

Page 36: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Types of Errors: Compile Errors

• These are errors in the syntax (form) of your program

• Visual Basic .NET will inform you of these as soon as they are found

• The area of the error will be shown with a jagged blue line

• A description of the error will be given in the Task List window

Page 37: Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET

Starting Out with Visual Basic .NET 2nd Edition

Types of Errors: Runtime Errors

• These errors occur while your program is running

• Visual Basic .NET will detect some of these and inform you about them

• Others you must detect yourselfAlways carefully check the operation of your program to be sure that it operates as required